代码之家  ›  专栏  ›  技术社区  ›  Pratik Butani Umesh N

不同的通知声音在Oreo中不工作

  •  1
  • Pratik Butani Umesh N  · 技术社区  · 6 年前

    我正面对一些 通知 相关问题 奥利奥 仅版本。我跟随 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 声音,但这里播放的是第一次播放的声音(当我第一次收到通知时)。

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

    有什么帮助吗?谢谢你的帮助。

    2 回复  |  直到 6 年前
        1
  •  3
  •   Mehul Joisar    6 年前

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

    解决方案:
    要么创建单独的频道,要么删除自己的频道。

    策略:
    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);
    
        2
  •  2
  •   Pratik Butani Umesh N    6 年前

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

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

    谢谢您。