Custom sound for remote notification - ios

I have to set custom sound when I receive APN in my iOS app.
For custom sound I mean a sound that every user can change within his app and not different from the default sound.
I already know that I can set the attribute "sound" on the payload of my notifications, but what I would like to understand if there is a way to play a different sound based on the user's settings (user A can set sound1.aiff, user B can set sound2.aiff).
The only way that I can see is that the notification is already sent with the name of the file to play that the user has set on preferences: this means that I have to store settings on server-side. Now I do not this.
Do you confirm me that's the only way?

Not an ideal solution, but this might work for you...
Send a silent notification instead. When your app receives the silent notification, check the user’s settings and post a local notification with the desired sound.

Related

APNS Modify Notification to be a Silent Notification

I have a weird feature I'm looking to add to iOS and am not sure if it's possible.
I want to send push notifications to all users through a third party and have the client decide whether or not to show it depending on some feature. I was reading that I can modify the notification before it reaches the app and was hoping that I could receive the notification, do some logic and, if the criteria is satisfied, modify the notification to be silent. But I'm not sure if this is possible.
Has anybody been able to do this?
As far as I know, it's not possible to hide a notification after it was sent.
You can modify the payload through a service extension but I'm pretty sure you cannot hide it from the user.
To decide on the client if a notification is visible or not - you'll have to send your notification as silent to begin with and then trigger a local notification.
The problems with that are:
Silent notifications are disabled if the user disabled Background Activity.
Silent notifications have a lower priority and might be throttled after a while.
The purpose of silent pushes is to inform the app of new content to perform a background fetch.
My recommendation is to put whatever logic you want on the server side before sending the notification.
For Android you can decide whether to show the notification or not.
For iOS, you can only modify the way the notification is presented but you cannot stop it from being shown. In order to modify the way the notification is presented to the user on iOS you need to add a Notification Extension Service.

IOS Silent Push Notification to send custom Key/Value

I have a use case where I need to send a custom key/value pair. Please see the example below:
{
"aps":{"content-available":1},
"test_data":{"Name":"Vinay"}
}
I have done this exercise at my end but I am not able to send this, Also I have not find a clear proof where Apple restrict this. Can anybody support me on this context.
Yes Apple authorize it the silent push. In fact, Apple explain how to to so:
To support silent remote notifications, add the remote-notification value to the UIBackgroundModes array in your Info.plist file. To learn more about this array, see UIBackgroundModes.
And, in Configuring a Silent Notification:
The aps dictionary can also contain the content-available property. The content-available property with a value of 1 lets the remote notification act as a silent notification. When a silent notification arrives, iOS wakes up your app in the background so that you can get new data from your server or do background information processing. Users aren’t told about the new or changed information that results from a silent notification, but they can find out about it the next time they open your app.
-
For a silent notification, take care to ensure there is no alert, sound, or badge payload in the aps dictionary. If you don’t follow this guidance, the incorrectly-configured notification might be throttled and not delivered to the app in the background, and instead of being silent is displayed to the user.
Update:
But it seems to be impossible to have a totally silent push AND custom data. It's not well documented, but take a look at this: https://stackoverflow.com/a/36327058/2846494
Source:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html#//apple_ref/doc/uid/TP40008194-CH107-SW6
https://stackoverflow.com/a/36327058/2846494
there is no issue with this until and unless you have configured well. For configuration details refer apple documentation. I have created a POC and it is working perfectly fine and I noticed only once issue if we are utilising Artisian SDK(older version) so it will swallow your payload and that is the only Issue what I was facing.
To be precise:
We can invoke app in background without notifying user by using silent notification. And we can send our custom data as shown in below example:
{"Data":"Vinay","aps": {"content-available": 1}}

push notifications with NO sound JUST in some situations

The app is registered to receive all the 3 kinds of notifications (badge, alert, sound).
I want that push notifications be with NO sound in background JUST IN some situations (in the app user click a button to disable notifications) and to allow the sound for all the other cases.
Do you have some ideas? Thanks
From Apple's Remote Notification Guide:
The aps dictionary can also contain the content-available property.
The content-available property with a value of 1 lets the remote
notification act as a “silent” notification. When a silent
notification arrives, iOS wakes up your app in the background so that
you can get new data from your server or do background information
processing. Users aren’t told about the new or changed information
that results from a silent notification, but they can find out about
it the next time they open your app.
To support silent remote notifications, add the remote-notification
value to the UIBackgroundModes array in your Info.plist file. To learn
more about this array, see UIBackgroundModes in Information Property
List Key Reference.
Depending on your model (assuming a system like Parse or something), store the user's preference in the cloud and, when generating your PUSH notifications, decide whether to add this flag or not.

How to allow user to select notification sound from default sounds in Settings.app?

I'm wondering if it's possible to allow the user to change the push notification sound for my app in Settings.app->Notifications to any of the default sounds (similar to how the alert tone for Messages can be changed there). If so, what do I need to change in code to allow this? Do I need to change the Settings.bundle?
You can send the name of the sound file chosen by the user to your server (along will the device token) and store it in your DB. When you send a push notification to that user's device, you'll fetch the sound file name from the DB and put it in the sound property of the notification payload.
If the user doesn't choose a specific sound, you can send a default sound file name in the notification payload.

How don't allow iOS to start application when specific type of push notification arrives?

I want PHP server send to my iOS application two types of push notifications:
New income message. For this push I want iOS start my application if it was suspended, show badge, play sound, etc.
New friend request. I don't want this push to start my application and I only want to handle if the app is in the foreground.
How can I achieve this? How can I handle different push notifications differently?
I'm not sure if it will work, but you should try for your 2nd scenario to send a notification that contains only custom properties. In this case there will be no alert to display, sound to play nor badge to update, so I think this notification will only reach your app if it's already running.
For the 1st scenario, send a notification with pre-defined properties (alert, sound, badge).
Application-side handling for remote notifications should start with the method in the application delegate protocol application:didReceiveRemoteNotification:.
However, in order to avoid the application launching in the first place, you need to make sure the PHP server crafted notification doesn't offer the option to launch the application.
See the documentation on the Apple Push Notification Service here:
http://developer.apple.com/library/ios/#DOCUMENTATION/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW9
In particular, you want to focus on the content of the aps dictionary as documented in The Notification Payload section. The aps dictionary received can badge your application's icon without opening the app at all.

Resources