Change Push Notification Sound

Change push notification sound

Follow Apple documentation for preparing custom sound file for your app.

For remote notifications in iOS, you can specify a custom sound that
iOS plays when it presents a local or remote notification for an app.
The sound files can be in the main bundle of the client app or in the
Library/Sounds folder of the app’s data container.

Custom alert sounds are played by the iOS system-sound facility, so
they must be in one of the following audio data formats:

Linear PCM MA4 (IMA/ADPCM) µLaw aLaw You can package the audio data in
an aiff, wav, or caf file. Then, in Xcode, add the sound file to your
project as a nonlocalized resource of the app bundle or to the
Library/Sounds folder of your data container.

You can use the afconvert tool to convert sounds. For example, to
convert the 16-bit linear PCM system sound Submarine.aiff to IMA4
audio in a CAF file, use the following command in the Terminal app:

afconvert /System/Library/Sounds/Submarine.aiff ~/Desktop/sub.caf -d
ima4 -f caff -v You can inspect a sound to determine its data format
by opening it in QuickTime Player and choosing Show Movie Inspector
from the Movie menu.

Custom sounds must be under 30 seconds when played. If a custom sound
is over that limit, the default system sound is played instead.

Once you have made the file, easiest way is to put it in app bundle.

The, when you send push notification, just add the name of file in JSON payload. Example:

{
"aps" : {
"alert" : "You got your emails.",
"badge" : 9,
"sound" : "bingbong.aiff"
}
}

Thats it! You don't have to do anything special in code of app.

Edit:
Please put the file inside your project bundle (i.e inside the hierarchy of project) and have Copy items if needed option selected while drag and drop. The blacked out part has project name.

enter code here

how to play custom notification sound in ios push notification using FCM

thanks for your answers and comments

It is working fine now after adding notification file in build phases (Xcode).

For Ref: Playing a custom sound on receiving a remote push notification on iOS 12 from FCM

Change notification sound in iOS Flutter

You should replace this default given sound name to your sound name.

var iOSPlatformChannelSpecifics = IOSNotificationDetails(sound: 'slow_spring_board.aiff'); //put your own sound text here 

(Before this, your sounds should be added to your project using Xcode)
Here is the link how to add custom sound to your project for iOS devices using Xcode: https://medium.com/@dmennis/the-3-ps-to-custom-alert-sounds-in-ios-push-notifications-9ea2a2956c11

Note: Sound duration should be less than 30 seconds otherwise it will not work

Custom sound push notification does not work (Flutter)

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 the
Library/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 sound Submarine.aiff to IMA4
audio in a CAF 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"
}
}

Sample Image

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.

Sample Image

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 /p>

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 /p>

Swift -How to change push notification && sound from within the app itself?

There doesn't seem a possible way to do this from within the app itself

https://stackoverflow.com/a/33520827/4833705

https://stackoverflow.com/a/36654919/4833705

Also I tried the code below and when Notifications were on (from outside the app) I used this to toggle them off and it didn't do anything.

@objc func switchValueDidChange(_ sender: UISwitch) {
if (sender.isOn == true) {

// *** doesn't work ***
UIApplication.shared.registerForRemoteNotifications()

} else {

// *** doesn't work ***
UIApplication.shared.unregisterForRemoteNotifications()
}
}


Related Topics



Leave a reply



Submit