0

Than app received the push and the app is not running I need to call a network method to my API. I need to do it silently without any interactions from user(like clicking notification or someting else - without it)

I set the content-available key in your push payload to 1.

How can I do it?

I implemented

 func application( application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {

And as a test I added

  UIApplication.sharedApplication().applicationIconBadgeNumber = 20

But looks like it is not working because then I close my app and send a test notification , the bagde number is not 20

3 Answers3

0

It's because your app is not running. When the app is in background didReceiveRemoteNotification can be call. If your app was quit/kill, there is no way to do something silently (except updating the badge, to do so, you'll need to send the badge number directly into the payload of your notification).

RomOne
  • 2,065
  • 17
  • 29
0

If you want to update the badge of the app without launching the app, you need to send it in push notification from your server. See the Apple documentation: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/TheNotificationPayload.html

The method you have implemented is only called when app is running. But if you include "badge" parameter in your push payload iOS will automatically set it for you.

Heisenbug
  • 951
  • 6
  • 25
0

If you need to trigger an API call that downloads content from your server, you need to set the content-available key in your push payload to 1.

Per the Using Push Notifications to Initiate a Download in Apple's documentation:

If your server sends push notifications to a user’s device when new content is available for your app, you can ask the system to run your app in the background so that it can begin downloading the new content right away. The intent of this background mode is to minimize the amount of time that elapses between when a user sees a push notification and when your app is able to able to display the associated content. 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.

To support this background mode, enable the Remote notifications option from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the remote-notification value in your app’s Info.plist file.)

For a push notification to trigger a download operation, the notification’s payload must include the content-available key with its value set to 1. When that key is present, the system wakes the app in the background (or launches it into the background) and calls the app delegate’s application:didReceiveRemoteNotification:fetchCompletionHandler: method. Your implementation of that method should download the relevant content and integrate it into your app.

JAB
  • 3,165
  • 16
  • 29
  • If you set it to 1, then the app will wake up in the background even if it is not running and call the didReceiveRemoteNotification method. You can make your API call and store your new data from that method. Hope that helps, you're on the right track! – JAB Oct 11 '16 at 23:48