简体   繁体   English

无法将C2DM消息发送到设备

[英]Not able to send C2DM message to device

I'm new to android and I'm trying to develop push notification in my app. 我是android新手,正在尝试在我的应用中开发推送通知。

I had read so many post and able to generate device registration key and authentication key. 我已经阅读了很多文章,并且能够生成设备注册密钥和身份验证密钥。

but when I'm trying to send push notification message to device i got following message. 但是,当我尝试将推送通知消息发送到设备时,出现了以下消息。

<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

and following is php code which I'm using to send message: 以下是我用来发送消息的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");

I had also enable "Google Cloud Messaging for Android" service by emaid which I had used to generate authentication token. 我还通过用来生成身份验证令牌的emaid启用了“ Android的Google Cloud Messaging”服务。

This is wrong way because you tried to work with G2DM. 这是错误的方法,因为您尝试使用G2DM。 G2DM is deprecated. 不推荐使用G2DM。

This is great tutorial for GCM. 这是GCM的绝佳教程。 You can set up the push notification in 30 minutes. 您可以在30分钟内设置推送通知。 http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/ http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM