安卓oreo上的通知-动态启用声音/振动



我有一个前台服务,它显示一个进度通知,如果完成了,该通知将被重新用作普通通知,以显示服务的结果

我允许用户在我的应用程序中定义最终通知是否为静音。所以我要做的是:

// 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用户告诉我,每次更新时通知都会发出声音/振动
  • 同一位用户告诉我,重新启动后,声音/振动消失了

我想要什么

  • 我希望通知频道在默认情况下振动并播放声音(如果用户没有在我的频道的android设置中更改此设置)
  • 我想动态地决定,即使启用了声音/振动,我也不播放声音/振动
  • 如果用户禁用我的频道的声音/振动,我很好,但我不会在任何时候播放声音或振动

我该如何做到这一点?

编辑-一个解决方案如下:

使用两个通道:

  • 一个用于更新通知
  • 一个用于最终结果通知

这对我来说很奇怪,我认为我可以使用一个频道,默认情况下用声音设置它,然后使用这个频道。如果用户允许我播放声音(似乎我目前被迫播放声音),我应该能够定义自己是否播放声音。否则,我当然不会演奏任何声音。目前看来,每个服务都需要两个频道,显示通知的进度,并在完成后默认播放声音。这很难向用户解释,因为同一操作需要两个通道,一个用于进度,另一个用于最终结果。。。

我对通知的声音很恼火。我做到了。

.setSound(Uri.parse("android.resources://" + getPackageName() + "/" + R.raw.silence))

和这个

.setOnlyAlertOnce(true)

还要注意,你也必须在频道中设置它们。

最新更新