안드로이드 헤드업 알림 안됨 - andeuloideu hedeueob allim andoem

// foreground 상태에서 해드업 알림
    private void sendNotification2(String title, String body, Intent intent) {

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        String NOTIFICATION_CHANNEL_ID = "fore001";

        if (intent == null)
            intent = Intent.makeRestartActivityTask(new ComponentName(this, SplashActivity.class));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "푸시 알림", NotificationManager.IMPORTANCE_HIGH);

            // Configure the notification channel.
            notificationChannel.setDescription("Channel description");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(new long[]{0, 1000});
            notificationChannel.enableVibration(true);
            notificationManager.createNotificationChannel(notificationChannel);
        }


        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

        notificationBuilder.setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("TICKER")
                .setPriority(NotificationManager.IMPORTANCE_HIGH)
                .setContentIntent(pendingIntent)
                .setContentTitle(title)
                .setContentText(body);
                //.setContentInfo("Info");
        notificationManager.notify(/*notification id*/1, notificationBuilder.build());
    }

* 채널 아이디를 디폴트 채널아이디와 동일하게 설정하면 해드업이 나타나지 않음.


프로그래밍/Android

2021. 4. 6. 17:13

1. Head Up Notification

Android O (API26)부터는 Notification을 하려면 Channel을 먼저 생성해주어야 한다.

안드로이드 헤드업 알림 안됨 - andeuloideu hedeueob allim andoem
안드로이드 헤드업 알림 안됨 - andeuloideu hedeueob allim andoem
  • Channel Id : 고유한 ID여야 하고 길면 잘릴 수 있다.
  • Importance : 헤드업 알림되어야 한다면 IMPORTANCE_HIGH로 설정하고, 아니면 IMPORTANCE_DEFAULT로 설정한다.

채널이 생성되었다면, 해당 채널로 Notification을 생성한다.

안드로이드 헤드업 알림 안됨 - andeuloideu hedeueob allim andoem
안드로이드 헤드업 알림 안됨 - andeuloideu hedeueob allim andoem
  • setPriority() : 헤드업 알림을 위해서는 PRIORITY_HIGH로 설정한다.
  • setSmallIcon() : 알림시 보여지는 아이콘. 반드시 필요
  • setContentTitle() : 제목 텍스트. (생략 가능)
  • setContentText() : 본문 텍스트. (생략 가능)
  • setDefaults() : 알림시 효과음, 진동 여부. (Android M(갤s6)에서는 이 값이 설정되어야 헤드업 알림된다.)
  • setStyle() : text는 한줄이 넘어가면 짤린다. 모든 텍스트를 보여주기 위해서는 BigText로 설정해야 한다.
  • setTimeoutAfter() : 지정한 시간 이후 알림이 취소된다. (화면에서 사라진다)
  • notify() : 생성한 notification을 등록한다. id가 같으면 1개의 notification만 보여지고 다르면 여러개가 보여진다.

2. Clickable Notification

우선 Intent를 생성하고, Notification 클릭시에 실행할 activity를 지정한다.

안드로이드 헤드업 알림 안됨 - andeuloideu hedeueob allim andoem

그리고 Notification에 PendingIntent를 지정하면 해당 Notification은 클릭이 가능해진다.

안드로이드 헤드업 알림 안됨 - andeuloideu hedeueob allim andoem
  • setContentIntent() : notification 클릭시 실행될 PendingIntent를 지정한다.
  • setAutoCancel() : true이면 알림 클릭시 화면에서 사라진다. 만약 PendingIntent가 지정되지 않았다면, 알림이 클릭되지 않으므로 setAutoCancel은 동작하지 않는다.
FLAG_UPDATE_CURRENT : PendingIntent가 이미 존재할 경우, Extra Data를 모두 대체

FLAG_CANCEL_CURRENT : PendingIntent가 이미 존재할 경우, 기존 PendingIntent를 cancel하고 다시 생성