5

I'm trying to send push notifications to my iOS app with GCM. The app doesn't get the notification when it's in the background but it does when it's in the foreground. I was testing the push notifications with a PHP script also which sends the message directly to the APNS and it's working in the background.

The JSON sent to GCM: (I'm sending it from a rest client for testing)

{
  "to" : "token...",
  "notification" : {
    "title": "GCM TITLE",
    "body" : "FROM GCM",
    "badge": "1",
    "sound": "default"
  }
}

Not working: The userInfo received from GCM in didReceiveRemoteNotification:

Notification received: [aps: {
    alert =     {
        body = "FROM GCM";
        title = "GCM TILE";
    };
    badge = 1;
    sound = default;
}, gcm.message_id: 123...]

Working: The userInfo received when sent from the PHP script (I also added the message_id to the JSON to see if that's the problem)

Notification received: [aps: {
    alert =     {
        body = "FROM PHP";
        title = "PHP TITLE";
    };
    badge = 2;
    sound = default;
}, gcm.message_id: 123...]

I tried adding content_available to the JSON with different combinations but didn't help, the Content-Type and Authorization request headers are also set:

Content-Type:application/json
Authorization:key=... 
5
  • may or may not work, i had a similar issue, try removing the title from the JSON
    – Rob85
    Oct 17, 2015 at 22:17
  • thanks for the advice! unfortunately it still doesn't work
    – barna941
    Oct 17, 2015 at 22:25
  • do you get a success back from the GCM? what environment are you testing, have you archived the app or testing straight to the device?
    – Rob85
    Oct 17, 2015 at 22:27
  • Yes I get that the message was delivered successfully, I'm testing it on my device. But now it seems to be working, I set the priority to "high" in the JSON and now I get the notifications in the background!
    – barna941
    Oct 17, 2015 at 22:37
  • mmm GCM for you, took me a while to get it fully working, i have it in a few applications but always seems a bit hit or miss with APNS. Anyway glad you got it working
    – Rob85
    Oct 17, 2015 at 22:40

2 Answers 2

8

In case if someone has the same problem, the solution was for me to add the "priority": "high" to the JSON. This way I get the notifications in the background.

{
  "to" : "token...",
  "priority": "high",
  "notification" : {
    "title": "GCM TITLE",
    "body" : "FROM GCM",
    "badge": "1",
    "sound": "default"
  }
}
1

To get notification when app is in background i note that we need to add:

"content_available":true // when app in background
"priority":"high" //when app is completely closed not even running in background

 // "content_available":true  ..most important field 

{
"to" : "token...",
 "priority":"high",
 "content_available":true,
 "notification" : {
 "title": "GCM TITLE",
 "body" : "FROM GCM",
 "badge": "1",
 "sound": "default"
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.