简体   繁体   English

gcm消息中的空值

[英]Null value in gcm message

I'm trying to use a typical php backend using curl to send GCM push notifications to android devices.The php script is as follows : 我正在尝试使用使用curl的典型php后端将GCM推送通知发送到android设备。php脚本如下:

<?php
//request url
$url    = 'https://android.googleapis.com/gcm/send';
//your api key
$apiKey = 'AIzaSyB-1uEai2WiUapxCs2Q0GZYzPu7Udno5aA';
//registration ids
$registrationIDs = array('APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...');
//payload data
$data   = array('score' => '1-0', 'scorer' => 'Ronaldo', 'time' => '44');

$fields = array('registration_ids' => $registrationIDs,
            'data' => $data);

//http header
$headers = array('Authorization: key=' . $apiKey,
             'Content-Type: application/json');

//curl connection
$ch = curl_init();

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_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

$result = curl_exec($ch);

curl_close($ch);

echo $result;
?>  

I use a broadcast receiver in the client app : 我在客户端应用程序中使用广播接收器:

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String newMessage = intent.getExtras().getString("score");
        // Waking up mobile if it is sleeping
        WakeLocker.acquire(getApplicationContext());


        // Showing received message
        lblMessage.append(newMessage + "\n");           
        Toast.makeText(getApplicationContext(), " New Message: " + newMessage, Toast.LENGTH_LONG).show();

        // Releasing wake lock
        WakeLocker.release();
    }
};

From the result of the php script, I'm pretty sure that the json message is being sent properly. 从php脚本的结果来看,我很确定json消息已正确发送。 I also recive a notification in the registered device. 我还会在已注册的设备中收到通知。 But the thing is, I get a "null" message. 但问题是,我收到“空”消息。 As I'm just trying to print the string I receive, "null" gets printed. 当我只是尝试打印收到的字符串时,将打印“ null”。 Is there something wrong with this statement : String newMessage = intent.getExtras().getString("score"); 此语句是否有问题: String newMessage = intent.getExtras().getString("score"); ? Or should I decode the json in order to retrieve the message? 还是应该解码json以检索消息?

I think the problem is here 我认为问题出在这里

$fields = array('registration_ids' => $registrationIDs,
            'data' => $data);

you have to make it like this: 您必须像这样:

$fields = array('registration_ids' => $registrationIDs,
            'data.score' => $data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM