Google云端消息传递 - 示例服务器

时间:2012-07-13 04:34:39

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

我需要一个用于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");
    }
}


}

4 个答案:

答案 0 :(得分:23)

您需要通过Android SDK下载。转到 Window-> Android SDK Manager 。向下滚动到额外内容并选中“Google Cloud Messaging”并安装。

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

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

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

答案 1 :(得分:6)

"卷曲"命令行工具可用于向使用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

答案 2 :(得分:5)

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

答案 3 :(得分:4)

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

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

screenshot

相关问题