通过PHP发送GCM不会显示onMessageReceived数据

时间:2016-05-07 11:45:13

标签: php android curl google-cloud-messaging

我开始在Android设备上使用GCM当我通过CURL和PHP将数据从我的服务器发送到我的Android应用程序上的GCM时只在onMessageReceived上获取null我认为这是我在应用程序上的代码但是如果我打电话通过终端卷曲它显示数据希望你可以帮助我使用我的PHP脚本来查看这有什么问题,顺便说一句,我得到了GCM的请愿,但只通过终端显示数据

<?php
// Message to be sent


// Set POST variables
$url = 'https://android.googleapis.com/gcm/send';

$fields = array(
                'to'  => 'ezgWcLrhacM:APA91bHzxeKdOnK.......gBfmpF3YrqOkjuHii296Ie1TPEmeQi-B-g_DJg0EHq89V0........7DCh3bzuHDOpN1Ksmp1n_EpkZvKrl56rrWB7M5Z8I',
                'data '  => array( 'message' => 'Hello World!' ),
                );

$headers = array( 
                    'Content-Type: application/json',
                    'Authorization: key=AIzaSyBBgVZuc...........DnZeDY4OZr_uyI' 

                );



// Open connection
$ch = curl_init();

// Set the url, number of POST vars, POST data
curl_setopt( $ch, CURLOPT_URL, $url );

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( $fields ) );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//echo $ch;
//$info = curl_getinfo($ch);



// Execute post
echo json_encode($fields);
$result = curl_exec($ch);

// // Close connection
curl_close($ch);

 echo $result;

?>

和来自android终端的日志

05-07 06:34:48.694 14037-14239/mx.com.iubix.iubixmessage D/ListenerService: Datos recibidosBundle[{collapse_key=do_not_collapse}] 
05-07 06:34:48.694 14037-14239/mx.com.iubix.iubixmessage D/ListenerService: From: 1007097169676 
05-07 06:34:48.694 14037-14239/mx.com.iubix.iubixmessage D/ListenerService: Message: null

我从终端发送的代码在哪里

curl --header "Authorization: key=AIzaSyBBgVZ........eDY4OZr_uyI" --header "Content-Typ.....e: application/json" https://android.googleapis.com/gcm/send -d "{\"to\":\"ezgWcLrhacM:APA91bHzxeKdOnKgk.......ZxjEchBezgBfmpF3YrqOkjuHii296Ie1TPEmeQi-B-g_DJg0EHq89V0yllLGw9Yji0zv.......Ksmp1n_EpkZvKrl56rrWB7M5Z8I\",\"data\":{\"message\":\"Hello World\"}}"

这个工作正常

2 个答案:

答案 0 :(得分:0)

您可以尝试以下脚本:

<?php 
define( 'API_ACCESS_KEY','YOUR API KEY' );

$registrationIds = array('ID's OF WHOM YOU WANT TO SEND NOTIFICATION');

$msg = array
(
    'Message'   => $text,

); 

$fields = array
(
    'registration_ids'  => $registrationIds,
    'data'          => $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;
?>

答案 1 :(得分:0)

对不起,伙计们已经在数据参数中解决了这个问题,我有一个额外的空间并将其移除并完美运行!抱歉给你带来不便 !无论如何,谢谢大家

'data ' 

更改此内容

 'data'