1

I want to set a different notification sound to a notification depending on it's content. I have set a working sound if the title of the notification contains number "1", "2", or "3". However, the sound must be different if the title contains "acknowledge", but it's not working, the alarmsound is still the one ringing.

Uri defaultsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Uri alarmsound = Uri.parse("android.resource://" + getApplicationContext().getPackageName() + "/" + R.raw.siren); //ContentResolver.SCHEME_ANDROID_RESOURCE +

    String NOTIFICATION_CHANNEL_ID = "com.example.bantay.bantay";
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setPriority(Notification.PRIORITY_MAX);
    notificationBuilder.setDefaults(Notification.DEFAULT_ALL);
    notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    notificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
    notificationBuilder.setWhen(System.currentTimeMillis());
    notificationBuilder.setSmallIcon(R.drawable.marikinalogo);
    notificationBuilder.setContentTitle(title);
    notificationBuilder.setContentText(body);
    notificationBuilder.setVibrate(new long[]{500, 1000, 500, 1000, 500, 1000});

    if(title.toLowerCase().contains("1")){
        notificationBuilder.setContentIntent(pendingIntent);
        notificationBuilder.setSound(alarmsound);
    }
    else if(title.toLowerCase().contains("2")){
        notificationBuilder.setContentIntent(pendingIntent1);
        notificationBuilder.setSound(alarmsound);
    }
    else if(title.toLowerCase().contains("3")){
        notificationBuilder.setContentIntent(pendingIntent2);
        notificationBuilder.setSound(alarmsound);
    }
    else if(title.toLowerCase().contains("acknowledge")){
        notificationBuilder.setContentIntent(pendingIntent3);
        notificationBuilder.setSound(defaultsound);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);

        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();

        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{500, 1000, 500, 1000, 500, 1000});

        if(title.toLowerCase().contains("1")){
            notificationChannel.setSound(alarmsound, audioAttributes);
        }
        else if(title.toLowerCase().contains("2")){
            notificationChannel.setSound(alarmsound, audioAttributes);
        }
        else if(title.toLowerCase().contains("3")){
            notificationChannel.setSound(alarmsound, audioAttributes);
        }
        else{
            notificationChannel.setSound(defaultsound, audioAttributes);
        }
        notificationManager.createNotificationChannel(notificationChannel);
    }
Enzo
  • 57
  • 9
  • You need to create different Notification channel for each type .. Just declare two local variable containing Channel name and Id and modify them as per type . – ADM Apr 29 '19 at 11:54
  • 1
    @ADM I created two notification channel and it worked. Thank you – Enzo Apr 29 '19 at 16:10
  • @Enzo can you share me the code how did u created channel i also tried same but nothing work for me. – NagarjunaReddy Jun 10 '22 at 09:59

0 Answers0