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
};
Android Firebase notification have no sound
Just do one thing say your backend developer to just send data payload in the notification, ask him to restrict and remove notification payload,
Because when you are getting a notification at that time if your app in the background and you are getting notification payload at that time system handle notification from their side and that's why problem arise,
So simply just remove notification payload from the server-side and it will work fine.
Add your php code like bellow for data
$title = 'Whatever';
$message = 'Lorem ipsum';
$fields = array
(
'registration_ids' => ['deviceID'],
'priority' => 'high',
'data' => array(
'body' => $message,
'title' => $title,
'sound' => 'default',
'icon' => 'icon'
)
);
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"
}
Related Topics
How to Create Multiple Pendingintents with the Same Requestcode and Different Extras
Status Bar Turns White and Does Not Show Content Behind It
Android App Integrated with Ok Google
Android:Load Svg File from Web and Show It on Image View
Access_Coarse_Location Permission Gives a Cell Tower Precision on Android
Android:Textallcaps="False" Not Working for Tablayout Design Support
My Android Camera Uri Is Returning a Null Value, But the Samsung Fix Is in Place, Help
Android' Is Not Recognized as an Internal or External Command
Android Sax Parser Not Getting Full Text from Between Tags
Recyclerview Items with Big Empty Space After 23.2.0
Add an Image from Url into Custom Infowindow Google Maps V2
Get Android Subscription Status, Failed with 403