GCM使用PHP推动主题 [英] GCM push to topics using PHP
问题描述
我正在尝试编写一个PHP脚本,它会使用主题方法。它似乎是成功的并返回一个消息ID,但没有任何东西会显示在手机上。我确实有一个类似的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(数据)
打印dataAsJSON
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'=>数组(
缺少 i
。应该是通知
。
经典和容易错过(lol)。在我的结尾,之后能够显示通知。
I'm trying to write a PHP script that will send a push notification to my android app using the topics method. It seems to be successful and returns a message ID, but nothing will show up on the phone. I do have a similar python script, and that one works so GCM must have been implemented correctly in the app.
Not working PHP script
$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;
?>
Working python script
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()
I figured it out. I copied your whole PHP script and tested it on my end. My onMessageReceived()
was being triggered, but I noticed that the details wasn't retrieved, same as your scenario.
It was simply missed. You misspelled notifcation
in your script:
'notifcation' => array(
It's missing an i
. It should be notification
.
Classic and easy to miss (lol). Tried it on my end, was able to show a notification afterwards.
这篇关于GCM使用PHP推动主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!