2

I do know there are several posts here which talks about my issue but none works for me.Its either incomplete solution mentioned or left as is. My query: I am getting

Field "data" must be a JSON array: [{"sound":1,"vibrate":1,"message":"Push Notification Message","title":"Push Notification Title"}]

error when trying to use GCM. My regID, Sender ID, and server key looks fine. May know how to resolve this.

PHP Code:

<?php
$to="";
if($_GET['id']){
$to = $_GET['id'];
}
$title="Push Notification Title";
$message="Push Notification Message";
sendPush($to,$title,$message);

function sendPush($to,$title,$message)
{
// API access key from Google API's Console
// replace API
define( 'API_ACCESS_KEY', 'API_HIDDEN');
$registrationIds = array($to);

$msg = array
(
'message' => $message,
'title' => $title,
'vibrate' => 1,
'sound' => 1

// you can also add images, additionalData
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => array($msg),

);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );

curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;

}
?>
Anand
  • 47
  • 7

1 Answers1

1

Replace the line

$message="Push Notification Message";

by

$message=array( 'response' =>  json_encode("Push Notification Message"));

GCM requires the response data in JSONArray format.

OR Use below method

function sendGoogleCloudMessage( $data, $ids ) {
    $apiKey = 'YOUR_KEY';
    $url = 'https://android.googleapis.com/gcm/send';
    $post = array(
                    'registration_ids'  => $ids,
                    'data'              => $data,
                    );
    $headers = array( 
                        'Authorization: key=' . $apiKey,
                        'Content-Type: application/json'
                    );
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $post ) );
    $result = curl_exec( $ch );
    if ( curl_errno( $ch ) ) {
        $result = $result . 'GCM error: ' . curl_error( $ch );
    }
    curl_close( $ch );
    return $result;
}

and call it using

$response = array();
$response["title"] = "title";
$response["message"] = "Notification message.";
$data = array( 'response' =>  json_encode($response));
$result = sendGoogleCloudMessage($data, $ids);

It is working in my application.

Hope it'll help.

ELITE
  • 5,815
  • 3
  • 19
  • 29
  • $message is just the attribute value in request .. same goes with title ...Can you please confirm here ...Request data is already in an array ..$msg = array ( 'message' => $message, 'title' => $title, 'vibrate' => 1, 'sound' => 1 // you can also add images, additionalData ); – Anand Mar 12 '16 at 17:49
  • looks strange for me .. it still behaves the same .. will try to dig again .. anyways thanks for the help :) – Anand Mar 12 '16 at 18:07
  • i tried various combinations among json_encode and array .. It either gives invalid registration OR field data must be a json array error.After changing the code to your updated post, it gave invalid registration error.This line [[[$data = array( 'response' => json_encode($response));]] gives invalid registration error.If I change it to [[[$data = array( $response)]]] it gives json field array error .. Stuck from last 2 days :( – Anand Mar 12 '16 at 18:16
  • any other go for this to make up ? – Anand Mar 12 '16 at 18:39
  • is there any way to check the registered ID's in my google console account .. I mean i need to verify whether the registration ID really exists or not – Anand Mar 13 '16 at 11:35
  • I don't know about that. You can debug app and check if you're getting device id. – ELITE Mar 13 '16 at 11:38
  • I got that bro .. There was a little problem in the request which was done from PHP to GCM.. Thanks for the support .. Cheers ! – Anand Mar 13 '16 at 14:25
  • 1
    I am running in local php file and when I found, I was appending double quotes in the request.This quote mismatched with Reg.ID .. ... Took my weekend out with silly mistake .. :) – Anand Mar 13 '16 at 14:34