无法在频道“null"上发布通知目标 API 为 26 [英] Failed to post notification on channel "null" Target Api is 26
问题描述
两个日志显示
1:对于音量控制以外的操作,不推荐使用流类型
1: Use of stream types is deprecated for operations other than volume control
2:请参阅 setSound() 的文档,了解如何与 android.media.AudioAttributes 一起使用以限定您的播放用例
2: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
推荐答案
来自开发者文档:
当您面向 Android 8.0(API 级别 26)时,您必须实施一个或多个通知渠道来向您的用户显示通知.
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);
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
是没有效果的,所以你可以在您启动应用程序时创建所有频道.
One interesting thing to note: If a channel already exists, calling createNotificationChannel
has no effect, so you can create all channels whenever you start your application.
这篇关于无法在频道“null"上发布通知目标 API 为 26的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!