我是 android 新手,我正在尝试在我的应用程序中开发推送通知。
我已经阅读了很多帖子并且能够生成设备注册密钥和身份验证密钥。
但是当我尝试向设备发送推送通知消息时,我收到了以下消息。
<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
以下是我用来发送消息的 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);
echo "<pre>";
print_r($response); die;
}
sendMessageToPhone($auth_token, $device_id , "UTF-8","hello");
我还启用了 emaid 的“Google Cloud Messaging for Android”服务,我曾使用它来生成身份验证令牌。