android.app.RemoteServiceException:startForeground的错误通知 [英] android.app.RemoteServiceException: Bad notification for startForeground

查看:253
本文介绍了android.app.RemoteServiceException:startForeground的错误通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按以下方式启动前台服务:

I'm trying to start foreground service as follow:

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模拟器上遇到了以下错误.

This solution works for Android 8.0 Emulator. But I got the error below on Android 8.1, 9.0 and 10.0 Emulator.

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设备上尝试过,但没有收到此错误.

PS: I tried on a non-emulator Android 9.0 device and didn't get this error.

推荐答案

之所以发生此问题,是因为您正在创建具有 ID 的频道,但以其他 ID :

Issue is happening because you are creating a channel with an ID but sending the notification in a different 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");

因此,我相信您只需将Channel ID更改为 MyChannelID 即可,如下所示:

So, I believe you just need to change Channel ID to MyChannelID as follows:

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

这篇关于android.app.RemoteServiceException:startForeground的错误通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆