我正在尝试为Android O版本实现通知。我已经阅读了有关通知管理器和通道的相关内容。所以,Android O仍然不希望再次出现通知。在主要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();
在构建26th 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使用了通知构建器中的一些小变化。现在它也接受chanelid。因此,在26个API中使用具有两个参数的构建器。