任何在Android上进行C2DM的人

时间:2010-10-25 12:12:16

标签: android android-c2dm

我需要在我的应用中实现c2dm。还有谁也这样做?请帮助..一些教程将非常有用或者如果您已经完成了c2dm实现,那么我们非常感谢教程。

请帮忙。

2 个答案:

答案 0 :(得分:26)

我继续下载了Android的Chrome2Phone源代码并通过该示例了解了它的工作原理,我在实现App的服务器端时遇到了最大的麻烦。

从以下网址下载: http://code.google.com/p/chrometophone/source/checkout

或svn it:

svn checkout http://chrometophone.googlecode.com/svn/trunk/ chrometophone-read-only

你应该了解的基本事项。

在C2DMBaseReciever类中,您有:

@Override
    public final void onHandleIntent(Intent intent) {
        try {
            Context context = getApplicationContext();
            if (intent.getAction().equals(REGISTRATION_CALLBACK_INTENT)) {
                handleRegistration(context, intent);
            } else if (intent.getAction().equals(C2DM_INTENT)) {
                onMessage(context, intent);
            } else if (intent.getAction().equals(C2DM_RETRY)) {
                C2DMessaging.register(context, senderId);
            }
        } finally {
            //  Release the power lock, so phone can get back to sleep.
            // The lock is reference counted by default, so multiple 
            // messages are ok.

            // If the onMessage() needs to spawn a thread or do something else,
            // it should use it's own lock.
            mWakeLock.release();
        }
    }

此方法接收来自C2DM服务的意图并处理它们。

在handleRegistration方法中,您将看到一些看起来像的代码:

} else {
            try {
                onRegistrered(context, registrationId);
                C2DMessaging.setRegistrationId(context, registrationId);
                //Add some code here to send your server the registration ID for this phone.
            } catch (IOException ex) {
                Log.e(TAG, "Registration error " + ex.getMessage());
            }
        }

然后,您必须使用google oAuth登录服务将服务器注册到服务,一旦完成,您就可以发送消息。当我测试时,我使用curl将http发送请求发送到服务器。

从服务器注册:

curl https://www.google.com/accounts/ClientLogin -d Email=theEmailYouWhitelisted -d Passwd=pass****word -d accountType=HOSTED_OR_GOOGLE -d source=Google-cURL-Example -d service=ac2dm

您将收到一封带有身份验证码的邮件。然后使用它来发送消息。要发送消息,请使用:

curl --header "Authorization: GoogleLogin auth=**authFromRegistrationAbove**" "https://android.apis.google.com/c2dm/send" -d registration_id=**phoneRegistrationId(reciever)** -d "data.message=StringToPass" -d collapse_key=something -k

从以下网址下载curl: CURL

希望这有帮助。

答案 1 :(得分:1)

有关c2dm客户端/服务器注册和消息发送/接收的教程。

http://android.arnodenhond.com/tutorials/cloud-to-device-messaging

  • 意图申请注册ID
  • 收件人接收注册ID
  • 要求注册服务器的网址
  • 要求发送消息的网址
  • 广播接收器以接收消息