gpt4 book ai didi

php - 在 android c2dm 中推送通知

转载 作者:行者123 更新时间:2023-11-30 04:20:19 25 4
gpt4 key购买 nike

我正在使用 c2dm 在 android 中开发一个示例推送通知应用程序。这是我的 PHP 代码,用于将消息从服​​务器发送到设备。

function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) {

$headers = array('Authorization: GoogleLogin auth=' . $authCode);
$data = array(
'registration_id' => $deviceRegistrationId,
'collapse_key' => $msgType,
'data.message' => $messageText //TODO Add more params with just simple data instead
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
if ($headers)
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


$response = curl_exec($ch);

curl_close($ch);

}
sendMessageToPhone("my application server auth token ","my device id","UTF-8","hello");

但我收到“无信息”。我的模拟器上的通知。我哪里错了?请帮助我。

最佳答案

$ch = curl_init();<br/>
curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");<br/>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br/>
curl_setopt($ch, CURLOPT_POST, 1);<br/>
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);<br/>
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: GoogleLogin auth=$token", "Content-Length: $len", "Content-Type: application/x-www-form-urlencoded"));<br/>
echo curl_exec($ch);<br/>
curl_close($ch);

这是我的应用程序用来发送 C2DM 消息的 php 代码,其中 $data 是您的数据数组。请注意 Content-Length 是必需的,它是您的数据的长度。

编辑:您可能还会发现有用的东西 class用于使消息发送更友好的 php。

关于php - 在 android c2dm 中推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9359884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com