GCM 使用 PHP 推送主题

GCM push to topics using PHP

我正在尝试编写一个 PHP 脚本,该脚本将使用 topics method 向我的 android 应用程序发送推送通知。好像成功了returns一个message ID,但是phone什么也不会显示。我确实有一个类似的 python 脚本,并且该脚本有效,因此 GCM 必须已在应用程序中正确实施。

不工作PHP脚本

$msg = array(
    'to'          => '/topics/my_little_topic',
    'notifcation' => array(
                            'body'      => 'here is a message message',
                            'title'     => 'This is a title title',
                            'icon'      => "ic_launcher"
    )
);

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

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://gcm-http.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( $msg, JSON_UNESCAPED_SLASHES) );
$result = curl_exec($ch );
curl_close( $ch );

echo $result;

?>

工作 python 脚本

from urllib2 import *
import urllib
import json
import sys

MY_API_KEY="AIzaSyBh...aWIVA"

messageTitle = sys.argv[1]
messageBody = sys.argv[2]

data={
    "to" : "/topics/my_little_topic",
    "notification" : {
        "body" : messageBody,
        "title" : messageTitle,
        "icon" : "ic_launcher"
    }
}

dataAsJSON = json.dumps(data)

print dataAsJSON

request = Request(
    "https://gcm-http.googleapis.com/gcm/send",
    dataAsJSON,
    { "Authorization" : "key="+MY_API_KEY,
      "Content-type" : "application/json"
    }
)

print urlopen(request).read()

我明白了。我复制了您的整个 PHP 脚本并在我这边进行了测试。我的 onMessageReceived() 被触发了,但我注意到没有检索到详细信息,与您的情况相同。

简直是错过了。您在脚本中拼错了 notifcation

'notifcation' => array(

缺少 i。应该是 notification.

经典又容易错过(笑)。我这边试过了,之后可以显示通知。