1
votes

I am sending push notification from Firebase to my Android application, but it is only playing the default sound when the notification is recieved.

I have set the custom sound param {“sound”:”notificationsound.mp3”} in the fcm notification object and the file is present in the res/raw folder according to (https://firebase.google.com/docs/cloud-messaging/http-server-ref) But It’s still playing the default sound on all app states (background, foreground and killed). This is my request body for sending the notification on Android :

{
"to" : “some id”,
"notification": {
"title":"asdasd",
"body":"sdsad",
"click_action" : "MAIN",
"sound":"notificationsound.mp3"
},
"data": {
"title" : "Something",
"body" : "Important",
"type" : "message"
},
"priority": "high"
}

What I can do to play the custom notification sound.

1
It's possible that your android device does not allow overriding the notification sound. You are using the legacy firebase api which also might not work in the same way for newer devices. But it's just a guess. Perhaps try the new api: firebase.google.com/docs/reference/fcm/rest/v1/… - JensV

1 Answers

3
votes

I was also looking for the solution to custom sound for firebase notification in the android, And I have solved this problem through Notification Channel.

I have created one notification channel with custom sound, that sound plays after receiving notification in the application background state.

You can refer following links of the notification channel.

https://medium.com/exploring-android/exploring-android-o-notification-channels-94cd274f604c

https://developer.android.com/training/notify-user/channels

You need to put your mp3 file at /res/raw/ path.

Please find the code.

NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NotificationManager.class); // If you are writting code in fragment

OR

NotificationManager notificationManager = (NotificationManager) getSystemService(NotificationManager.class); // If you are writting code in Activity

createNotificationChannel function

private void createNotificationChannel() { 
 Uri sound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + context.getPackageName() + "/" + R.raw.sample); //Here is FILE_NAME is the name of file that you want to play 
// 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 = "mychannel"; 
    String description = "testing"; 
    int importance = NotificationManager.IMPORTANCE_DEFAULT; 
    AudioAttributes audioAttributes = new AudioAttributes.Builder() 
     .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) 
     .setUsage(AudioAttributes.USAGE_ALARM) 
     .build(); 
   NotificationChannel channel = new NotificationChannel("cnid", name, importance); 
   channel.setDescription(description); 
   channel.enableLights(true); channel.enableVibration(true); 
   channel.setSound(sound, audioAttributes); 
   notificationManager.createNotificationChannel(channel); 
  } 
};

createNotificationChannel(); 

To achieve this you need to pass android_channel_id property in the firebase notification request object.

{
 "notification": {
 "body": "this is testing notification",
 "title": "My App",
 "android_channel_id": "cnid"
 },
 "to": "token"
}