Modify 'sound' attribute in Push Notification Payload in UNNotificationService Extension - ios

I am using a UNNotificationService extension to update the Payload content of a received Push Notification.
I'm able to change the displayed alert text and the updated text is correctly displayed. However changing
"sound":"default" => "sound":"something.caf"
does not play the modified sound. The default alert sound is played.
The audio file "something.caf" is packaged with the App and plays correctly when the Payload received from the backend server contains "sound": "something.caf".
Any suggestion to what I should be looking at? (missing configuration etc)
Thanks

Is the sound file added to your Notification Service ?
You can verify this on inspector tab -> Target Membership section

Setting the "UNMutableNotificationContent.sound" property to the required sound file fixes the issues and plays the required file.

Related

FCM custom sound not working on iOS when in background mode

Did something change with FCM recently? Our apps use custom sound in push notifications. This has been working fine on both Android and iOS, whether in foreground, background, or closed. But recently something seems to have changed as the same PHP code we've been using suddenly stopped working for custom sounds when the app is closed or in background mode? (The notification is delivered, but it just uses the default device sound).
I was able to get it working again on Android by just including the sound files in /app/src/res/raw (we used to keep them there, but for a long time now it wasn't needed yet always worked... apparently it's needed again). Ok Android solved.
But for iOS it still has stopped working! I'm not sure if it's something I'm doing wrong in xCode or if I need to change the PHP code used to trigger the notifications. So here's both to review:
I am using:
xCode 11.6
iOS target 11.0
Testing on real device, iOS 13.6.1
Sound files are at {Project Name}/Resources/ and were added with "Copy items if needed" and "Create groups" both checked
Server side code to trigger notifications (which always worked fine before and has not been changed):
{
"notification": {
"title": "Test Title",
"body": "Test Body",
"sound": "sound_file.mp3",
},
"registration_ids": ["APA...g","fl...CS"]
}
Please help... thought I was past all of this years ago...ugggg!
Detail information:
https://medium.com/#ThatJenPerson/authenticating-firebase-cloud-messaging-http-v1-api-requests-e9af3e0827b8
Sample code:
{
"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"
}
}
}
}
}
We just touched base with the Firebase dev team and they were able to reproduce the issue. They have just created an internal ticket to fix this. Will follow up here on the resolution progress
After several additional rounds communication with Firebase, here is the current status update and their response:
I would like to let you know that this issue was just recently
identified as a bug with our Legacy API that’s not properly handling
the custom sound for iOS. Our FCM gurus have identified the fix for
this issue, and the fix is about to be released on our production, but
we can’t share any exact details or timeline for the release. The fix
should let you send notifications properly based on your custom sounds
configuration and there should be no code changes needed on your end.
Also, it should still properly handle the notification sounds if the
app is closed or in background state for your iOS application.
We are furthermore told that for the moment, migration from FCM HTTP to HTTP v1 is the only work around. Will post here again once we hear confirmation on the fix to the Legacy API (which most people reading this as using). Stay tuned...
We created a ticket with Google/Firebase support.
Their answer:
There's no ongoing issue on our end that may cause the custom sound to not work. However, by specifying a custom sound, the sound file must be on the user's device already, either in the app's bundle or in the library/Sounds folder of the app's container. You can refer to this documentation for more information.
For FCM, there isn't any extra sound processing, only passing the payload field, so as long as the payload on the client shows "sound" file is successfully passed down, there may be an issue with the setup. If there's no recent change for your application and the sound files, then this may be something caused on Apple's end. With that, it's best to check this with them.
We discovered that we apparently use the legacy HTTP API so we tested the newer HTTP v1 version. (described here: https://firebase.google.com/docs/cloud-messaging/migrate-v1 )
This solved the problem for us. Custom sound is back.
In the mean time Firebase support replied the following and now acknowledges there is a problem with the legacy HTTP API.
Thanks for sharing your observations, Mark.
We’ve received similar reports regarding this issue and that indicates
that this is not an unusual issue in relation to the increasing
reports on the Stack Overflow discussion that you’ve mentioned too.
With that, this issue was just recently identified as a bug with our
Legacy API that’s not properly handling the custom sound for iOS. Our
engineering team has identified the fix for this issue, and we
couldn’t provide a definite timeline at the moment for the release in
our production. I’ll let you know once I have an update for this
issue.
For now, you might consider using our HTTP v1 API to send your
messages. Apologies for the inconvenience this may have caused you.
On my side, no matter what sound name I set, I always receive on ios side "sound": "default"
Sending:
{
"notification": {
"title": "Alert",
"body": "Test Message",
"sound": "alert.aiff",
},
"registration_ids": ["APA...g","fl...CS"]
}
Receiving:
{
aps = {
alert = {
body = "Test Message";
title = Alert;
};
"content-available" = 1;
sound = default;
};
}
The only reason for this to happen is that FCM set sound to default while it redirect to APNS. Need ask google
Please check guys, Google has fixed this issue !!

how can I play sound for long time when PN(push-notification) remote notification arrive

How can I repeat sound file when PN(push-notification) arrive in my device using Objective- c.
You can find how to Custom Alert Sounds in this website: Managing Your App’s Notification Support and the ringtone can not be Over 30s
Preparing Custom Alert Sounds
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 must be in the main bundle of the client app.
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.
You may 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.
You can make this example dictionary send to APNs server with your custom alert sound name for the value of 'sound':
// Create the payload body
$body['aps'] = array(
'alert' => 'TestNotification',
'sound' => 'YourCustomAlertSoundName.caf',
'badge' => 8
After making a good ringtone to your project's main bundle, the important step is to add the sound file you added (YourCustomAlertSoundName.caf) to Build Phases-> Copy Bundle Resources in the Project Settings, and the ringtone can not be Over 30s, otherwise the system will enable the default ringtone (default).
You just have to set custom file into sound on push payload. Put the same audio file into your build resource and just fire notification from server, it will play audio.
{
"aps" :
{
"alert" : "This is your alert text"
"sound" : "YOUR_COSTOM_FILE_NAME.mp4"
},
}
When the application is in foreground mode, then you need to play same audio file when you get receive notification.

iOS custom push notification sounds using GCM

I am trying to play a custom sound when receiving a push notification to my iOS app. I am using GCM on my server to push to Android and Apple devices.
By defaults I was sending the following in my push notification payload:
"sound":"default". This was working no problems, 100%.
As soon as I changed it to: "sound":"sound.wav" I was no longer receiving the "sound" tag in my payload in the debugger and naturally no sound was playing when the device received the payload.
Obviously I have added sound.wav to my bundle and checked it in the bundle resources.
Can anybody shed some light on this problem?
There are few things which can cause this.
The sound file "sound.wav" should be a second long and limited size in KBs.
Make sure your payload is like this:
{
"title": "Some title",
"data": [
{
"version": "1.0"
}
],
"aps": {
"badge": 1,
"sound": "soundfilenameinbundle.wav",
"alert": "Some Alert to Show"
}
}
Please make sure "sound" tag goes in "aps".
As you have said you have added the sound file, can you also check if it is added in the required target.
Sometimes big sized payloads also cause this. Try by reducing your payload size by removing some data for testing and check
As you said you cannot see "sound" tag in the payload. I suspect the problem in payload itself. Can you try changing file name.
Also try on iOS 7/8/9 to check if the problem is not OS specific.
I hope this will help.
Thanks Suhail,
The problem is with Google Cloud Messaging. They only support the "default" value for the sound tag. So whenever I change it to anything but that the GCM server does not send the sound tag at all.
Hope they fix it soon...
Details here:
https://developers.google.com/cloud-messaging/http-server-ref

iOS Push notification Custom sound

I need to customize push notification sound. I don't want to create and include to Bundle. I need to implement like whatsapp notification sounds or please give a list of Apple provided sounds(like default sound). Any help would be highly appreciated.
Thanks
After digging this issue for more than 3 weeks, and writing back and forth with apple this is the only solution
change payload to something like
sound = "custom"
now you need to show all your system sounds in a table view (if you don't know how to do it, look on github).
once the user mark his sound notification you need to copy the file to library/sound and give it the name of custom (or whatever the exact name in the payload).
that way you never change the server side code and you keep it on custom, and on the other hand you just overwrite the custom file with a new sound the user has been chosen.
*SIDE NOTE: on version 9.2.1 there's a bug which cause the notification not to work on the second time, or at all, it's should be fixed according to Apple in the next version 9.3 !
in the breath I wish the solution I could override the payload like we can do in Android, Apple makes push notification a lot harder on the developers.
in you bundle add a sound file named "pushSound.caf".
//write your payload this way
{
aps =
{
alert = "message";
sound = "pushSound.caf";//this file will have to your bundle
};
}
Preparing Custom Alert Sounds
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.
It needs to be in the correct format as well, see https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW6
To use the default sound for a notification
let content = UNMutableNotificationContent()
/// Set up content ...
content.sound = UNNotificationSound.default()
To use a custom sound, the sound file has to be stored in the app's main bundle OR download it and store it in the Library/Sounds subdirectory of the app's container directory.
The "main bundle" approach can only be used with a new application release, the "downloading the sound file" approach is more flexible and makes shipping new sounds without a new version release.

How to assign device alert tones as notitfication sound in iPhone

How to get only Alert tones of device (not all the device sounds) and select one of them as my app notification.
I've gone through Apple documentation which states that we cannot use System Sounds for our app.But, in WhatsApp you can see this feature.
Please help me out to solve this issue.
The aps dictionary contains a property that specify a sound to play:
{
"aps" : {
"alert" : "You got a push.",
"badge" : 9,
"sound" : "bingbong.aiff" //here
}
}
You have to embed the sound file in your app, if you like the systems sounds, then try to make your own.
Documentation
The name of a sound file in the application bundle. The sound in this
file is played as an alert. If the sound file doesn’t exist or default
is specified as the value, the default alert sound is played. The
audio must be in one of the audio data formats that are compatible
with system sounds; see “Preparing Custom Alert Sounds” for details.
Note: You Not put Alert Notification sound means Its default Take System(iPhone or iPad) Message Tone.
You can't get List of Tones from iPhone.Apple not allowed this. WhatsApp may be used for copy the tones and add their resource file.
Reference:
Access apple's iOS ringtones and display them

Resources