【问题标题】:GCM PHP : Field "data" must be a JSON arrayGCM PHP:字段“数据”必须是 JSON 数组
【发布时间】:2016-03-12 17:34:59
【问题描述】:

我知道这里有几篇帖子讨论了我的问题,但没有一个对我有用。它要么提到不完整的解决方案,要么保持原样。 我的查询: 我得到了

Field "data" must be a JSON array: [{"sound":1,"vibrate":1,"message":"Push Notification Message","title":"Push Notification Title"}]

尝试使用 GCM 时出错。我的 regID、发件人 ID 和服务器密钥看起来不错。 可能知道如何解决这个问题。

PHP 代码:

<?php
$to="";
if($_GET['id']){
$to = $_GET['id'];
}
$title="Push Notification Title";
$message="Push Notification Message";
sendPush($to,$title,$message);

function sendPush($to,$title,$message)
{
// API access key from Google API's Console
// replace API
define( 'API_ACCESS_KEY', 'API_HIDDEN');
$registrationIds = array($to);

$msg = array
(
'message' => $message,
'title' => $title,
'vibrate' => 1,
'sound' => 1

// you can also add images, additionalData
);
$fields = array
(
'registration_ids' => $registrationIds,
'data' => array($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;

}
?>

【问题讨论】:

    标签: php android cordova google-cloud-messaging phonegap-pushplugin


    【解决方案1】:

    换行

    $message="Push Notification Message";
    

    通过

    $message=array( 'response' =>  json_encode("Push Notification Message"));
    

    GCM 需要JSONArray 格式的响应数据。

    或者使用下面的方法

    function sendGoogleCloudMessage( $data, $ids ) {
        $apiKey = 'YOUR_KEY';
        $url = 'https://android.googleapis.com/gcm/send';
        $post = array(
                        'registration_ids'  => $ids,
                        'data'              => $data,
                        );
        $headers = array( 
                            'Authorization: key=' . $apiKey,
                            'Content-Type: application/json'
                        );
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_URL, $url );
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
        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( $post ) );
        $result = curl_exec( $ch );
        if ( curl_errno( $ch ) ) {
            $result = $result . 'GCM error: ' . curl_error( $ch );
        }
        curl_close( $ch );
        return $result;
    }
    

    并使用它调用它

    $response = array();
    $response["title"] = "title";
    $response["message"] = "Notification message.";
    $data = array( 'response' =>  json_encode($response));
    $result = sendGoogleCloudMessage($data, $ids);
    

    它在我的应用程序中运行。

    希望对你有所帮助。

    【讨论】:

    • $message 只是请求中的属性值 .. 与标题相同...您能否在此处确认...请求数据已经在数组中 ..$msg = array ( 'message ' => $message, 'title' => $title, 'vibrate' => 1, 'sound' => 1 // 也可以添加图片,additionalData );
    • 对我来说看起来很奇怪..它仍然表现相同..将尝试再次挖掘..无论如何感谢您的帮助:)
    • 我尝试了 json_encode 和数组之间的各种组合。它要么给出无效注册,要么字段数据必须是 json 数组错误。将代码更改为更新后的帖子后,它给出了无效注册错误。这一行[[[$data = array('response' => json_encode($response));]] 给出无效的注册错误。如果我将其更改为 [[[$data = array($response)]]] 它会给出 json 字段数组错误 .. 过去 2 天卡住了 :(
    • 还有什么可以弥补的吗?
    • 我在本地 php 文件中运行,当我发现时,我在请求中附加了双引号。这个引号与 Reg.ID 不匹配 .. .. 带着愚蠢的错误度过了我的周末.. :)
    猜你喜欢
    • 2023-04-11
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多