简体   繁体   中英

Push notification not receiving in background iOS

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?

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

Create notification in this pattern

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM