As I can see in the APNS documentation, silent notifications are handled in didreceiveremotenotification if the app is not running, but they have an low priority. So sometimes my iOS application doesn't receive silent notifications.
Does iOS show non silent notifications, if the app is not running (not in foreground, not in background)? And will a non silent notification trigger the didreceiveremotenotification?
For non silent notifications,
didreceiveremotenotification will be triggered if the app is in active or inactive state. Not when terminated or suspended state.
In case of terminated or suspended state when user taps on notification app will be launched by calling didFinishLaunchingWithOptions and launchingOptions will have the payload as Dictionary.
In case you provide UNNotificationServiceExtension then iOS will call didReceive(_:withContentHandler:) on receiving the notification and you can use it to customize the content of a remote notification before it is delivered to the user.
read:https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension
In case you provide UNNotificationContentExtension then iOS will call the didReceive on receiving the notification and you can use it custom load notification content.
Read : https://developer.apple.com/documentation/usernotificationsui/unnotificationcontentextension
P.S:
Normal notifications can not be used as an alternative/work around to silent notification just because you cant use silent notification in app terminated state.
Silent notifications are intended for syncing the client app with the updated content available at server. As this can be done without the explicit user interaction silent notification can be used.
Silent notifications must contain content-available key and must not contain alert, sound, or badge keys.
read : https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CreatingtheNotificationPayload.html
Where as in case of normal notification, there is no way to hide notification banner/alert/sound unless the user setting on phone says so.
Related
I just took a look at Is Silent Remote Notifications possible if user has disabled push for the app?.
It basically says the Silent Notifications disregard notification settings for user. It then says:
Users still have the ability to switch off your app’s ability to
process a “silent push” by means of the “Background App Refresh”
control. Even though Apple Push Notification service (APNs) will
deliver a push marked “content-available” to your phone, the OS will
not wake up your app to receive it, effectively dropping it on the
floor.
This is confusing to me. I want to make silent notifications go out only while the app is open, to update state of the app while in use only. So I wouldn't care if background app refresh is off because I wouldn't need to "wake up [my] app to receive it".
Secondarily Silent push notifications only delivered if device is charging and/or app is foreground talks about needing to have your phone plugged in to receive these notifications.
Both questions are from iOS 8, which is quite a ways back. Do they hold up all this time?
My answers are based on observation and my work on the apple notification.
Before iOS 13
Silent notification is not received even if notification is force killed by the user even if the background app refresh is on. Silent push received in case of foreground, background or killed by iOS
After iOS 13
Silent notification are received always if background app refresh is on.But if background app refresh is off silent push received only in foreground and background case.
If you want to only send silent push in foreground or background you should not add background mode in capability in Xcode. So it will receive only when the application is in the foreground or background
A silent remote notification must include the key content-available: 1 which also launches a suspended app into background.
Is there a way to send a silent push notification that does not contain any user-visible information but also does not launch the app into background?
It does not work by simply removing the key content-available: 1, because a remote notification without the keys badge, alert or sound does not seem to be delivered to the client.
Apparently that's not possible.
A remote notification to be delivered has to contain:
at least one of the keys alert, sound, badge which makes it a user-visible notification or
the key content-available: 1 which makes it a silent notification and launches the app in background
If user does not give permission for notifications, didreceiveremotenotification is not called when a remote notification is received while the app is in the foreground. I do not want to use a silent notification because when the app is in the background and user allows notifications, I want the alert notification to appear. But when in the foreground, I want to handle the notification data myself, even if the user has opted out of notifications.
How can this be achieved?
EDIT: I see that device registration token is not created when user disables notifications. But how would silent notifications work then?
I have found the following SO answer: What is difference between remote notification and silent notification in iOS?
In short, to achieve what I wanted, I needed to enable Remote Notifications in the Bacground Modes of plist. Then I was able to register a token even if the user opted out of receiving notifications.
Push notification is not receiving when the app runs in foreground when "content-available":true in payload. But Notification is received when app is not running in foreground.
Also I have added "Required Background Modes" for Background fetch and Remote Notification. And I'm using didReceiveRemoteNotification:fetchCompletionHandler: method to handle push message which is calling when app is not running and but not calling only in foreground mode.
I want to download some contents from sever even the app is running in foreground using silent push notification. is there any other way to receive push notification in foreground mode with "content-available":true payload?
Thanks!
You have probably implemented didReceiveRemoteNotification:. Implement didReceiveRemoteNotification:fetchCompletionHandler: instead.
I want to clear my local notification in notification tray. For this to be
implemented, I am thinking to use the silent push notification. So I want
to confirm when the device receives it and which things I can do with it?
They can be used to inform the application of new content without having the user informed. Instead of displaying a notification alert, the application will be awakened in background (iOS does not automatically launch your app if the user has force-quit it) and application:didReceiveRemoteNotification:fetchCompletionHandler: will be called. You then have the opportunity to process any information transparently for the user :
Download some content
Synchronize some elements,
Inform the user directly within the application when he opens it back
Note that your time is limited to 30s.
To configure silent notifications
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.
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
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
When you send a silent push notification and if app is suspended then the system wakes up or launches your app and puts it into the background running state before calling the method but if the app is killed by user manually then it will not wakeup.
application:didReceiveRemoteNotification:fetchCompletionHandler:
This method is called when you send a silent push notification and your app has up to 30 seconds of wall-clock time to perform the download or any other kind of operation and call the specified completion handler block. If the handler is not called in time, your app will be suspended.
If you want to send a silent push notification then your notification payload should be like this :
{
"aps" = {
"content-available" : 1,
"sound" : ""
};
// You can add custom key-value pair here...
}