C2DM:如何接收设备中的信息? (使用PHP)设备、信息、C2DM、PHP

2023-09-08 09:45:20 作者:你转身向北,侧脸还是很美

我的注册ID和身份验证令牌的C2DM。然后,我通过在数据库存储这些值。并使用PHP,我可以把一个消息给C2DM服务器。但我的问题是我不知道如何接收消息的应用程序。我不知道我得到消息的方式是否正确。无论如何,我都会给它下面。

I have the registration id and auth token for c2dm. And then I pass store these values in db. and using php, i could send one message to c2dm server. But my problem is I dont know how to receive the message in the application. I am not sure whether my way of getting the message is correct or not. Anyway i will give it below.

我有一个活动而注册使用登记意向的C2DM。和一个接收器接收所述reg_id和通知消息。它与注册C2DM和不接收消息。

I have one activity which registers to the c2dm using registration intent. and one receiver to receive the reg_id and notification message. it is registering with c2dm and not to receive message.

                                                                                      

    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.REGISTRATION"></action>
        <category android:name="my.android.c2dm"></category> 
    </intent-filter>
    <intent-filter>
        <action android:name="com.google.android.c2dm.intent.RECEIVE"></action>
        <category android:name="my.android.c2dm"></category>
    </intent-filter>
    </receiver>

</application>

C2dmRegistration.class(活性)

    Intent objRegIntnet=new Intent("com.google.android.c2dm.intent.REGISTER");
    objRegIntnet.putExtra("app",PendingIntent.getBroadcast(this,0,new Intent(),0));
    objRegIntnet.putExtra("sender","mymail@gmail.com");
    startService(objRegIntnet);

c2dmReceiver

public class c2dmReceiver extends BroadcastReceiver 
{
    private static String KEY = "c2dmPref";
    private static String REGISTRATION_KEY = "registrationKey";


    private Context context;



    @Override
    public void onReceive(Context context, Intent intent)
    {
        this.context = context;
        if (intent.getAction().equals("com.google.android.c2dm.intent.REGISTRATION")) 
        {
                  handleRegistration(context, intent);
             } 
        else if (intent.getAction().equals("com.google.android.c2dm.intent.RECEIVE"))
        {
            handleMessage(context, intent);
             }              
    }

     private void handleRegistration(Context context, Intent intent) 
{
       //handles registeration
     }
     private void handleMessage(Context context, Intent intent)
{

    String title= intent.getStringExtra("title");
    String message= intent.getStringExtra("msg");
    Toast.makeText(context,"title : "+title+"\n message : "+message,1).show();
    //Do whatever you want with the message
}

请告诉什么是错误,我做了...

please tell what is the mistake i have done...

大家好,同样的code今天回环我。这个错误我所做的是用php code。传递值作为POST的instaed,我把它作为是GET。当我改成了POST,敬酒的消息显示。但还没有一点问题都没有。

Hi all, the same code is woring for me today. The mistake i have done is with php code. instaed of passing the values as POST, i sent it was as GET. When I changed it to POST, the toast message is showing. but yet some problems are there.

标题,和味精值为空在这里。 我的PHP code是:

The title, and msg values are null here. my php code is :

function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText)
 {
       //$messageText="have a nice day";
       //$msgtype="important";
       $headers = array('Authorization: GoogleLogin auth=' . $authCode);
       $data = array(
        'registration_id' => $deviceRegistrationId,
        'collapse_key' => $msgType,
        'data.message' => $messageText
                        );
      $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);
      return $response;
    }

其实我不知道应该用collapse_key什么类型的值,并data.message变量。

Actually i am not sure what type of values should use for collapse_key, and data.message variables.

请帮我... 谢谢你...

Please help me... Thank you...

推荐答案

最后,我发现给予collapse_key和和数据的方式.. collapse_key应该是一个字符串,它是一组消息或氨基酸parthicular类型的消息的名称。如果我们发送多于一个消息具有相同collapse_key,最新的消息将被从C2DM服务器发送到设备。

Finally I found the way of giving collapse_key and and data.. collapse_key should be a string which is a name for a group of messages or a a parthicular type of messages. If we send more than one message with same collapse_key, the latest message will be sent to the device from c2dm server.

例:$ collapse_key =重要;

Example : $collapse_key = "important";

和数据。是最重要的事情。这将包含我们要发送的邮件。

And the data. is the important thing. This will contain the message that we want to send.

例如:如果我们想发送一条消息有一个愉快的一天,那么我应该把它给一个键名。     。数据=有一个愉快的一天; 这里'希望'是关键。而在接收器,我应该retreive消息使用相同的密钥名。

Ex: if we want to send a message "Have a nice day", then i should give a key name to it. data.="Have a nice day"; here 'wishes' is the key. And in receiver, i should retreive the message with the same key name.

private void handleMessage(Context context, Intent intent) 
{  
      String mywish= intent.getStringExtra("wishes");    
      Toast.makeText(context,"my wishes : "+mywish,1).show(); 
} 

对不起,所有的..

Sorry to all..