IOS App implementing Google Cloud Messaging receives notifications only when active

354 Views Asked by At

I have implemented in my IOS app the new Google Cloud Messaging framework for handle push notifications. After implementation I'm able to receive push notifications only when App is active and in foreground. If App is closed or in background I didn't get the notification alert in my device. In the iOS notifications settings I see my app enabled to receive them.

2

There are 2 best solutions below

0
On BEST ANSWER

I was having a similar problem where the app would receive a notification only if the app was running (foreground/background) and wouldn't receive anything if app was killed manually (terminated)

All I had to do to fix that was to add priority & content_available to the notification body when sending it (as @KayAnn pointed out)

Here's how it should look like :

{
  "to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
  "content_available" : true,
  "priority": "high",
  "notification" : {
    "body" : "Winter is coming!",
    "title" : "John Snow"
    }
}

Keep in mind that I also have UIBackgroundModes: remote-notification in Info.plist.

3
On

Google documentations (in step 4) quotes:

If the GCMExample application is running in the foreground, you can see the content of the notification printed to the Xcode debug console; if the GCMExample app is in the background, you will receive an APNS notification.

So in order to receive messages when the app is in background you have to register APNS as options as below as described here.

_registrationOptions = @ {
  kGGLInstanceIDRegisterAPNSOption: deviceToken,
  kGGLInstanceIDAPNSServerTypeSandboxOption: @YES
};

EDIT:

There are few other things you need to do:
1) While publishing the app move to production certificate

In the JSON Payload:
2) Use "content_available": "true" OR
3) Set the "priority": "high"

EDIT:2

Using content_available and priority high in the same payload conflicts with Apple's recommendation (Apple docs). I have come across this during my testing. In such a scenario the message may be throttled.

Use either / or of these two parameters instead.
A Tip on working use-cases:
- content_available: use when you are sending data only and do not have other notification specific parameters like alert, badge, sound etc. This is because by default a content_available message is a silent push.
- Priority: high: Use it when you are sending a notification which should reach the users immediately, i.e time critical notifications such as game scores.