ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 안드로이드 10 (Q) head up notification 띄우기
    Android 2020. 11. 3. 02:59

     

     

    build.gradle (app)

    android {
        compileSdkVersion 29
        ...
        defaultConfig {
            ...
            targetSdkVersion 29

    안드로이드 10의 SDK 버전인 29로 설정

     

     

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH; //head up notification
        NotificationChannel channel = new NotificationChannel(MY_CHANNEL_ID, MY_CHANNEL_NAME, importance);
        channel.setDescription("MyDescriptionText");
        notificationManager.createNotificationChannel(channel);
     }

    안드로이드 8 Oreo 부터는 Notification을 채널에 할당해야 작동한다.

    head up 알람을 위해서는 NotificationChannel 객체에서 중요도를

    IMPORTANCE_HIGH 이상으로 설정한다.

    NotificationManager 객체에 NotificationChannel 객체를 설정한다.

     

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, MY_CHANNEL_ID);
    builder.setPriority(NotificationCompat.PRIORITY_HIGH)	//head up notification
            .setDefaults(NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE)

    이전 버전(안드로이드 7.1이하)에서는 각 알림의 중요도가

    NotificationCompat.Builder 객체에 Priority 값으로 결정된다.

    PRIORITY_HIGH 이상 설정해야 head up을 띄운다.

    또한 vibrate와 sound값을 설정해주는 것이 좋다.

     

    notificationManager.notify(CHANNEL_ID, builder.build());

    그외에 Builder 설정값은 원하는대로 하고

    NotificationManager 객체의 notify 메서드 호출하면 Notification을 띄운다.

     

     

    setGroup으로 그룹핑시킨 notification의 경우도 마찬가지의 방법이다.(summary 에 따로 설정할필요X)

     

    ※ 변경된 PRIORITY 값을 적용할 경우

    이전 디바이스에 빌드한 앱을 삭제하고 다시 빌드해야한다. 그러지 않으면 값이 바뀌지 않는다.

    참고 : stackoverflow.com/questions/47969923/how-to-show-heads-up-notification-in-oreo-or-higher/53294969

     

     


    full screen 알람 띄우기

     

    위의 head up 알람의 경우 나타났다가 잠시 뒤 자동으로 들어간다.

    full screen 알람을 사용할경우 사용자가 밀거나 클릭하는 액션을 취하지 않으면 화면앞에 사라지지 않는다.

    또한 PendingIntent를 사용하여 새 Activity를 띄우는 등 다양하게 활용할 수 있다.

    full screen 알람의 경우도 위와 같이 중요도를 HIGH 이상 설정해야 적용된다.

     

     

    AndroidManifest.xml

        <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />

    안드로이드 10 부터 이러한 권한이 필요하다.

     

    Intent myIntent = new Intent(context, MyActivity.class);
    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, MY_CHANNEL_ID);
    builder.setFullScreenIntent(fullScreenPendingIntent, true);

    NotificationCompat.Builder 객체에 추가로 Full Screen Intent를 설정한다.

     

     

     

    참고 : www.theteams.kr/teams/6636/post/69665

     

Designed by Tistory.