奥利奥系统中不起作用的不同通知声音

5

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

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

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

如果我接收到的通知类型是“Pending”,那么我想要使用自定义声音,否则使用默认声音,但这里播放的声音是第一次收到通知时播放的声音。
在OREO系统中出现了这个问题,在其他设备上都正常工作。
有什么帮助吗?您的帮助将不胜感激。
2个回答

9

问题:
看起来是通知渠道的问题。

解决方案:
要么你应该创建一个单独的渠道,要么你应该删除自己的渠道。

策略:
1) 创建单独的渠道:
如果您想为应用程序持久化多个渠道以及各种配置,则可以选择此策略。
只需在创建时提供唯一的渠道ID即可创建单独的渠道。
例如:

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

谢谢Mehul,我从你的回答中得到了提示。我已经为不同的声音创建了两个通道ID,它们完美地工作着。 - Pratik Butani
1
@PratikButani,我很高兴能够帮忙。我添加了一些更多的细节,这可以根据需求来决定策略。 - Mehul Joisar
3
如果在删除频道后重新创建它,系统将采用该频道 ID 的预先保存配置,即使您在重新创建频道时设置了新的声音文件,它仍将采用以前附加到该频道的声音文件。我是否漏掉了任何配置设置步骤? - Tulika
你是对的,@Tulika系统会保留先前保存的通道的配置。因此,如果您想更改通知声音,则必须使用不同的通道ID重新创建通知通道。请参阅此博客以获取更多信息:https://rathorerahul586.medium.com/android-change-notification-sound-93eb4fd4ece5 - Rahul Rathore

5
我从 @Mehul Joisar 的回答中得到了解决问题的提示。
正如他所写:
要么你应该创建单独的通道,要么你应该删除自己的通道。
我为不同的声音创建了两个单独的通道。
据我所知,一旦我们创建了通道,我们就不能更改通知通道设置。我们必须删除并创建新的或者我们必须为不同的设置创建单独的通道。
在这里我分享完整的代码来帮助其他人。
private void sendNotification(NotificationBean notificationBean) {
    String textTitle = notificationBean.getTitle();
    String alert = notificationBean.getMessage().getAlert();
    int orderId = notificationBean.getMessage().getOrderId();
    String notificationType = notificationBean.getMessage().getNotificationType();

    Intent intent = new Intent(this, NavigationDrawerActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Uri soundUri;
    String channelName;

    if (notificationType.equals("Pending")) {
        channelName = getString(R.string.str_chef);
        soundUri = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.sound);
    }
    else {
        channelName = getString(R.string.str_customer);
        soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, channelName)
            .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(channelName, 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());
}

注意:必须先卸载您的应用程序,然后再使用此代码进行测试。

谢谢。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接