【问题标题】:Google Cloud messaging - Sample ServerGoogle Cloud 消息传递 - 示例服务器
【发布时间】:2012-07-12 22:22:26
【问题描述】:

我需要一个用于 Google Cloud 消息传递的示例应用程序。使用示例服务器来测试我的应用程序。有人可以帮我吗?

我需要一个示例服务器来测试我的代码,我已经编写了代码,但我不知道它是否可以工作。我不知道服务器端编码,所以任何人都可以帮助我。这是我的代码

意向服务

package com.example.pushnotificationsample;

import android.content.Context;

public class GCMIntentService extends GCMBaseIntentService {

protected GCMIntentService(String senderId) {
    super(senderId);
    // TODO Auto-generated constructor stub
}

@Override
protected void onError(Context arg0, String arg1) {
    // TODO Auto-generated method stub
}

@Override
protected void onMessage(Context arg0, Intent msgIntent) {
    // TODO Auto-generated method stub
    Log.d("GCM", "RECIEVED A MESSAGE");
  //        String msg=msgIntent.getStringExtra("Message");
    Log.d("GCM", msgIntent.toString());
    // Get the data from intent and send to notificaion bar

}

@Override
protected void onRegistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub
}

@Override
protected void onUnregistered(Context arg0, String arg1) {
    // TODO Auto-generated method stub
}
}

我的主要活动

package com.example.pushnotificationsample;

import android.app.Activity;
import com.google.android.gcm.GCMRegistrar;
import android.os.Bundle;
import android.util.Log;

public class MainActivity  extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GCMRegistrar.checkDevice(this);
   // GCMRegistrar.checkManifest(this);
    final String regId = GCMRegistrar.getRegistrationId(this);
    if (regId.equals("")) {
      GCMRegistrar.register(this, "555817657362");
      Log.v("Msg", "registered");
    } else {
      Log.v("Msg", "Already registered");
    }
}


}

【问题讨论】:

  • 亲爱的,如果您已经下载了“从 SDK 管理器中下载 GCM 包”,那么这里有客户端、服务器的示例。只需检查 android-sdk/extras/google/GCM 目录
  • @PareshMayani chat.stackoverflow.com/transcript/message/4508859#4508859 但无法在额外文件夹的 sdk 管理器中找到
  • 快速提问 - 在 register 方法中,发送者 ID(此处为:555817657362)是每个设备还是每个应用程序(独立于设备)?
  • @MasterGaurav 每个应用程序

标签: android google-cloud-messaging android-c2dm


【解决方案1】:

您需要通过 Android SDK 下载。转到窗口->Android SDK 管理器。向下滚动到 extra 并选中“Google Cloud Messaging”并安装。

完成后,您可以查看:android-sdk/extras/google/gcm/samples

或者你可以试试这个(我自己上传了):gcm

对于服务器端,请检查此答案:https://stackoverflow.com/a/11253231/554740

【讨论】:

    【解决方案2】:

    我们在 GitHub 上有一个示例客户端:https://github.com/indigorose/airbop-client(基于 GCM 客户端示例),它可以与我们基于 GCM 的服务 AirBop:http://www.airbop.com 一起使用,您可以免费进行测试。

    【讨论】:

      【解决方案3】:

      我在这里找到了一个适用于 Windows 的开源发件人客户端:https://gcm.codeplex.com/

      • 在您实现 GCM 注册代码并通过客户端应用检索您的注册 ID 后可以找到设备令牌(设置断点或打印语句以便您能够复制/粘贴此值,它很长)
      • 您在 Google 的开发者控制台中设置您的项目后会找到身份验证密钥

      【讨论】:

        【解决方案4】:

        “curl”命令行工具可用于向注册了 GCM 的设备发送消息。

        curl -X POST \
          -H "Authorization: key= <YOUR_AUTHORIZATION_KEY>" \
          -H "Content-Type: application/json" \
          -d '{
          "registration_ids": [
            "<YOUR_DEVICE_TOKEN>"
          ],
          "data": {
            "message": "<YOUR_MESSAGE>"
          }
        }' \
          https://android.googleapis.com/gcm/send
        

        有关详细信息,请参阅此博客文章。 http://www.zinniakhan.com/2014/07/check-google-cloud-messaging-gcm-client.html.

        【讨论】:

        • Stack Overflow 强烈反对仅链接的答案。相反,it is preferable 在此处包含答案的基本部分,并提供链接以供参考。
        • 对我来说就像一个魅力,请注意!!!不要将任何字符(需要分隔)“It's me, some dummy message”放入消息中;)因为它会给你带来很多痛苦。
        猜你喜欢
        • 1970-01-01
        • 2015-10-08
        • 2015-09-16
        • 1970-01-01
        • 2014-08-02
        • 2022-06-11
        • 1970-01-01
        • 2013-12-30
        • 2016-02-16
        相关资源
        最近更新 更多