gpt4 book ai didi

android - 不同的通知声音在奥利奥中不起作用

转载 作者:行者123 更新时间:2023-12-02 13:32:39 24 4
gpt4 key购买 nike

我仅在 Oreo 版本中遇到一些与通知相关的问题。我关注this link并按照他的建议卸载/安装应用程序后成功获得自定义声音。

现在的问题是我想在我的应用程序中使用两个自定义声音,为此,我有如下代码:

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 声音,但这里它正在播放第一次播放的声音(当我第一次收到通知时。)。

我只在奥利奥中遇到这个问题。在所有其他设备中它工作正常。

有什么帮助吗?我们将不胜感激您的帮助。

最佳答案

问题:
看来通知 channel 有问题。

解决方案:
您要么应该创建单独的 channel ,要么应该删除自己的 channel 。

策略:
1) 创建单独的 channel :
如果您希望为您的应用保留多个 channel 以及各种配置,您可以选择此策略。
要创建单独的 channel ,只需在创建时提供唯一的 channel ID 即可。
即:

NotificationChannel channel = new NotificationChannel(uniqueChannelId, name, importance);

2) 删除现有 channel 并重新创建:
如果您希望为您的应用保留仅一个 channel 以及更新配置,您可以选择此策略。

要删除您自己的 channel 并重新创建它,以下操作可能会正常工作:

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);

关于android - 不同的通知声音在奥利奥中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53913251/

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