Android O报告通知没有发布到频道 – 但它是

几个Android O通知问题:

1)我创build了一个通知通道(见下面),用.setChannelId()调用构build器(传入我创build的通道名称“wakey”;然而,当我运行该应用程序时,我没有发布通知通道“空”。什么可能导致这个?

2)我怀疑#1的答案可以在它所说的“日志”中find,但我已经检查了logcat并没有看到有关通知或通道的任何信息。 它在哪里看到的日志?

以下是我用来创build频道的代码:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); CharSequence name = context.getString(R.string.app_name); String description = "yadda yadda" int importance = NotificationManager.IMPORTANCE_DEFAULT; NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL, name, importance); channel.setDescription(description); notificationManager.createNotificationChannel(channel); 

以下是生成通知的代码:

 Notification.Builder notificationBuilder; Intent notificationIntent = new Intent(context, BulbActivity.class); notificationIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND); // Fix for https://code.google.com/p/android/issues/detail?id=53313 PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); Intent serviceIntent = new Intent(context, RemoteViewToggleService.class); serviceIntent.putExtra(WakeyService.KEY_REQUEST_SOURCE, WakeyService.REQUEST_SOURCE_NOTIFICATION); PendingIntent actionPendingIntent = PendingIntent.getService(context, 0, serviceIntent, PendingIntent.FLAG_CANCEL_CURRENT); _toggleAction = new Notification.Action(R.drawable.ic_power_settings_new_black_24dp, context.getString(R.string.toggle_wakey), actionPendingIntent); notificationBuilder= new Notification.Builder(context) .setContentTitle(context.getString(R.string.app_name)) .setContentIntent(contentIntent) .addAction(_toggleAction); if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { notificationBuilder.setChannelId(NOTIFICATION_CHANNEL); } notificationBuilder.setSmallIcon(icon); notificationBuilder.setContentText(contentText); _toggleAction.title = actionText; int priority = getNotificationPriority(context); notificationBuilder.setPriority(priority); notificationBuilder.setOngoing(true); Notification notification = notificationBuilder.build(); notificationManager.notify(NOTIFICATION_ID, notification); 

这是我得到的警告: 在这里输入图像说明

我想我已经学会了几件事情,所有这些都加起来了答案:

  1. 我正在使用模拟器设备,图像不包含Play商店。
  2. 图像上的Google Play服务版本并不是最新版本,因此我应该收到一条通知,告诉我需要升级。 由于该通知没有被应用到频道,所以没有出现。
  3. 如果我在Android Studio中将logcat设置为“No Filters”而不是“Show only selected application”,那么我find了指出有问题的通知是Play Services“需要更新”通知的日志。

所以,我改成了包含Play商店的图片,并且正确显示了通知(也许该通知的频道是由Play商店设置的?),让我更新到最新的Google Play服务,我避难自那以后没有看到这个警告。

因此,如果您使用Google Play服务并在模拟器上进行testing,请select包含Play商店的图片,或者忽略敬酒(祝您好运!)。

我有同样的问题,并通过使用构造函数解决它

new Notification.Builder(Context context, String channelId) ,而不是在API水平> = 26(Android O)上弃用的 : new NotificationCompat.Builder(Context context)

如果您的notificationBuilder是使用不build议使用的构造函数构build的,下面的代码将不起作用:

 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { notificationBuilder.setChannelId(NOTIFICATION_CHANNEL);} 

使用以下代码创build通知:

  Notification notification = new Notification.Builder(MainActivity.this) .setContentTitle("New Message") .setContentText("You've received new messages.") .setSmallIcon(R.mipmap.ic_launcher) .setChannelId(channelId) .build(); 

不使用:

 Notification notification = new NotificationCompat.Builder(MainActivity.this) .setContentTitle("Some Message") .setContentText("You've received new messages!") .setSmallIcon(R.mipmap.ic_launcher) .setChannel(channelId) .build(); 

首先创build通知频道:

  public static final String NOTIFICATION_CHANNEL_ID = "4565"; //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) .setChannelId(NOTIFICATION_CHANNEL_ID) .setContent(contentView) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setLargeIcon(picture) .setTicker(sTimer) .setContentIntent(pendingIntent) .setAutoCancel(false);