Phonegap Push插件在通过PHP发送消息时抛出401错误

时间:2014-09-17 09:55:30

标签: php cordova push-notification google-api-php-client phonegap-pushplugin

我试过这段代码。

function sendNotification( $apiKey, $registrationIdsArray, $messageData )
{   
    $headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
    $data = array(
        'data' => $messageData,
        'registration_ids' => $registrationIdsArray
    );

    $ch = curl_init();

    curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); 
    curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );

    $response = curl_exec($ch);
    curl_close($ch);

    return $response;
}

并调用此函数:

$message      = "the test message";
$tickerText   = "ticker text message";
$contentTitle = "content title";
$contentText  = "content body";

$registrationId = 'APA91bEgsAG3vmliDnJE7jfLAOGSUv3K9p41MkNranPFV4EY0svABRax8NY5oulOHv7s3v2Ks_bQutsLLw8j4mHOr5LkrRlFfXxfs3hxxwAlxIOG7cXCB4YPhlLCDspVtImyWBL_znGgkZzEWCncV3tidHMV'; (Id is wrong here for security reasons)
$apiKey = "AIzaSyD6kZoY3Qb_1ut57IEmwdRg0JuxC42W1"; (Key is wrong here for security reasons)

$response = sendNotification( 
                $apiKey, 
                array($registrationId), 
                array('message' => $message, 'tickerText' => $tickerText, 'contentTitle' => $contentTitle, "contentText" => $contentText) );

echo $response;

现在我被卡住了。我只是创建一个PHP页面,其中包含我自己的设备注册ID和谷歌API密钥。

但它显示我的错误: 擅自 错误401

当我运行此网址http://vbought.com/sendnotification.php

我甚至在GCM参考中添加了我的服务器IP和域名

.vbought.com /

*。vbought.com

50.87.3.82

有什么我做错了吗?或者我需要知道?我只是想向我唯一的设备发送一条消息。

谢谢! (提前)

1 个答案:

答案 0 :(得分:1)

我找到了答案。答案是我不需要在GCM中定义任何内容。就像我定义了我的域名和IP地址。所以我不需要做任何事情。把它留空,就会像魅力一样......

度过愉快的一天:)