Custom sound push notification does not work (Flutter)
ShadowSheep did a good job at answering this question, but there's one thing I want to clarify for trying to get the iOS sounds to work.
You have to add the sound into XCode (which is where ShadowSheep speaks of including the asset inside of the main bundle
). You can just drag and drop the audio file (in .caf or other supported format mentioned above) into the root directory (usually called Runner for Flutter) in XCode:
If you have done this and follow the setup described in the above question/answer, you should be in business.
Reading this it seems that it should be manage automatically (if you didn't use a notification builder) on Android but you have to specify the .mp3
extension too and put it inside notification
field and not data
one..
"sound": "alarm.mp3"
iOS
behaves very differently under the hood but you can use a custom sound by setting the sound:
field in the notification payload too. Anyway .mp3
is not a valid APN notification file format, and you need to specify also the file extention.
"sound": "filename.caf"
Follow Apple documentation in order to forge your custom sound file for your app.
mp3 is not a valid format
Preparing Custom Alert Sounds
Local and remote notifications can specify custom alert sounds to be played when the notification is delivered. You can package the audio data in an aiff, wav, or caf file. Because they are played by the system-sound facility, custom sounds must be in one of the following audio data formats:
Linear PCM
MA4 (IMA/ADPCM)
µLaw
aLaw
Place custom sound files in your
app bundle
or in theLibrary/Sounds
folder of your app’s container directory. Custom sounds must be under 30 seconds when played. If a custom sound is over that limit, the default system sound is played instead.You can use the
afconvert
tool to convert sounds. For example, to convert the 16-bit linear PCM system soundSubmarine.aiff
toIMA4
audio in aCAF file
, use the following command in the Terminal app:afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d ima4 -f caff -v
For exampole to convert your mp3
file in a caf
file you could type in terminal:
afconvert -f caff -d LEI16 alarm.mp3 alarm.caf
Read this doc in order to have a deep inside of all generic and specific notifciation payload fields.
UPDATE
I've tested the Android part and I can confirm that putting your .mp3
file in res/raw/
folder the sound is played as documented and expected.
That's my notification payload:
{
"to" : "my_device_token",
"collapse_key" : "type_a",
"priority" : "high",
"notification" : {
"body" : "Test Notification body for custom sound {{datestamp}}",
"title": "Custom sound alert.mp3",
"sound": "alert.mp3"
}
}
I've tested also the iOS version after converting .mp3
file to .caf
file in that way:
afconvert -f caff -d LEI16 alert.mp3 alert.caf
the same json
payload with the different filename works:
{
"to" : "my_device_token",
"collapse_key" : "type_a",
"priority" : "high",
"notification" : {
"body" : "Test Notification body for custom sound {{datestamp}}",
"title": "Custom sound alert.mp3",
"sound": "alert.caf"
}
}
Remember to add the file in your main bundle
.
That works if the app is terminated or in background.
If you want to show an alert and play a sound when the app is in foreground you have to manage it on onMessage
event like someone already have told you here, or you can use a platform-channel here to build your own notification with a Notification.Builder on Android and a UNNotificationCenter on iOS (for example).
UPDATE
This issue has been solved. See here the official comment:
Hey all ð
As part of our roadmap (#2582) we've just shipped a complete rework of the firebase_messaging plugin that aims to solve this and many other issues.
If you can, please try out the dev release (see the migration guide for upgrading and for changes) and if you have any feedback then join in the discussion here.
Given the scope of the rework I'm going to go ahead and close this issue in favor of trying out the latest plugin.
Thanks everyone ð¤
For me I am using the flutter_local_notifications to create the notification channel.
include this function (may create multiple notification channel)
Future<void> _createNotificationChannel(String id, String name,
String description, String sound) async {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var androidNotificationChannel = AndroidNotificationChannel(
id,
name,
description,
sound: RawResourceAndroidNotificationSound(sound),
playSound: true,
);
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(androidNotificationChannel);}
call the function in initState: (this created 2 notification channel)
_createNotificationChannel("channel_id_1", "channel_name", "description", "alert");
_createNotificationChannel("channel_id_2", "channel_name", "description", "alarm");
Remember to save the file of alert
and alarm
in the res/raw
in file format of .mp3
.
with this payload :
{
"notification": {
"title": "My First Notification",
"body": "Hello, I'm push notification"
},
"data": {
"title": "My First Notification"
},
"android": {
"notification": {
"channel_id": "channel_id_1"
}
},
"to": "device_token"}