Java 创建自定义通道后服务通知错误的通道无效

Java 创建自定义通道后服务通知错误的通道无效,java,android,service,android-notifications,background-service,Java,Android,Service,Android Notifications,Background Service,我已经实现了一个startForeground()方法来启动在onCreate()中调用的正在进行的通知。在两个不同的类中有两种用法。我正在测试API 27(安卓8.1) 第一次使用(PictureCapturingService.java): onCreate(): @Override public void onCreate() { super.onCreate(); Context mContext = this.getApplicationContext(); c

我已经实现了一个
startForeground()
方法来启动在
onCreate()
中调用的正在进行的通知。在两个不同的类中有两种用法。我正在测试API 27(安卓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());
    }
@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);
}
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());
    }
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());
    }
@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);
}
startMyOwnForeground()

@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());
    }
@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);
}
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());
    }
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());
    }
@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);
}
第二种用法(VideoRecordService.java):

onCreate()
startRecord()调用

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());
    }
@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);
}
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());
    }
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());
    }
@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);
}
开始录制()

@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());
    }
@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);
}
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());
    }
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());
    }
@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);
}
开始主题音乐前景

@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());
    }
@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);
}
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());
    }
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());
    }
@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)

您的图标设置为
R.mipmap.ic\u启动器
。通过名称,这表明您正试图使用应用程序启动器图标作为通知图标。但这不是有效的通知图标


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

听着,您在其中创建通知通道的类必须继承应用程序类


根据
channel=null
,似乎有第三条路径,您正试图通过没有通道的
通知启动前台服务。您在哪个Android版本中遇到上述错误?恐怕问题出在
startForeground(1234,新构建器(getApplicationContext()).setContentTitle(“视频录制”).setContentText(“”).setMallicon(R.mipmap.ic_launcher).build())
而不是
startMyOwnForeground
方法。@madlymad我目前正在API上测试27@CommonsWare先生,在我的整个项目中,只有一种方法是
startForeground
方法,这里提到了这五种方法。该错误是否会出现在针对APII调用的默认方法中?我正在使用以下逻辑创建通道和通知,并且从未遇到问题。。。我注意到2之间的唯一区别是,命名系统的频道可能不喜欢“点”以图片形式发布带有代码的答案,这意味着搜索引擎无法访问该答案,并且没有人可以剪切和粘贴该答案。你应该考虑点击你的问题并添加代码以及格式化文本。问题已经解决,问题的评论中提到了答案。它不是一个单独的答案的原因是因为代码是正确的,这个问题是由gradle文件中的不匹配引起的。