无法在频道上发布通知" null"目标Api是26

时间:2017-08-16 11:08:49

标签: android android-notifications android-8.0-oreo

显示两个日志

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

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

Showing this

8 个答案:

答案 0 :(得分:26)

  

当您定位到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());

答案 1 :(得分:14)

如果你的最低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 :(得分:4)

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

希望这有帮助

答案 3 :(得分: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);

答案 4 :(得分: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

答案 5 :(得分: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);
    }
}

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

答案 6 :(得分:0)

如果你得到这个错误,应该注意2个项目并且他们订购:

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

    通知需要设置者:

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

    请参阅github中的示例。

答案 7 :(得分:0)

String CHANNEL_ID = "my_channel";

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