Android

Android 13 TIRAMISU 포그라운드 서비스 실행시 알림 상태 표시줄 고정하는 방법

포그라운드에서 서비스 실행시 알림은 필수 사항이다. 즉 포그라운드 서비스는 상태 표시줄에 대한 알림을 제공해한다는 의미이다. 이 때 우선순위가 PRIORITY_LOW이상이어야, 사용자가 앱이 하는 일을 인식할 수 있다.

참고: Android 9(API 레벨 28) 이상을 대상으로 하고 포그라운드 서비스를 사용하는 앱은 FOREGROUND_SERVICE 권한을 요청해야 한다. 이 권한은 정상 권한이므로 시스템은 요청 앱에 자동으로 권한을 부여한다.

API 레벨 28을 대상으로 하는 앱이 FOREGROUND_SERVICE를 요청하지 않고 포그라운드 서비스를 생성하려고 시도하면, 시스템이 SecurityException을 발생시킨다.


상태표시줄에 알림이 고정이 안되는 이유는?

안드로이드 13부터 상태표시줄에 알림이 고정이 안되는 문제점이 발생했다.

사용자가 상태표시줄에서 알림을 좌 또는 우로 밀어도 절대 사라지지않도록

“notification.flags |= Notification.FLAG_NO_CLEAR; ” 알림 플러그 관련 코드를 추가하였다.

Android 12 이하 기기에서는 ‘ 모두 지우기 ‘ 버튼이나 스와이프를 사용해도 알림이 삭제되지 않는다.

그러나 안드로이드 13에서는 먹히지 않는다.

그렇다고해서 포그라운드에서 실행중인 서비스가 죽는 것은 아니다.

사용자가 밀어서 제거해도 다시 살아난다. 그럼에도 불구하고 고정해달라는 사용자들이 있다.

안드로이드12 버전까지는 정상적으로 사용했던 코드는 다음과 같다.

            if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                int NOTIFICATION_ID = 21020;
                String CHANNEL_ID = "smart_bar";
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                        context.getResources().getString(R.string.content_txt_43),
                        NotificationManager.IMPORTANCE_NONE);//.IMPORTANCE_HIGH);//IMPORTANCE_LOW);

                ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

                Notification notification = new androidx.core.app.NotificationCompat.Builder(this, CHANNEL_ID)
                        //.setContentTitle("") //자동표기됨
                        .setAutoCancel(false)
                        //.setWhen(System.currentTimeMillis())
                        .setShowWhen(false)
                        //.setLargeIcon(null)
                        .setSmallIcon(getPngImage(level))
                        .setContentIntent(pendingIntent)
                        .setCustomContentView(remoteViews)
                        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())

                notification.flags |= Notification.FLAG_NO_CLEAR; // 지우기 버튼 눌렀을때 지워지지 않게

                startForeground(NOTIFICATION_ID, notification);
}



티라미수 버전에서 알림바 고정을 위한 해결방법은?

FLAG_NO_CLEAR 대신 FLAG_ONGOING_EVENT 플래그를 사용하여 해결할 수 있다.

안드로이드 13버전 부터만 아래 코드 스니펫 처럼 플래그값을 변경해주면된다.

notification.flags |= Notification.FLAG_ONGOING_EVENT; 
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
                int NOTIFICATION_ID = 21020;
                String CHANNEL_ID = "smart_bar";
                NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                        context.getResources().getString(R.string.content_txt_43),
                        NotificationManager.IMPORTANCE_NONE);//.IMPORTANCE_HIGH);//IMPORTANCE_LOW);

                ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

                Notification notification = new androidx.core.app.NotificationCompat.Builder(this, CHANNEL_ID)
                        //.setContentTitle("") //자동표기됨
                        .setAutoCancel(false)
                        //.setWhen(System.currentTimeMillis())
                        .setShowWhen(false)
                        //.setLargeIcon(null)
                        .setSmallIcon(getPngImage(level))
                        .setContentIntent(pendingIntent)
                        .setCustomContentView(remoteViews)
                        .setStyle(new NotificationCompat.DecoratedCustomViewStyle())

                notification.flags |= Notification.FLAG_ONGOING_EVENT; // 지우기 버튼 눌렀을때 지워지지 않게

                startForeground(NOTIFICATION_ID, notification);
}

[연관자료]

[Android 13] 알림 권한 허용 구현방법(POST_NOTIFICATIONS, areNotificationsEnabled(), 알림 설정창 호출 방법)

[Android] 알림(Notification) 클릭이 먹히지않을 때 해결방법: PendingIntent , setFullScreenIntent



[REFERENCE]

FLAG_NO_CLEAR is ignored on Android 13



Leave a Reply

error: Content is protected !!