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...
}
Related
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
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.
According to Apple's official documentation, if one sends a "silent notification", i.e. a notification with content-available: 1 we have
For background download apps: A push notification arrives for an app
and the payload of the notification contains the content-available key
with a value of 1. The system wakes the app at opportunistic moments
to begin downloading new content. For apps downloading content in the
background using the NSURLSession class, all tasks associated with
that session object either completed successfully or received an
error. A download initiated by a Newsstand app finishes.
also it says in that document
Apps are typically woken up at roughly the same time that the user sees the notification but that still gives you more time than you might have otherwise.
How to interpret this? If an app is submitted with a UIBackgroundMode of remote-notification in the plist, does this mean that the app is actually launched in the background if we send a remote notification? Even if the user has restarted the iOS device, and didn't launch our app? Even if the user force-quit our app before?
My question is, how reliable is the above expectation? Has anyone had direct experience with and tested this, and it is true 100% of the time in your experience?
From Apple
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
iOS is gonna wake up your app and will give you up to 30 seconds to download whatever you need. Keep in mind that notifications are not reliable, so you can't trust they're gonna arrive 100% of the time. If you have really important data to show, you'd need to verify that your app is sync when user opens it. Background fetching is just to improve user experience
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.
I have an iOS app that needs to update its content while running in foreground automatically. My app does NOT need to update if in background.
There is a existing way to do so, which is APNS(Apple Push Notification Service).
Because I don't want users to see notification message while in background, using push notification without alert or message might be a solution.
However, if using APNS, iOS would ask users to confirm if they want to receive notifications by my app. I think that users may be confused when being asked by the OS since my app does not actually push notification to users.
The current method I use is keep pulling my API every 30 seconds to see if new content is available. This method would fail if there are too many users.
Is there any 3rd party push-notification-like service that provides notification while app runs in foreground only? (no need to get notification while in background)
You can use Silent notification for that, in this
In the WWDC 2013's "What's New with Multitasking" presentation, there is a section about Silent Push Notifications. if you send the APS payload with just the content-available set to 1, users will not be notified of the notification.
And the notification arrives in application:didReceiveRemoteNotification:fetchCompletionHandler:
Your payload is like
{
aps: {
content-available: 1,
sound: "default"
}
}
In case of push notification, it is necessary for user to accept push notification on application 1st run. You can set a silent push notification also and for this user will not get any alert of getting a notification during application run loop.
If you want to avoid push notification, then you can only set a NSTimer that you are doing already.
There can be a 3rd case, Application only sync with the server when it comes to foreground. And for this you can refer to my this post.