1

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
SAJITH
  • 21
  • 1
  • 8
  • If a push arrives when the application is not running, no code is executed. See http://stackoverflow.com/questions/11153631/increment-the-push-notification-badge-iphone – Ozgur Vatansever Apr 29 '16 at 05:05
  • 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 Apr 29 '16 at 05:10
  • Did you turn on the background mode capabilities? – mmtootmm Apr 29 '16 at 05:23
  • Try this link its very helpfull for you http://stackoverflow.com/questions/31109514/making-gcm-work-for-ios-device-in-the-background – Nishant Gupta Apr 29 '16 at 13:25
  • possible duplicate. Making GCM work in the background for iOS device has been resolved in this [thread](http://stackoverflow.com/questions/31109514/making-gcm-work-for-ios-device-in-the-background) – ReyAnthonyRenacia Apr 29 '16 at 13:58
  • https://stackoverflow.com/a/53391380/1522584 – Abhijith Nov 20 '18 at 13:17

3 Answers3

4

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

Rishil Patel
  • 1,977
  • 3
  • 14
  • 30
balkaran singh
  • 2,754
  • 1
  • 17
  • 32
1

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.

mark_dimitry
  • 81
  • 1
  • 2
0

Create notification in this pattern

{
    "to": "ID",
    "notification": {
        "sound": "default",
        "title": "TITLE",
        "body": "BODY"
    },
    "priority": "high"
}
miken32
  • 42,008
  • 16
  • 111
  • 154