使用php的gcm推送通知出错

时间:2015-01-21 10:29:48

标签: php push-notification google-cloud-messaging

当我使用gcm推送通知时,

Field"data"mustbeaJSONarray: {
    "details": [
        {
            "regid": "APA91bH8zxTxfoSLWhE21IbTR9a10cvIcm17-zsPY_0OAy3JhO_8gTwMwwAUd_4vZiLu5UF4A1m8R3TEIffDaChVR0y2us9iebngkaWlOM34ix4PUeOgIoM9aGOcxLLECAGjKNSwupTY0p2O0BeXjSCp8RYFaD-xzg",
            "status": "true",
            "post_userid": "34",
            "post_id": "7",
            "postuser_name": "dev",
            "message": "gggggg"
        }]}

并且给我错误:“字段数据必须是JSON数组”

请指导我

我的代码:

$fields = array('data'=> $message); 
$headers = array('Authorization: key=' . API_ACCESS_KEY, 'Content-Type: application/json' ); 
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, '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 :(得分:1)

好吧,你的JSON看起来不像它应该看起来。 它应该是这样的:

{
  "data": {
        "status": "true",
        "post_userid": "34",
        "post_id": "7",
        "postuser_name": "dev",
        "message": "gggggg"
  },
  "registration_ids":["APA91bH8zxTxfoSLWhE21IbTR9a10cvIcm17-zsPY_0OAy3JhO_8gTwMwwAUd_4vZiLu5UF4A1m8R3TEIffDaChVR0y2us9iebngkaWlOM34ix4PUeOgIoM9aGOcxLLECAGjKNSwupTY0p2O0BeXjSCp8RYFaD-xzg"]
}