我正在使用AlarmManager,当闹钟启动时,它也会显示通知。我已经为奥利奥和奥利奥之前创建了通知。在奥利奥正常工作之前的通知 - 我可以禁用声音和设置灯光,但我不能在奥利奥做这个工作。我有类似的振动问题,但能够找到一个有效的解决方案。我从NotificationCompat尝试过很多事情(1,2,3,4,5,6 ......) ,改变重要性,但无法使其发挥作用。
我的问题只是奥利奥通知,我无法禁用声音(每次都是这样),我不能让灯闪烁。我经历了一堆SO问题和官方文档。有些解决方案已过时,已弃用(NotificationCompat.Builder),其他解决方案根本不起作用(包括官方文档中的一些示例)。
以下是Oreo (不工作)和旧版(工作)的代码:
//region Oreo notifications
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "AlarmNotification";
String description = "Alarm notification";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel mChannel = new NotificationChannel(channelIdOreo, name, importance);
mChannel.setDescription(description);
mChannel.setShowBadge(true);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (sNotifications.equals("false")) {
//NOT WORKING
mChannel.setSound(null, null);
}
//VIBRATION WORKING
if (sVibration.equals("true")) {
if (vibrator != null && vibrator.hasVibrator()) {
VibrationEffect effect = VibrationEffect.createOneShot(1000, VibrationEffect.DEFAULT_AMPLITUDE);
vibrator.vibrate(effect);
}
}
Notification notification = new Notification.Builder(context, channelIdOreo)
.setContentTitle(contentTitleText)
.setContentText(contentContentText)
.setNumber(1)
.setSmallIcon(whiteLogo)
.setBadgeIconType(whiteLogo)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
if (notificationManager != null) {
notificationManager.notify(notificationCode, notification);
}
}
//endregion
//region Pre-Oreo notifications
else {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(whiteLogo)
.setLargeIcon(largeIcon)
.setContentTitle(contentTitleText)
.setContentText(contentContentText)
.setOngoing(false)
.setNumber(1)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true);
mBuilder.setLights(colorPPDOrange, 1000, 2000);
if (sNotifications.equals("true")) {
mBuilder.setSound(uri);
}
if (sVibration.equals("true")) {
mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
}
mBuilder.setContentIntent(pendingIntent);
if (notificationManager != null) {
notificationManager.notify(notificationCode, mBuilder.build());
}
}
//endregion
答案 0 :(得分:0)
最后找到了答案。每次更改与频道相关的任何内容时,都必须将频道ID更改为全新的ID,这是从未使用过的。 不能仅与当前ID不同。除此之外,我用代码中的实际字符串替换了字符串资源。另外,我还必须移动几行代码,如下所示。
这是我的代码现在的样子:
String channelIdOreo = "FfffffF";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//region Oreo otification
Notification notification = new Notification.Builder(context, channelIdOreo)
.setContentTitle(contentTitleText)
.setContentText(contentContentText)
.setNumber(1)
.setSmallIcon(whiteLogo)
.setBadgeIconType(whiteLogo)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
//endregion
NotificationChannel mChannel = new NotificationChannel(channelIdOreo, "Channel human readable title and stuff", NotificationManager.IMPORTANCE_DEFAULT);
mChannel.enableLights(true);
mChannel.setLightColor(Color.YELLOW);
//region Conditions from settings
if (sNotifications.equals("true")) {
mChannel.setSound(uri, null);
} else {
mChannel.setSound(null, null);
}
if (sVibration.equals("true")) {
mChannel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
} else {
mChannel.enableVibration(false);
}
//endregion
if (notificationManager != null) {
notificationManager.createNotificationChannel(mChannel);
}
if (notificationManager != null) {
notificationManager.notify(notificationCode, notification);
}
}
希望这可以节省一些时间。