Things take time

[Android] 오레오(8.0) 업데이트 및 노티피케이션 채널(Notification Channel) 본문

Android(기능)

[Android] 오레오(8.0) 업데이트 및 노티피케이션 채널(Notification Channel)

겸손할 겸 2017. 12. 1. 11:54

[오레오]


SDK 26버전, 안드로이드 오레오가 업데이트되었다. 아직 넥서스로만 테스트중이긴하나..

어쨌든 이후엔 마이그레이션 해야하기 때문에 정보를 알아보는 중이다.


https://developer.android.com/about/versions/oreo/android-8.0-changes.html?hl=ko


[요약]


1. 권한 => 권한은 위험 권한 같은 경우, 개발자가 직접 권한을 요청해야하는데.. 이때 요청할 때 필요한 권한 그룹들의 목록을 모두 다 적어야한다. 예를 들어, 기존의 경우 WRITE_EXTERNAL_STORAGE 권한의 경우, 해당 권한을 허용하면 자동적으로 READ_EXTERNAL_STORAGE가 부여됐으나, 이제는 권한 요청시 배열안에 READ도 같이 넣으라는 의미이다. 같은 권한 그룹에 있다 하더라도, 필요한 권한은 모두 다 명시해서 적는 것이 중요하다.


2. 푸시 => 이제 푸시를 받았을 때, 사용자에게 보여지는 Notification에서 채널(Channel)이란 개념이 생겨, Oreo이상의 기기에서는 Channel을 생성해줘야한다. 이전 기본 생성자는 deprecated되었다.


3. 백그라운드 => 앱이 백그라운드 상태에 돌입하면 위치 정보 등 백그라운드 앱 기능이 저하되었다. 배터리 관련 이슈와 연관있는 듯 하다.



어쨌든, 푸시 쪽으로만 보겠다. 채널이라하여 푸시를 그룹화할 수가 있게 되었다. 사용자는 설정 -> 앱 -> 해당 앱 알림에서 각 채널로 구분된 푸시 그룹을 받을지 말지 등 설정이 가능하게 됐다. 이는 개발자가 제어할 수는 없는 영역이라 한다.



[코드]

         PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                    PendingIntent.FLAG_ONE_SHOT);
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= 26) {
                Log.e("오레오","오레오레오");
                NotificationChannel mChannel = new NotificationChannel("andokdcapp", "andokdcapp", NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(mChannel);
                notificationBuilder = new NotificationCompat.Builder(this,mChannel.getId());
            } else {
                notificationBuilder = new NotificationCompat.Builder(this);
            }

            notificationBuilder.setAutoCancel(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                    .setContentTitle(contentTitle)
                    .setContentIntent(pendingIntent)
                    .setSound(defaultSoundUri);

            notificationManager.notify(0
                    // ID of notification
                    , notificationBuilder.build());


코드를 보면 오레오 버전 이상일 때는 Channel을 하나 생성하고 create로 해줘야 한다. 그리고 notificationBuilder의 생성자에 넣어주면 된다. 필요시에 채널을 여러 개 생성하여 넣어주면 끝


아직 그룹화에 대해 필요성을 못느껴서 예제를 작성하진 않았으나 이를 기본으로 하면 오레오 대응 중에 푸시쪽은 해결되었다.



** 참고


이번에 targetSDK를 올리면서 compile되는 dependencies를 변경하다보니.. 위와 같은 소스로도 노티가 안나오는 현상이 발견되었다. 이는 compile에 있는 appcompat 버전과 관련이 있는 것을 찾았으니 아래와 같은 소스 버전을 기준으로 했다는 것을 참고하면 된다.

android { compileSdkVersion 26 buildToolsVersion '26.0.2' defaultConfig { applicationId "패키지" minSdkVersion 16 targetSdkVersion 26 ... } ... } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.0.0' compile 'com.google.firebase:firebase-messaging:11.0.4'