未能在频道 "null" 上 post 通知目标 Api 是 26

Failed to post notification on channel "null" Target Api is 26

两条日志显示

1:对于音量控制以外的操作,不推荐使用流类型

2:请参阅 setSound() 的文档以了解使用什么来代替 android.media.AudioAttributes 来限定您的播放用例

您可以通过两种方式解决此问题,但对于这两种方式,您都需要创建一个具有特定频道 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);

希望对您有所帮助

首先创建通知渠道:

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);

如果您的最低 API 是奥利奥,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());

如果出现此错误,应注意 2 项并按顺序排列:

  1. NotificationChannel mChannel = new NotificationChannel(id, name, importance);
  2. builder = new NotificationCompat.Builder(this, id);

另外 NotificationManager notifManager 和 NotificationChannel mChannel 只创建一次。

通知需要设置器:

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

参见 中的示例。

来自developer documentation

When you target Android 8.0 (API level 26), you must implement one or more notification channels to display notifications to your users.

int NOTIFICATION_ID = 234;
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
    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.

此问题与旧的 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

String CHANNEL_ID = "my_channel";

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID);

我有一个类似的问题,但是启动一个服务作为后台 activity,在服务中这个代码有效:

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

这不是最好的解决方案,只是在后台启动服务它确实有效