1
votes

I am doing push notification in my project through GCM. My Application is able to receive notification in foreground but not in background.

I receive a message inside the method

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])  

when the application is in foreground but I am not getting any call to the method

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

when the application in background mode.

I had a long search in Internet and came to know that it's the problem with the payload format I receive. The payload I received looks like

[notification: {"body":"anything","title":"any title"}, priority: high, content_available: true, to: kcF23gblKok...., collapse_key: do_not_collapse, from: 7812....]

Can anyone suggest me the correct format of payload?

3
If a push arrives when the application is not running, no code is executed. See stackoverflow.com/questions/11153631/…Ozgur Vatansever
My exact problem is i am not at all receiving any message when the app enters in to background mode. In foreground mode all messages are receiving successfully .SAJITH
Did you turn on the background mode capabilities?mmtootmm
Try this link its very helpfull for you stackoverflow.com/questions/31109514/…Nishant Gupta
possible duplicate. Making GCM work in the background for iOS device has been resolved in this threadnoogui

3 Answers

4
votes

use this payload

{
    "aps": {
        "alert": "Hello World",
        "sound": "default"
        "content-available" :1
    }
}

With content-available enabled:

1 App is in Foreground

No system alert shown

application:didReceiveRemoteNotification:fetchCompletionHandler: is called

2 App is in Background

System alert is shown

application:didReceiveRemoteNotification:fetchCompletionHandler: is called

3App is in Suspended

App state changes to Background

System alert is shown

application:didReceiveRemoteNotification:fetchCompletionHandler: is called

4 App is Not Running because killed by user

System alert is shown

No callback is called

1
votes

For ones who are dealing with Pushy instead of GSM, pushy's completion handler might not get called when app is in background because of this:

Even though you configure notification payload with propriate keys and values such as for example:

{"to":"device***Token", "data": {"message": "Hello World!"}, "notification": {"title": "test", "body": "my message"}, "content_available": true}

and send it using Pushy's Console, it happens that all these data are placed in pushy's site field: 'NOTIFICATION DATA'. So using Console we found no way to send: true, for key: "content_available" which is necceserry to involve handler when app is in background.

You can get out of this by using Postman for instance, configuring your request as this:

  1. type: POST;
  2. raw;
  3. url: https://api.pushy.me/push?api_key=YOUR_APP_API_KEY;
  4. Content-Type: application/json;

And in body place something you need to send, for example:

 {"data":{"message": "Hello World!"},"tokens":["device***Token"],"content_available": true}

With this, you'll place "content_available" key inside "aps" and not inside "data", which will call your handler while app is in background.

0
votes

Create notification in this pattern

{
    "to": "ID",
    "notification": {
        "sound": "default",
        "title": "TITLE",
        "body": "BODY"
    },
    "priority": "high"
}