任何在 Android 上使用 C2DM 的人

发布于 2024-09-28 21:45:48 字数 94 浏览 0 评论 0原文

我需要在我的应用程序中实现 c2dm。有没有人也在做这个?请帮忙..一些教程会非常有帮助,或者如果您已经完成了 c2dm 实现,那么教程将非常有用。

请帮忙。

I need to implement c2dm in my app. Is there anyone who is also doing this? Please help..some tutorials will be very helpful OR if you have completed your c2dm implementation then a tutorial is more than appreciated.

Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-10-05 21:45:48

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

从以下位置下载:
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 post请求。

从服务器注册:

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

您将收到一条带有身份验证 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

从以下位置下载curl:
CURL

希望这有帮助。

I went ahead and downloaded the Chrome2Phone source code for android and understood how it works through that example, I had the most trouble implementing the server side of the App.

Download it from:
http://code.google.com/p/chrometophone/source/checkout

or svn it:

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

Basic things you should understand.

In the C2DMBaseReciever class you have:

@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();
        }
    }

This method recieves the intents from the C2DM service and handles them.

In the handleRegistration method you will see some code that looks like:

} 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());
            }
        }

You then have to use the google oAuth login service to register your server to the service, once that is done you can send a message. When I was testing I was using curl to send http post requests to the server.

To register from the server:

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

You will get a message with an auth id. You then use that to send the messages. To send a message use:

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

Download curl from:
CURL

Hope this helps.

猫弦 2024-10-05 21:45:48

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

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

  • 意图请求注册id
  • 接收者接收注册id
  • 调用注册服务器
  • url 调用发送消息
  • 广播接收者接收消息

a tutorial about c2dm client/server registration and sending/receiving of messages.

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

  • intent to request a registration id
  • receiver to receive registration id
  • url to call for registering the server
  • url to call for sending a message
  • broadcast receiver to receive the message
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文