任何在Android上进行C2DM的人

mud*_*dit 10 android android-c2dm

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

请帮忙.

bli*_*uff 26

我继续下载了Android的Chrome2Phone源代码并通过该示例了解了它的工作原理,我最难实现App的服务器端.

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

或者它:

svn checkout http://chrometophone.googlecode.com/svn/trunk/ chrometophone-read-only
Run Code Online (Sandbox Code Playgroud)

你应该了解的基本事项.

在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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

此方法接收来自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());
            }
        }
Run Code Online (Sandbox Code Playgroud)

然后,您必须使用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
Run Code Online (Sandbox Code Playgroud)

您将收到带有auth id的消息.然后使用它来发送消息.要发送消息,请使用:

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
Run Code Online (Sandbox Code Playgroud)

curL下载 curl

希望这可以帮助.