3
votes

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.

4

4 Answers

0
votes

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

Making GCM work for iOS device in the background

0
votes

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.

0
votes

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;
}
0
votes

This notification worked for me

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