在Android的C2DM推送通知 [英] Push Notification in android c2dm

查看:109
本文介绍了在Android的C2DM推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我'开发在Android中使用C2DM样本推送通知的应用程序。这里是我的PHP code从服务器发送信息到设备。

I'am developing a sample push notification app in android using c2dm. Here is my PHP code to send the message from server to device.

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");

但我'提示无信息。在我的模拟器通知。凡我'的问题呢?请帮我。

But i'am getting "No info." notification on my emulator. Where i'am going wrong ? Please help me.

推荐答案

$ CH = curl_init();结果
    curl_setopt($ CH,CURLOPT_URL,https://android.apis.google.com/c2dm/send);结果
    curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真实);结果
    curl_setopt($ CH,CURLOPT_POST,1);结果,
    curl_setopt($ CH,CURLOPT_POSTFIELDS,$数据);结果
    curl_setopt($ CH,CURLOPT_HTTPHEADER,阵列(授权:的GoogleLogin AUTH = $标记,内容长度:$ LEN,内容类型:应用程序/ x-WWW的形式urlen codeD));结果
    回声curl_exec($ CH);结果
    curl_close($ CH);

这是我的应用程序使用其中$数据是你的数据数组发送C2DM消息PHP code。请注意,内容长度是必要的,是你的数据的长度。

This is the php code that my app uses to send C2DM messages where $data is your data array. Please note that the Content-Length is necessary and is is the length of your data.

编辑:有些事情,你也可以找到有用的一个为PHP,使得发送消息的更好一点。

Something you may also find useful a class for php that makes sending messages a little nicer.

这篇关于在Android的C2DM推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆