Android O 通知未显示
Android O notification not showing
我正在尝试为 Android O 版本实现通知。
我已经阅读了有关通知管理器和渠道的信息。所以AndroidO还是不想重现通知。在主要 Activity 的 PostCreate 方法中,我写了这个。
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
String CHANNEL_NAME = "my Channel Name";
int NOTIFICATION_ID = 1;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationChannel notificationChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mNotifyManager.createNotificationChannel(notificationChannel);
}
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification myNotification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("You have been notify")
.setContentText("This is your Notifiaction Text")
.setSmallIcon(R.drawable.ic_donut_large_black_24dp)
.setChannel(CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setContentIntent(notificationPendingIntent)
.setAutoCancel(true)
.setSound(alarmSound)
.build();
mNotifyManager.notify(NOTIFICATION_ID, myNotification);
Toast.makeText(this, "accepted", Toast.LENGTH_SHORT).show();
在第 26 次构建后 API 它不会创建通知,触发 Toast 消息,并且日志说我:
W/Notification: Use of stream types is deprecated for operations other than volume control
W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
如何处理这种错误情况?
更新。经过一些调查,我发现 26 api 在通知生成器中使用了一些小改动。现在它也接受香奈儿。所以对于 26,使用带有两个参数的生成器。
您的 alarmSound 可能应该是其他东西,而不是 URI?
您不应在通知生成器上使用 setSound 方法,而应使用您创建的通知渠道来设置这些设置。
notificationChannel.setSound(Uri sound, AudioAttributes audioAttributes);
创建 notification/notificationcompact 对象时传递频道 ID。
Notification.Builder(Context context, String channelId)
或
NotificationCompat.Builder(Context context, String channelId)
我也收到了警告,但我注意到我实际上仍然收到通知(拉下托盘),只是它是无声的并且没有在系统栏中显示图标...
我看到你的重要性设置为低:NotificationManager.IMPORTANCE_LOW
对于一个频道(我的也是),它不会播放声音,你需要将它设置为 IMPORTANCE_HIGH
或 IMPORTANCE_DEFAULT
实际看到和听到您的通知。
我正在尝试为 Android O 版本实现通知。 我已经阅读了有关通知管理器和渠道的信息。所以AndroidO还是不想重现通知。在主要 Activity 的 PostCreate 方法中,我写了这个。
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID = "my_channel_01";
String CHANNEL_NAME = "my Channel Name";
int NOTIFICATION_ID = 1;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationChannel notificationChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
notificationChannel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
mNotifyManager.createNotificationChannel(notificationChannel);
}
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification myNotification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("You have been notify")
.setContentText("This is your Notifiaction Text")
.setSmallIcon(R.drawable.ic_donut_large_black_24dp)
.setChannel(CHANNEL_ID)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setContentIntent(notificationPendingIntent)
.setAutoCancel(true)
.setSound(alarmSound)
.build();
mNotifyManager.notify(NOTIFICATION_ID, myNotification);
Toast.makeText(this, "accepted", Toast.LENGTH_SHORT).show();
在第 26 次构建后 API 它不会创建通知,触发 Toast 消息,并且日志说我:
W/Notification: Use of stream types is deprecated for operations other than volume control W/Notification: See the documentation of setSound() for what to use instead with android.media.AudioAttributes to qualify your playback use case
如何处理这种错误情况?
更新。经过一些调查,我发现 26 api 在通知生成器中使用了一些小改动。现在它也接受香奈儿。所以对于 26,使用带有两个参数的生成器。
您的 alarmSound 可能应该是其他东西,而不是 URI?
您不应在通知生成器上使用 setSound 方法,而应使用您创建的通知渠道来设置这些设置。
notificationChannel.setSound(Uri sound, AudioAttributes audioAttributes);
创建 notification/notificationcompact 对象时传递频道 ID。
Notification.Builder(Context context, String channelId)
或
NotificationCompat.Builder(Context context, String channelId)
我也收到了警告,但我注意到我实际上仍然收到通知(拉下托盘),只是它是无声的并且没有在系统栏中显示图标...
我看到你的重要性设置为低:NotificationManager.IMPORTANCE_LOW
对于一个频道(我的也是),它不会播放声音,你需要将它设置为 IMPORTANCE_HIGH
或 IMPORTANCE_DEFAULT
实际看到和听到您的通知。