FCM Custom notification sound
11,957
Solution 1
notification.sound = Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.pop);
notification.defaults |= Notification.DEFAULT_VIBRATE;
Use above code to add custom sound from resources.
The above code can be used if we are using Notification class.
Notification notification = new Notification(icon, tickerText, when);
As you are using NotificationBuilder, use the below code.
Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notifysnd);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.mipmap.ic_notif)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(sound)
.setContentIntent(pendingIntent)
.setCustomBigContentView(remoteViews)
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image))
;
Solution 2
Use setSound()
method to set the sound
if(!silent) { // check if phone is not in silent mode
notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(9999, notificationBuilder.build());
}
}
Or you can use
{
"to" : "XXYYXXYY...",
"notification" : {
"body" : "The stock opened on a bullish note at Rs. 449 and touched a high of Rs. 461.35, up 5.06 per cent over its previous closing price on the BSE. A similar movement was seen on the NSE where the stock opened at Rs. 450 and hit a high of Rs. 463.70, up 5.32 per cent.",
"title" : "Stocks in focus: Kalpataru Power, Punj Lloyd, J B Chem, Bharti Airtel",
"icon" : "ic_stock",
"sound" : "res_notif_sound"
}
}
If you want to use default sound of the device, you should use: "sound": "default".
Author by
Jack N
Updated on June 04, 2022Comments
-
Jack N almost 2 years
I have Firebase messaging with my andriod application. I am using Firebase to send push notifications. I want to change the default notification sound to a custom one. how can i do it ?
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setLargeIcon(image)/*Notification icon image*/ .setSmallIcon(R.mipmap.ic_notif) .setContentTitle(title) .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent) .setCustomBigContentView(remoteViews) .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(image)) ; NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(Integer.parseInt(id) /* ID of notification */, notificationBuilder.build()); }
-
Ergin Ersoy over 6 yearsyou can find this article very useful. It explains how you can handle custom sound.
-
-
Jack N over 6 yearsWhere should i replace it ? Can we Modify above code with ours or ?
-
Rinav over 6 years@JackN, unable to understand your comment
-
Jack N over 6 yearsIt [Worked ] Thank You Very Much Bro :) it Helped me a lot :)
-
TheHound.developer over 6 yearsMy Pleasure to help. Please accept the answer as it is working. :)
-
Anu Bhalla over 5 yearsres_notif_sound is system file? and If we want to refer to a file in res folder. how to do that?
-
Anu Bhalla over 5 yearsThanks,It helped me a lot.
-
Rinav over 5 years@AnuChaudhary Happy to Help ;)