【问题标题】:Invalid channel for service notification error even after creating custom channel即使在创建自定义频道后,服务通知错误的无效频道也是如此
【发布时间】:2019-04-06 11:30:38
【问题描述】:

我已经实现了一个startForeground() 方法来启动在onCreate() 中调用的持续通知。在两个不同的类中有两种用法。我正在测试 API 27 (Android 8.1)

第一次使用(PictureCapturingService.java):

onCreate()

@Override
public void onCreate() {
    super.onCreate();
    Context mContext = this.getApplicationContext();
    context = this.getApplicationContext();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startMyOwnForeground();
    } else {
        startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Capturing Image").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
    }

startMyOwnForeground()

@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground() {
    String channelID = "com.example.code";
    NotificationChannel notificationChannel = new NotificationChannel(channelID, "Background Service", NotificationManager.IMPORTANCE_NONE);
    notificationChannel.enableLights(false);
    notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(notificationChannel);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.drawable.icon_notif)
            .setContentTitle("Capturing Image")
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();

    startForeground(1234, notification);
}

第二次使用(VideoRecordService.java):

onCreate()startRecord() 调用

onCreate()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startMyOwnForeground("Sending Message");
    }
    else {
        startForeground(1234, new Builder(this).setContentTitle("Sending Message").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
    }

startRecord()

private void startRecord() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startMyOwnForeground("Video Recording");
    } else {
        startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Video Recording").setContentText("").setSmallIcon(R.drawable.icon_notif).build());
    }

startMyOwnForeground

@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(String str) {
    String channelID = "com.example.code";
    NotificationChannel notificationChannel = new NotificationChannel(channelID, "Background Service", NotificationManager.IMPORTANCE_NONE);
    notificationChannel.enableLights(false);
    notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    assert manager != null;
    manager.createNotificationChannel(notificationChannel);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelID);
    Notification notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.drawable.icon_notif)
            .setContentTitle(str)
            .setPriority(NotificationManager.IMPORTANCE_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();

    startForeground(1234, notification);
}

我收到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.raudram.barathikannamma, PID: 18871
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 vis=PRIVATE)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1797)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6651)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)

【问题讨论】:

  • 基于channel=null,似乎存在第三条路径,您正在尝试使用没有通道的Notification 启动前台服务。
  • 您在哪个 Android 版本中遇到上述错误?恐怕问题出在startForeground(1234, new Builder(getApplicationContext()).setContentTitle("Video Recording").setContentText("").setSmallIcon(R.mipmap.ic_launcher).build()); 而不是startMyOwnForeground 方法上。
  • @madlymad 我目前正在 API 27 上进行测试
  • @CommonsWare 先生,我整个项目中只有startForeground方法中的five usages,这里全部提到了5个。错误可能出在针对 API
  • 我正在使用以下逻辑来创建频道和通知github.com/madlymad/uptime/blob/master/app/src/main/java/com/… 并且从未遇到过问题...我注意到 2 之间的唯一区别是频道命名系统可能不喜欢“点” "

标签: java android service android-notifications background-service


【解决方案1】:

您的图标设置为R.mipmap.ic_launcher。顾名思义,这表明您正在尝试将应用启动器图标用作通知图标。不过,这不是一个有效的通知图标。

使用 Android Studio 中的图像资产向导创建一个专用的通知图标 - 您将在向导中为“图标类型”找到一个专用的“通知图标”类别。然后,切换到使用该资源,而不是 R.mipmap.ic_launcher,看看是否有更好的结果。

【讨论】:

    【解决方案2】:

    听着,你创建通知通道的类必须继承Application类enter image description here

    enter image description here

    【讨论】:

    • 用代码作为图片发布答案意味着搜索引擎无法访问它,并且没有人可以剪切和粘贴它。您应该考虑在您的问题上单击 edit 并将代码添加为格式正确的文本。
    • 问题已经解决,问题的cmets中提到了答案。之所以不是单独的答案是因为代码是正确的,问题是由于gradle文件不匹配造成的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-08
    • 2021-08-08
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 2018-06-20
    相关资源
    最近更新 更多