Custom sound in Firebase push notification
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"
}
How to send Push Notifications with custom sounds via the Firebase FCM Console
Once you have compiled your app with the sound notification in your app's resource folder
You can then use the Notification Composer to send custom payloads from the Firebase Console, including sound settings in the custom payload as key
:value
.
Documentation: https://firebase.google.com/docs/cloud-messaging/js/send-with-console#about
You can also invoke FCM with cloud functions with this kind of payload
"message":{
"token":"ewrTK.....",
"notification":{
"title":"Breaking News",
"body":"New news story available."
},
"data":{
"title":"Notification title",
"body":"Notfication body"
},
"apns":{
"payload":{
"aps":{
"sound":"notification.mp3"
}
}
}
}
No custom sound with Android Firebase Notification
Finally I found the solution. For Android 8.0 and higher it's necessary to create a notification channel in your App:
NotificationChannel channel = new NotificationChannel('my_id', name, importance);
(more info: https://developer.android.com/training/notify-user/channels#java)
Then when you send the notification:
var registrationToken = 'xxxxxx';
var message = {
notification: {
title: 'my title',
body: 'my body',
},
android: {
ttl: 3600 * 1000,
notification: {
color: '#ff0000',
sound: 'mysound.mp3',
channel_id: 'my_id' // important to get custom sound
}
},
token: registrationToken
};
Related Topics
iOS - Using Uisearchdisplaycontroller with Uisearchbar That Is Uibarbuttonitem in Uitoolbar
Why Is a Https Nsurlsession Connection Only Challenged Once Per Domain
Breaking on Unrecognized Selector
iOS Calculate Text Height in Tableview Cell
How to Create Scrollable Table in Skspritekit
Avplayer Uitapgesturerecognizer Not Working
iOS - Custom Table Cell Not Full Width of Uitableview
Is There a Custom Url Scheme for the Built-In Contacts App
How to Solve This Exc_Bad_Access(Code=Exc_I386_Gpflt )In Swift Programming
Show Data in Uipickerview Second Component Based on First Component Selection
How to Switch to Speaker Output When Bluetooth Headsets Are Connected
How to Monitor Battery Level and State Changes Using Swift
iOS Automatic @Synthesize Without Creating an Ivar
Ios- Check Whether Current Time Is Between Two Times or Not
How to Determine the Correct Altitude for an Mkmapcamera Focusing on an Mkpolygon