Android

Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification

targetSdkVersion26 에서 targetSdkVersion 28로 변경 후 빌드까지 성공하고 앱 설치까지 성공하였다.
하지만 앱을 실행한 결과 앱은 죽었다.

Fatal Exception: android.app.RemoteServiceException
Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 backgroundColor=0x00000000 vis=PRIVATE)
android.app.ActivityThread$H.handleMessage + 1745 (ActivityThread.java:1745)
android.os.Handler.dispatchMessage + 106 (Handler.java:106)
android.os.Looper.loop + 193 (Looper.java:193)
android.app.ActivityThread.main + 6853 (ActivityThread.java:6853)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 493 (RuntimeInit.java:493)
com.android.internal.os.ZygoteInit.main + 860 (ZygoteInit.java:860)

startForeground 수행 후 반드시 NotificationChannel를 생성해야한다.

startForeground(2,new Notification()); 로직을 사용하는 꼼수는 더 이상 통하지 않는다.

@Override
public void onCreate() {
  String CHANNEL_ID = "channel_1";
  NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
  "Android test",
  NotificationManager.IMPORTANCE_LOW);

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

  Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
  .setContentTitle("")
  .setContentText("").build();

  startForeground(2, notification);
}

 

Leave a Reply

error: Content is protected !!