无法在通道“ null”上发布通知,目标Api为26

问题描述 投票:37回答:8

显示两个日志

1:不建议将流类型用于除音量控制之外的其他操作

2:有关如何将android.media.AudioAttributes用于限定播放用例的信息,请参见setSound()的文档

Showing this

android android-notifications android-8.0-oreo
8个回答
32
投票
developer documentation

当您以Android 8.0(API级别26)为目标时,必须实现一个或多个通知通道才能向用户显示通知。

int NOTIFICATION_ID = 234; NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { String CHANNEL_ID = "my_channel_01"; CharSequence name = "my_channel"; String Description = "This is my channel"; int importance = NotificationManager.IMPORTANCE_HIGH; NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); mChannel.setDescription(Description); mChannel.enableLights(true); mChannel.setLightColor(Color.RED); mChannel.enableVibration(true); mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); mChannel.setShowBadge(false); notificationManager.createNotificationChannel(mChannel); } NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx, CHANNEL_ID) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(title) .setContentText(message); Intent resultIntent = new Intent(ctx, MainActivity.class); TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx); stackBuilder.addParentStack(MainActivity.class); stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(resultPendingIntent); notificationManager.notify(NOTIFICATION_ID, builder.build());

要注意的一件有趣的事情:如果已经存在一个通道,则调用createNotificationChannel无效,因此可以create all channels whenever you start your application

15
投票
如果您的最低API是Oreo,Gulzar Bhat的答案将非常有效。但是,如果最低要求较低,则必须将NotificationChannel代码包装在平台级别检查中。之后,您仍然可以使用id,在Oreo之前它将被忽略:

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(true); notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); notificationManager.createNotificationChannel(notificationChannel); } NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify((int)(System.currentTimeMillis()/1000), mBuilder.build());


6
投票
您可以通过两种方式解决此问题,但是对于这两种方式,您都需要创建一个具有特定渠道ID的通知渠道。

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String id = "my_channel_01"; int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel mChannel = new NotificationChannel(id, name,importance); mChannel.enableLights(true); mNotificationManager.createNotificationChannel(mChannel);

第一种方法是在构造函数中设置通知通道:

Notification notification = new Notification.Builder(MainActivity.this , id).setContentTitle("Title"); mNotificationManager.notify("your_notification_id", notification);

第二种方法是通过Notificiation.Builder.setChannelId()设置通道

Notification notification = new Notification.Builder(MainActivity.this).setContentTitle("Title"). setChannelId(id); mNotificationManager.notify("your_notification_id", notification);

希望这会有所帮助

4
投票
首先创建通知渠道:

public static final String NOTIFICATION_CHANNEL_ID = "4655"; //Notification Channel CharSequence channelName = NOTIFICATION_CHANNEL_NAME; int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, NOTIFICATION_CHANNEL_NAME, importance); notificationChannel.enableLights(true); notificationChannel.setLightColor(Color.RED); notificationChannel.enableVibration(true); notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}); NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.createNotificationChannel(notificationChannel);

然后在构造函数中使用频道ID:

final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) .setDefaults(Notification.DEFAULT_ALL) .setSmallIcon(R.drawable.ic_timers) .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400}) .setSound(null) .setContent(contentView) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setLargeIcon(picture) .setTicker(sTimer) .setContentIntent(timerListIntent) .setAutoCancel(false);


3
投票
此问题与旧的FCM版本有关。

将您的依赖关系更新为com.google.firebase:firebase-messaging:15.0.2或更高。

这将纠正错误

failed to post notification on channel null

当您的应用在后台接收通知时,因为现在Firebase提供了具有基本设置的默认通知通道。

但是您也可以在清单中为FCM指定默认的通知通道。

<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>

要了解更多here

1
投票
我有一个类似的问题,但是将服务作为后台活动启动,该代码在服务中起作用:

public static final int PRIMARY_FOREGROUND_NOTIF_SERVICE_ID = 1001; @Override public void onCreate() { super.onCreate(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { String id = "_channel_01"; int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel mChannel = new NotificationChannel(id, "notification", importance); mChannel.enableLights(true); Notification notification = new Notification.Builder(getApplicationContext(), id) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("My chat") .setContentText("Listening for incoming messages") .build(); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (mNotificationManager != null) { mNotificationManager.createNotificationChannel(mChannel); mNotificationManager.notify(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification); } startForeground(PRIMARY_FOREGROUND_NOTIF_SERVICE_ID, notification); } }

这不是最好的解决方案,而只是在后台启动服务确实起作用

0
投票
如果出现此错误,请注意2个项目,它们会顺序排列:

    NotificationChannel mChannel = new NotificationChannel(id, name, importance);
  1. builder = new NotificationCompat.Builder(this, id);
  • 也仅创建一次NotificationManager notifManager和NotificationChannel mChannel。

    通知有必填项:

    builder.setContentTitle() // required .setSmallIcon() // required .setContentText() // required

    请参见On Android 8.1 API 27 notification does not display中的示例。

  • 0
    投票
    String CHANNEL_ID = "my_channel"; NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID);
    © www.soinside.com 2019 - 2024. All rights reserved.