不同的通知声音在奥利奥中不起作用 [英] Different notification sound not working in Oreo

查看:23
本文介绍了不同的通知声音在奥利奥中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仅在 Oreo 版本中遇到一些通知相关问题.我按照此链接,按照他的建议在卸载/安装应用程序后成功获得自定义声音.

I am facing some Notification related issue in Oreo Version only. I follow this link and successfully got custom sound after uninstall/install the app as he has suggested.

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

Now problem is that I want to use two custom sound in my app, For that, I have code like:

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

If I get notificationType = "Pending" then I want to use custom sound, otherwise DEFAULT sound but Here It is playing that sound which is played first-time (When I receive notification first time.).

我只在 OREO 中遇到这个问题.在所有其他设备上都可以正常工作.

I am getting this problem in OREO only. In all other devices its working fine.

有什么帮助吗?您的帮助将不胜感激.

Any help? Your help would be appreciated.

推荐答案

问题:
似乎是通知渠道问题.

Problem:
It seems Notification Channel issue.

解决方案:
您应该创建单独的频道,或者您应该删除自己的频道.

Solution:
Either you should create separate channel, or you should delete your own channel.

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

Strategy:
1) Create separate channel:
You may select this strategy if you want to persist multiple channels along with various configuration for your app.
To create separate channel, just provide unique channel ID while creating it.
i.e.:

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

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

2) Delete your existing channel and re-create it:
You may select this strategy if you want to persist only one channel along with updated configuration for your app.

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

To delete your own channel and re-create it, following may work fine:

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

这篇关于不同的通知声音在奥利奥中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆