Android O通知未显示
我正在尝试为Android O版本实现通知。 我读过关于通知管理器和频道的内容。所以Android O仍然不想复制通知。关于PostCreate方法中的主要活动,我写了这篇文章Android O通知未显示,android,notifications,android-8.0-oreo,Android,Notifications,Android 8.0 Oreo,我正在尝试为Android O版本实现通知。 我读过关于通知管理器和频道的内容。所以Android O仍然不想复制通知。关于PostCreate方法中的主要活动,我写了这篇文章 NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); String CHANNEL_ID = "my_channel_01"; Strin
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/通知:对于卷控制以外的操作,不推荐使用流类型
W/通知:请参阅setSound()的文档,了解如何使用android.media.AudioAttributes来限定播放用例
如何处理这种错误情况
Upd。经过一些调查,我发现26 api在notification builder中使用了一些更改。现在它也接受香奈儿。因此,对于26,请使用具有两个参数的生成器。您的alarmSound可能应该是其他内容,而不是URI?您不应在notification builder上使用setSound方法,而应使用您创建的通知频道来设置这些设置
notificationChannel.setSound(Uri sound, AudioAttributes audioAttributes);
在创建notification/notificationcompact对象时传递通道id
Notification.Builder(Context context, String channelId)
或
我也收到了警告,但我注意到我仍然收到了通知(拉下了托盘),只是它是无声的,没有在系统栏中显示图标
我看到你的重要性被设置为低:
NotificationManager.importance\u low
对于一个不会播放声音的频道(我的也是),你需要将它设置为importance\u HIGH
或importance\u DEFAULT
才能真正看到和听到你的通知。问题在于新的安卓O通知。即使没有setSound,它也不会出现,日志中的警告保持不变。可能重复我这样做,但会触发Toast消息,日志会继续告诉我:“W/Notification:对于卷控制以外的操作,不推荐使用流类型W/Notification:请参阅setSound()的文档关于如何使用android.media.AudioAttributes来限定您的播放用例,“我尝试过这样做,但当通知到达时,默认声音仍然出现。有什么建议可能会遗漏什么
NotificationCompat.Builder(Context context, String channelId)