티스토리 뷰

Android/자료정리

[Oreo]OS 8.0 GCM 대응하기

썩소천사 2018. 6. 21. 10:54
반응형

8월부터  targetSDK버전을 26으로 올려야 하는 이슈가 있기 때문에 큰 업데이트를 앞두고 25에서 26으로 변경

실 배포까지 했는데 crash가 마구 쏟아지고, OS 8.0사용자만 발생하는 이슈라고 나온다.


GcmBroadcastReceiver.java line 19


그렇다!! FCM이 아니라면 GCM 사용자의 경우 예외처리가 필요하다.

방법은 JobService 또는 NotificationChannel을 사용하면 된다.

JobService를 사용하기 위해선 minSDK 버전이 21이상이어야 한다. 

즉 OS 5.0이상 기기에서만 쓸 수 있어서 패스~


NotificationChannel을 쓰기로 한다.

기존 Notification에 채널 옵션만 추가하면 된다. 생각보다 셋팅이 간편하다.

하지만 옵션질이 존재했다.

자세한 내용은 구글 문서를 참조하자.

https://developer.android.com/reference/android/app/NotificationChannel


최종적으로 적용한 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public static final String ANDROID_CHANNEL_ID = "GCM_01";
 
Notification.Builder mBuilder = new Notification.Builder(this, ANDROID_CHANNEL_ID)
        .setContentTitle(getString(R.string.app_name))
        .setContentText("message")
        .setSmallIcon(R.mipmap.ic_launcher_push)
        .setAutoCancel(true);
 
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
 
Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + this.getPackageName() + "/" + R.raw.sound);
AudioAttributes attributes = new AudioAttributes.Builder()
        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
        .build();
 
NotificationChannel mChannel = new NotificationChannel(ANDROID_CHANNEL_ID, title, NotificationManager.IMPORTANCE_HIGH);
mChannel.enableLights(true);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{500500});
mChannel.setSound(sound, attributes);
PendingIntent contentIntent = PendingIntent.getActivity(this, notiId, intent, 0);
mBuilder.setContentIntent(contentIntent);
 
mNotificationManager.createNotificationChannel(mChannel);
mNotificationManager.notify(notiId, mBuilder.build());
cs


몇가지 난관이 있었다면 8.0부터 알림에 레벨 지정이 가능하다.

채널 등록시 레벨 설정이 가능한데 레벨에 따라 진동이나 사운드가 발생하지 않을 수 있다.

IMPORTANCE_LOW로 설정했다 진동이나 알림이 오지않아 고생 좀 했다.

NotificationManager.IMPORTANCE_HIGH로 변경할 경우 해결된다.

특히 NotificationChannel 옵션을 변경할 경우 앱삭제, 또는 앱 캐시 및 데이터 제거를 해야 변경된 코드가 적용된다.


참고로 8.0에서는 푸시 발송시 즉각 처리가 아닌 시스템이 관여하기 때문에 30초 정도 기다려야 한다.


다른 앱들의 푸시 상세정보를 눌러보면 항목이 제각각으로 표시되는데 개발 설정 및 gcm, fcm에 따라 다르게 표기되는 것으로 보인다.

채널에 setDescription을 쓸 경우 상세 정보에 표시되기도 한다. 


셋팅이 좀 더 세밀해 졌지만 크기는 더 커졌다. 고로 일이 많아졌다.


반응형
댓글
반응형