【发布时间】:2017-09-01 14:16:42
【问题描述】:
我正在尝试为 Android O 版本实现通知。 我已阅读有关通知管理器和频道的信息。所以 Android O 仍然不想重现通知。在 PostCreate 方法的主要 Activity 上我写了这个。
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:不推荐使用流类型进行音量控制以外的操作 W/Notification:请参阅 setSound() 的文档,了解如何使用 android.media.AudioAttributes 来限定您的播放用例
如何处理这种错误情况?
更新。经过一番调查,我发现 26 api 在通知生成器中使用了一些变化。现在它也接受chanelid。所以对于 26 使用带有两个参数的构建器。
【问题讨论】:
标签: android notifications android-8.0-oreo