【问题标题】:Error in gcm push notification with php使用 php 的 gcm 推送通知中的错误
【发布时间】:2015-01-21 10:29:47
【问题描述】:

当我使用 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;

【问题讨论】:

  • 请显示用于向 GCM 发送消息的代码。
  • $fields = array('data'=> $messsssagedddd); $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, 真); curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 真); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); $result = curl_exec($ch); curl_close($ch);回声 $result;
  • 我的代码是:$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, 真); curl_setopt($ch,CURLOPT_HTTPHEADER, $headers); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 真); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch,CURLOPT_POSTFIELDS, json_encode($fields)); $result = curl_exec($ch); curl_close($ch);回声 $result;

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


【解决方案1】:

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

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

【讨论】:

  • $fields = array('registration_ids'=>$msgd,array('data'=> $message));"registration_ids" 字段不是 JSON 数组
  • @DevangADD 然后将其设为 JSON 数组。我不知道 PHP,但也许像 $fields = array('registration_ids'=>array($msgd),array('data'=> $message)); 这样的东西会起作用。
  • 当我使用此代码时:$fields = array('registration_ids'=>$msgd,array('data'=> $message));它将显示此错误““registration_ids”字段不是 JSON 数组”
  • @DevangADD $msgd 包含什么?
  • 它是 regestration_id 变量
猜你喜欢
  • 2012-11-27
  • 2016-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 2012-06-30
  • 2017-06-04
相关资源
最近更新 更多