iOS custom push notification sounds using GCM - ios

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

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 !!

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

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.

iOS 10 Service Extension Inconsistent

While there is a similarity to another question here:
iOS 10 don't call Notification Service Extension
I wish to provide a more concrete issue with concrete misbehavior. Consider the following push payload:
{
"service-alert":{
"title":"[Service]Title",
"subtitle":"[Service]Subtitle",
"body":"[Service]Body may be substituted."
},
"image":"https://some.small/thumbnail.jpg",
"aps":{
"badge":1,
"alert":{
"title":"[Default]Title",
"subtitle":"[Default]Subtitle",
"body":"[Default]A simple body of text."
},
"sound":"default",
"mutable-content":1
}
}
(the "service-alert" dictionary is for testing over-rides on the default alert text for devices that support it)
I'm debugging in Xcode on my iOS 10 device (iPhone 6+), I set a breakpoint in the Service Extension to verify it receives an opportunity to modify the push before presenting.
When I send the push, there are times when the Service Extension is hit (breakpoint hit), I can verify that the push payload is correct, and the displayed push shows only the default "aps" payload. No image. No modified title/subtitle/body.
My breakpoint in serviceExtensionTimeWillExpire is not hit. The notification mechanism appears to be "working" yet the result is incorrect.
When I send the exact same push again, it works. The image is loaded from the "image" URL and the modified "service-alert" text is displayed as designed. Sometimes I have to send the same push 2 or 3 times before it works, but then it seems consistent after that. So, if I see a modified notification once, from that point on every push test appears to work properly.
I feel, but do not have conclusive data to support yet, that a period of inactivity causes the Service Extension to be flushed and it is too slow to wake up when called upon. If so, this makes it useless, because it will never be fully awake when a push is received.
Current environment:
Xcode 8.2 beta (8C30a)
iOS 10.2 betas 2, 3, and 4
Debug scheme
Can anyone confirm/deny this behavior on iOS 10 beta and/or non-beta?
Have I found a 10.2 bug, or is this "normal behavior?"
Using this Pay load Push will come along with default sound
{
"aps" : {
"alert": {
"title": "main title",
"subtitle":"Subtitle",
"body": "Custom message"
},
"badge" : 0,
"sound" : "default"
}
}
}

Google Cloud Messaging showing success message but not sending iOS

So I have run into a very strange problem with Google Cloud Messaging. The problem I am having is that it is registering the devices successfully, and when a message is sent I get a success message from Google. But the devices never receive any messages.
The message I get back from GCM is:
"result": "Push notification sent successfully: {\"multicast_id\":6008387530769664000,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1442824842607522%73fc535e73fc535e\"}]}"
To make things even more confusing, my implementation was working about 2 weeks ago and I have not changed anything to date. The Android version of the app is receiving messages with no problems it is only the iOS implementation that is not working.
Any help would be much appreciated!
Thanks!
So I finally solved this issue after of pulling the last remaining hairs out of my head.
It turns out the devices are receiving the messages but GCM sets the priority to the lowest priority by default. This means the device receives the notification but never displays it. This priority is used for silent notifications to wake the app up in the background. I discovered this because I kept receiving the message in the console saying:
Low Priority Push: [com.test.app] - Background Refresh Not Supported
Priority is a value between 1 and 10 so I then set the priority to 10 and got the message instantly on the device. My GCM POST request body now looks like this:
{
"to": "GCM token here",
"notification": {
"sound": "default",
"badge": "2",
"title": "default",
"body": "Test Push!",
},
"priority" : 10,
}
I really hope this helps others as I have spent a week pulling my hair out regarding this.
(ノಠ益ಠ)ノ
EDIT:
You can set "priority" to "high" and that works exactly the same as setting it to "10" (priority is a value between 0 and 10. Google coverts the text to the number for iOS
Instead of adding "priority" : 10,
You should add the following line:
"content_available" : true,
In APNS server (iOS), The content_avaialble changes to 1 which leads the push notification in background. And adding "priority":10, will drain more iphone battery. In my case I don't even have anything related to priority, but it still works.

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