其他通知声音在奥利奥(Oreo)中不起作用

时间:2018-12-24 12:08:19

标签: android android-notifications android-8.0-oreo

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

我仅在OREO中遇到此问题。在所有其他设备上,它都可以正常工作。

有帮助吗?您的帮助将不胜感激。

2 个答案:

答案 0 :(得分:4)

问题:
似乎是通知频道问题。

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

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

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

2)删除您现有的频道并重新创建:
如果您只想保留 仅一个频道 以及应用的 更新 配置,则可以选择此策略。< / p>

要删除自己的频道并重新创建频道,请执行以下操作:

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

答案 1 :(得分:2)

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

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

谢谢。