curl - Google Cloud Message API Notification Data doesn't appear - PHP

798

im sending notifications to my clients with PHP. Here is my PHP Code for this

<?php

define( 'API_ACCESS_KEY', 'AIzaSyAYq7KpB5J7x3p3zYyna...' );
$registrationId = array( $_GET['id'] );

$msg = array
(
    'message'   => 'here is a message. message',
    'title'     => 'This is a title. title',
    'subtitle'  => 'This is a subtitle. subtitle'
);
$fields = array
(
    'registration_ids'  => $registrationId,
    'notification'          => $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;

When i execute the script with my registration id there is notification like this, Notification

Title is Push message Body is The Message

As you can see, Notification title and message body is not equal with mines. How i can fix this ? Thank you.

502

Answer

Solution:

$msg should be a JSON object, as indicated in the Messaging Concepts and Options

{ "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...", "data" : { "Nick" : "Mario", "body" : "great match!", "Room" : "PortugalVSDenmark" }, }

For a more detailed discussion regarding Downstream Messaging, you can refer to its documentation page were it discusses various aspects of sending messages.

People are also looking for solutions to the problem: php - Iterating over generators with while

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.