gpt4 book ai didi

java - 无法启用奥利奥通知的闪烁灯并禁用通知声音

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:08 24 4
gpt4 key购买 nike

我正在使用 AlarmManager,当闹钟启动时它也会显示通知。我已经为奥利奥和奥利奥之前创建了通知。 Oreo 正常工作之前的通知 - 我可以禁用声音并设置灯光,但我无法在 Oreo 中执行此操作。我也遇到了类似的振动问题,但找到了可行的解决方案。我已经尝试了很多东西( 123456 ...),从NotificationCompat到改变重要性,但无法使其发挥作用。

我的问题仅在于 Oreo 通知,我无法禁用声音(它每次都会消失),并且我无法让灯光闪烁。我已经解决了很多问题和官方文档。有些解决方案已过时、已弃用 (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

最佳答案

终于找到答案了。每次我更改与我的 channel 相关的任何内容时,我都必须将我的 channel 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);
}
}

希望这能为某人节省一些时间。

关于java - 无法启用奥利奥通知的闪烁灯并禁用通知声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50227738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com