Java 未能在通道“null”上发布通知目标Api为27

Java 未能在通道“null”上发布通知目标Api为27,java,android,android-studio,Java,Android,Android Studio,将我的应用程序目标api升级到27后,通知不再工作,我在android studio emulator上收到一条toast消息: 无法在通道“null”上发布通知 这是我的通知代码: private void sendNotification(Context context, String sysModel) { Intent intentAction = new Intent(context, MainActivity.class); intentAction.putExt

将我的应用程序目标api升级到27后,通知不再工作,我在android studio emulator上收到一条toast消息:

无法在通道“null”上发布通知

这是我的通知代码:

 private void sendNotification(Context context, String sysModel) {
    Intent intentAction = new Intent(context, MainActivity.class);

    intentAction.putExtra("sysModel", sysModel);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intentAction, PendingIntent.FLAG_ONE_SHOT);

    notificationBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.changer_ico)
            .setContentTitle(context.getString(R.string.service_ready))
            .setContentIntent(pendingIntent)
            .addAction(R.drawable.ic_key_black_24dp, context.getString(R.string.turn_on), pendingIntent)
            .setAutoCancel(true);

    Notification notification = notificationBuilder.build();

    notificationManager.notify(1903, notification);

}

在android O中,您应发送如下通知:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.drawable.changer_ico)
                .setContentTitle(context.getString(R.string.service_ready))
                .setContentIntent(pendingIntent)
                .addAction(R.drawable.ic_key_black_24dp, context.getString(R.string.turn_on), pendingIntent)
                .setAutoCancel(true);

        Notification notification = notificationBuilder.build();
        NotificationChannel channel = new NotificationChannel("1903",
                context.getString(R.string.user_notify), NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
        notificationManager.notify(1903, notification);

你能测试下面的代码吗

注意,新的NotificationCompat.Buildercontext,通知通道;而不是新的通知compat.Buildercontext


您是否检查了通知的权限,顺便说一句,您需要从设备上卸载apk并重新安装。我当前的代码片段已发布在我的问题上,但请注意,我正在bootreciver.java类上发送此通知,如果这会引起任何尊重的话。。我是androidAs的新手,我说你需要在Android O中为你的通知创建一个通知通道,我在你的代码片段中没有看到。是的,我卸载了我的应用程序,甚至清理并重置了我的模拟器,仍然没有幸运的事,谢谢你,只是添加了一个;在以下行上:notificationBuilder.setSmallIconR.drawable.changer_ico;notificationBuilder.setContentTitlecontext.getStringR.string.service_就绪;notificationBuilder.SetContentIntentPendingContent;notificationBuilder.addActionR.drawable.ic_key_black_24dp,context.getStringR.string.turn_on,pendingContent;
private final static String NOTIFICATION_CHANNEL = "channel_name";
private final static String CHANNEL_DESCRIPTION = "channel_description";
public final static int NOTIFICATION_ID = 1903;

private void sendNotification(Context context, String sysModel) {

    // Intent
    Intent intentAction = new Intent(context, MainActivity.class);
    intentAction.putExtra("sysModel", sysModel);

    // Pending Intent
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intentAction, PendingIntent.FLAG_ONE_SHOT);

    // Notification Manager
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);

    // Create the notification channel if android >= 8
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, CHANNEL_DESCRIPTION, NotificationManager.IMPORTANCE_DEFAULT);
        notificationManager.createNotificationChannel(channel);
    }

    // Notification builder.. Note that I added the notification channel on this constructor
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL);
    notificationBuilder.setSmallIcon(R.drawable.changer_ico)
    notificationBuilder.setContentTitle(context.getString(R.string.service_ready))
    notificationBuilder.setContentIntent(pendingIntent)
    notificationBuilder.addAction(R.drawable.ic_key_black_24dp, context.getString(R.string.turn_on), pendingIntent)
    notificationBuilder.setAutoCancel(true);

    // Notify
    notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
}