android - 不同的通知声音在奥利奥中不起作用
问题描述
我仅在奥利奥版本中面临一些与通知相关的问题。按照他的建议,我按照此链接在卸载/安装应用程序后成功获得了自定义声音。
现在的问题是我想在我的应用程序中使用两个自定义声音,为此,我有如下代码:
private void sendNotification(NotificationBean notificationBean) {
String textTitle = notificationBean.getTitle();
String alert = notificationBean.getMessage().getAlert();
int orderId = notificationBean.getMessage().getOrderId();
String notificationType = notificationBean.getMessage().getNotificationType();
String sound = notificationBean.getMessage().getSound();
Intent intent = new Intent(this, NavigationDrawerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri;
if (notificationType.equals("Pending"))
soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.sound);
else
soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, getString(R.string.app_name))
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(textTitle)
.setContentText(alert)
.setSound(soundUri)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.app_name);
String description = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(getString(R.string.app_name), name, importance);
channel.setDescription(description);
AudioAttributes attributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.enableLights(true);
channel.enableVibration(true);
channel.setSound(soundUri, attributes);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(101, mBuilder.build());
}
如果我得到notificationType = "Pending"
了,那么我想使用自定义声音,否则DEFAULT
声音,但这里它正在播放第一次播放的声音(当我第一次收到通知时。)。
我只在 OREO 中遇到了这个问题。在所有其他设备中,它工作正常。
有什么帮助吗?您的帮助将不胜感激。
解决方案
问题:
似乎是通知通道问题。
解决方案:
您应该创建单独的频道,或者您应该删除自己的频道。
策略:
1)创建单独的通道:
如果您想保留多个通道以及应用程序的各种配置,您可以选择此策略。
要创建单独的频道,只需在创建时提供唯一的频道 ID。
IE:
NotificationChannel channel = new NotificationChannel(uniqueChannelId, name, importance);
2)删除您现有的频道并重新创建它:
如果您只想保留一个频道以及更新的应用程序配置,您可以选择此策略。
要删除您自己的频道并重新创建它,以下可能会正常工作:
NotificationManager mNotificationManager = getSystemService(NotificationManager.class);
NotificationChannel existingChannel = notificationManager.getNotificationChannel(channelId);
//it will delete existing channel if it exists
if (existingChannel != null) {
mNotificationManager.deleteNotificationChannel(notificationChannel);
}
//then your code to create channel
NotificationChannel channel = new NotificationChannel(channelId, name, importance);
推荐阅读
- javascript - 使用 projectPoints + solvePnP 的包装器构建 OpenCV.js
- ruby-on-rails - Rails 6:如何在演示者类中使用`button_to`?
- r - 在一个点(文本字符串)拆分多个相似的记事本文本文件,并使用 R 将特定部分合并到一个文件中
- xml - 为什么我修改后的 XML 没有保存修改后的值?
- c - gcc 和 clang 中的 C99 支持不一致
- javascript - 当我们从另一个视图返回时,如何在一个视图中重新加载数据表?
- angular - 如何在 Angular 项目的 Quill 编辑器中更新标题的字体大小值
- java - 使用 H2 和 Hibernate 的 Spring JPA 不能正确自动递增
- r - 如何选择性地执行函数并累积结果
- session - Docusign 会话在签名后过期