Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android notification setSound is not working

In my hybrid Cordova Android app targeting API 23+ I want to use a custom sound for notifications. To that end I have done the following

  • In plugin.xml file for the single custom plugin I use in the app I declare <resource-file src="src/android/res/unysound.mp3" target="res/raw/mysound.mp3" />'.

Opening the APK as a zip archive I see that the mp3 file has in fact ended up in `res/raw/mysound.mp3'. - When building the notification I do the following

    Notification notification = new Notification.Builder(context)     .setDefaults(0) //turns off ALL defaults     .setVibrate(vibrate)  /sets to vibrate     ....     .setSound(uri).build(); 

where

Uri uri = Uri.parse("android.resource://" + ctxt.getPackageName() + "/raw/mysound.mp3"); 

This appears to be the recipe indicated in a number of articles I find on a spot of googling and even in other threads on SO. And yet, when I issue a notification I do not hear the expected sound. What might I be doing wrong?


The answer below does not help since in the context of my hybrid Cordova app with a custom plugin attempting to build the APK throws up an error along the lines of class R not known/found...

like image 980
DroidOS Avatar asked Feb 26 '18 10:02

DroidOS


People also ask

Why is my notification sound not working?

Tap on three-dot apps in your Messages app, and go to Settings > Notifications. If not, enable Show notifications and proceed to tweak options such as Allow playing sound and Allow vibration.

How do I set notification channels on Android?

To create a notification channel, follow these steps: Construct a NotificationChannel object with a unique channel ID, a user-visible name, and an importance level. Optionally, specify the description that the user sees in the system settings with setDescription() .


2 Answers

below code will help you:

 String CHANNEL_ID="1234";      Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.mysound);     NotificationManager mNotificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);         //For API 26+ you need to put some additional code like below:     NotificationChannel mChannel;         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {                 mChannel = new NotificationChannel(CHANNEL_ID, Utils.CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);                 mChannel.setLightColor(Color.GRAY);                 mChannel.enableLights(true);                 mChannel.setDescription(Utils.CHANNEL_SIREN_DESCRIPTION);                 AudioAttributes audioAttributes = new AudioAttributes.Builder()                         .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)                         .setUsage(AudioAttributes.USAGE_NOTIFICATION)                         .build();                 mChannel.setSound(soundUri, audioAttributes);                      if (mNotificationManager != null) {                     mNotificationManager.createNotificationChannel( mChannel );                 }         }     //General code:      NotificationCompat.Builder status = new NotificationCompat.Builder(getApplicationContext(),CHANNEL_ID);                                status.setAutoCancel(true)                                 .setWhen(System.currentTimeMillis())                                 .setSmallIcon(R.drawable.logo)                                 //.setOnlyAlertOnce(true)                                 .setContentTitle(getString(R.string.app_name))                                 .setContentText(messageBody)                                 .setVibrate(new long[]{0, 500, 1000})                                 .setDefaults(Notification.DEFAULT_LIGHTS )                                 .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE+ "://" +mContext.getPackageName()+"/"+R.raw.apple_ring))                                 .setContentIntent(pendingIntent)                                 .setContent(views);                                                  mNotificationManager.notify(major_id, status.build()); 

where mysound is my ringtone which is put under res/raw folder.

Note: you have to only put name of ringtone without extension like raw/mysound

Note: In Android Oreo you must change your channel ID to changes take effect. And before Oreo version, using ".setDefaults()" seens prevent custom sound to play.

like image 53
Ketan Patel Avatar answered Sep 28 '22 11:09

Ketan Patel


  1. Try clearing data (or fresh install)
  2. Trying this again

The settings are set the first time you create the channel and then not modified unless you do it manually by fresh install or clearing data.

For more info on this read the top answer here: Android Oreo notification keep making Sound even if I do not set sound. On Older version, works perfectly

like image 44
Siddharth Jaswal Avatar answered Sep 28 '22 12:09

Siddharth Jaswal