Different notification sound not working in Oreo

前端 未结 2 1396
一整个雨季
一整个雨季 2020-12-06 21:10

I am facing some Notification related issue in Oreo Version only. I follow this link and successfully got custom sound after uninstall/inst

相关标签:
2条回答
  • 2020-12-06 22:11

    Problem:
    It seems Notification Channel issue.

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

    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) 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);
    
    0 讨论(0)
  • 2020-12-06 22:13

    I got hint to solve my problem from @Mehul Joisar's answer.

    As he wrote:

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

    I have created two separate channels for different sounds.

    As I think, we cant change Notification Channel settings after once we have created channel. We must have to remove and create new or else We have to create separate channels for different settings.

    Here I am sharing full code to help others.

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

    NOTE: Must uninstall your app first and then test with this code.

    Thank you.

    0 讨论(0)
提交回复
热议问题