我有一个显示进度通知的前台服务,如果完成,该通知将作为普通通知重用以显示服务的结果
我允许用户在我的应用程序中定义最终通知是否静音。所以这就是我所做的:
// 1) Create notifcation channel in my app, once only
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance);
notificationChannel.enableLights(false);
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
// 2) Init notification
notificationBuilder = new NotificationCompat.Builder(this, notificationChannelId);
notificationBuilder.setAutoCancel(false);
notificationBuilder.setOngoing(true);
notificationBuilder.setTicker(ticker);
notificationBuilder.setContentText(title);
// 3) Updating the notification to show the services progress
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(subTitle);
// 4) preparing the final notification
if (!MainApp.getPrefs().silentNotifications()) {
if (globalError || updated > 0 || prepared > 0 || contactsWithError.size() > 0) {
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
notificationBuilder.setVibrate(new long[]{0, 500});
notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
} else {
notificationBuilder.setVibrate(new long[]{});
notificationBuilder.setSound(null);
}
}
问题
- Android oreo 用户告诉我,通知在每次更新时发出声音/振动
- 同一个用户告诉我,重启后,声音/振动消失了
我想要什么
- 我希望通知 channel 默认振动并播放声音(如果用户没有在我的 channel 的 android 设置中更改此设置)
- 我想动态决定我不播放声音/振动,即使启用了声音/振动也是如此
- 如果用户为我的 channel 禁用声音/振动,我很好,我不会在任何时候播放声音或振动
我该如何实现?
编辑 - 一种解决方案如下:
使用两个 channel :
- 一个用于更新通知
- 一个用于最终结果通知。
这对我来说很奇怪,我假设我可以使用一个 channel ,默认设置声音并使用这个 channel 。如果用户允许我播放声音(好像我目前被迫播放声音),我应该能够定义自己是否播放声音。否则我当然不会播放声音。看起来目前每个服务都需要两个 channel ,在通知中显示进度并在完成时默认播放声音。这很难向用户解释,因为您需要两个 channel 来执行相同的操作,一个用于进度,一个用于最终结果...
最佳答案
我对通知的声音感到恼火。我这样做了。
.setSound(Uri.parse("android.resources://" + getPackageName() + "/" + R.raw.silence))
还有这个
.setOnlyAlertOnce(true)
另请注意,您也必须在 channel 中设置它们。
关于android - android oreo 上的通知 - 动态启用声音/振动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47259456/