3

I have enabled the background modes in xcode and checked "Remote notifications".

Do I have to enable "Push Notifications" too? for GCM

here is the payload from my rest client

{
    "data": {


            "displayMessage": {
                "message": "Package delivered",

            }

    },
    "registration_ids": [
        "dgfdghdfghgfhhfjhgjghjghjhjghjghjghjghjghjghjhgggh"
    ],
    "content-available" : true,
    "priority": "high"
}

This works when the app is in foreground and does not work when the app is not in foreground.

Any help to get this GCM for ios working would be highly appreciated.

Eugene
  • 1,865
  • 3
  • 21
  • 24
  • 1
    Take a look at this question though http://stackoverflow.com/questions/33192145/gcm-push-notification-when-ios-app-is-in-the-background – mojo Nov 11 '15 at 10:47
  • Possible duplicate of [GCM Notifications not receiving when app is in background mode in iOS](http://stackoverflow.com/questions/34704736/gcm-notifications-not-receiving-when-app-is-in-background-mode-in-ios) – Yusuf K. Feb 12 '16 at 07:56

4 Answers4

0

I would suggest to check the format of the message as it is mentioned here :

Making GCM work for iOS device in the background

Community
  • 1
  • 1
Serhan Oztekin
  • 475
  • 7
  • 20
  • I have tried that before posting this question with no success. Can you think of anything else that I might be missing? – Santhosh Adari Oct 12 '15 at 19:30
  • i suppose it is related to the server side not client, can you provide some code to show how you make requests to GCM? @SanthoshAdari – Serhan Oztekin Oct 12 '15 at 19:52
  • I just user rest client called "Advanced Rest Client" for google chrome – Santhosh Adari Oct 12 '15 at 20:35
  • { "to": "dfgdfgdfgdfc:APA91bGAmf3utL-k3BaZtzVXYayceu6XfW28ewdsgdgdfgdfhgdfgdf86zxxVyi9sIvJqRBZxz18-6PzZxjDeX_Bdn-ZmXaeTslB8qCGsH4m79iUK1bueN2bzTpQ3tx", "data": { "sound": "default", "badge": "2", "title": "default", "body": "Test Push!" }, "content-available":true } – Santhosh Adari Oct 12 '15 at 20:36
  • i get Internal server error when i use "notification" instead of "data". with "data" i can open notification when app is active. – Santhosh Adari Oct 12 '15 at 20:40
  • You can use `notification` and `data` together. – ztan Oct 13 '15 at 18:16
0

Your key is wrong. Use content_available, not content-available.

Also, your JSON is invalid. Specifically, this:

"message": "Package delivered",

should not have a comma at the end.

Doug Richardson
  • 10,483
  • 6
  • 51
  • 77
  • content_available would always give me "InternalServerError". I have read the documentation and I agree that it asks us to use content_available for gcm. – Santhosh Adari Oct 15 '15 at 13:00
  • I'm using GCM to push to iOS devices, it's working for me. I'm using content_available. – Doug Richardson Oct 15 '15 at 16:27
  • can you let me know what rest client are you using? – Santhosh Adari Oct 15 '15 at 19:35
  • the response is always `{ multicast_id: 6973668141157543000 success: 0 failure: 1 canonical_ids: 0 results: [1] 0: { error: "InternalServerError" }- - }` when i use content_available. however when i use content-available it won't wake up the app. – Santhosh Adari Oct 15 '15 at 19:36
  • Is the HTTP response also a 5xx error? If so, the docs says to contact the android-gcm group to report the error. https://developers.google.com/cloud-messaging/http-server-ref The group URL is https://groups.google.com/forum/#!forum/android-gcm – Doug Richardson Oct 15 '15 at 20:51
  • Oh, you know what. You also have a syntax error in your JSON because you have a comma after "Package delivered". Remove the comma and try again. Updated answer to include this part. – Doug Richardson Oct 15 '15 at 20:57
  • Thanks for your help @Doug Richardson. It was just a copy paste error.my payload looks like this and still can't get it working.`{ "to": "lTgMVojkkjhjgkikkaxdFbeCp0ti6j8W9V3aPMs_bZ7nIhAzpu6jyGK-St2lb8ANdE9uuopopnH1Hyuuiuiuiuiui3wnKJHNDulehzZzkRRbt4MShCdJkTLo", "content_available":true, "data":{ "displayMessage":"Hello world" } }' – Santhosh Adari Oct 15 '15 at 22:06
0

This issue occurred from server side. Notification message body should be like this.

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "content_available" : "true"
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark"
    }
}

Here is the code that I have implemented and working fine.

<?php

// Message to send
$notificationTitle = "Notification title";
$notificationtBody  = "Notification arrived";

$registrationId = 'GCM_DEVICE_TOKEN';
$apiKey = "GCM_SERVER_KEY";

$response = sendNotification( 
                $apiKey, 
                $registrationId, 
                array(  
                    'sound' => "default", 
                    'badge' => 1, 
                    'body' => $notificationtBody,
                    'title' => $notificationTitle
                )
            );              

echo $response;


function sendNotification( $apiKey, $registrationId, $messageData )
{   
    $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
    $data = array(
        'to' => $registrationId,
        'content_available' => true,
        'notification' => $messageData
    );

    $ch = curl_init();

    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); 
    curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}
Narendra
  • 1
  • 1
0

This notification worked for me

{
  "to":"ID",
  "notification":{  
    "sound":"default",
    "title":"TITLE",
    "body":"BODY"
  },
  "priority": "high"
}
Yury
  • 6,044
  • 3
  • 19
  • 41