android .app.RemoteServiceException: плохое уведомление для startForeground - PullRequest
3 голосов
/ 04 марта 2020

Я пытаюсь запустить службу переднего плана следующим образом:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    NotificationChannel chan = new NotificationChannel(
            getApplicationContext().getPackageName(),
            "My Foreground Service",
            NotificationManager.IMPORTANCE_LOW);
    chan.setLightColor(Color.BLUE);
    chan.setLockscreenVisibility(Notification.VISIBILITY_SECRET);

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(chan);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( 
            this, "MyChannelId");
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.mipmap.my_icon)
            .setContentTitle("App is running on foreground")
            .setPriority(NotificationManager.IMPORTANCE_LOW)
            .setCategory(Notification.CATEGORY_SERVICE)
            .setChannelId("MyChannelId")
            .build();

    startForeground(1, notification);
}

Это решение работает для эмулятора Android 8.0. Но я получил ошибку ниже на Android 8.1, 9.0 и 10.0 Эмуляторе.

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=MyChannelId pri=2 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 category=service vis=PRIVATE)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1945)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:214)
    at android.app.ActivityThread.main(ActivityThread.java:7356)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

PS: Я пробовал на устройстве без эмулятора Android 9.0 и не получил эту ошибку.

1 Ответ

1 голос
/ 04 марта 2020

Проблема возникает из-за того, что вы создаете канал с ID, но отправляете уведомление в другом ID:

// Here, you are using the package name as channel ID
NotificationChannel chan = new NotificationChannel(
              getApplicationContext().getPackageName(), 
              "My Foreground Service",
              NotificationManager.IMPORTANCE_LOW);

// But here, you are sending notifications to "MyChannelId"
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( 
              this, "MyChannelId");

Итак, я считаю, что вам просто нужно изменить идентификатор канала на MyChannelID следующим образом:

NotificationChannel chan = new NotificationChannel(
              "MyChannelId",
              "My Foreground Service",
              NotificationManager.IMPORTANCE_LOW);
...