如何在Android中使用GCM解析或实际获取推送消息
我正在使用Google()提供的示例实现一个GCM客户端,但是当我发送测试消息时,我不知道如何接收它 以下是我用来发送消息的代码:如何在Android中使用GCM解析或实际获取推送消息,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,我正在使用Google()提供的示例实现一个GCM客户端,但是当我发送测试消息时,我不知道如何接收它 以下是我用来发送消息的代码: String msg = ""; gcm = GoogleCloudMessaging.getInstance(BaseHeaderActivity.this); try { Bundle data = new Bundle(); data.putString("my_message", "Test
String msg = "";
gcm = GoogleCloudMessaging.getInstance(BaseHeaderActivity.this);
try {
Bundle data = new Bundle();
data.putString("my_message", "Test message");
data.putString("my_action", "com.google.android.gcm.demo.app.ECHO_NOW");
String id = Integer.toString(msgId.incrementAndGet());
gcm.send(ListCategoriesActivity.SENDER_ID + "@gcm.googleapis.com", id, data);
msg = "Sent message";
}
catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
data.putString("my_message", "Test message");
data.putString("my_action", "com.google.android.gcm.demo.app.ECHO_NOW");
消息似乎发送正常,然后GCMinentService的void onHandleIntent(Intent Intent)方法在我发送消息几秒钟后执行,但问题是,在Intent中的extras中没有像我在发送消息的方法中放置的键那样的键:
String msg = "";
gcm = GoogleCloudMessaging.getInstance(BaseHeaderActivity.this);
try {
Bundle data = new Bundle();
data.putString("my_message", "Test message");
data.putString("my_action", "com.google.android.gcm.demo.app.ECHO_NOW");
String id = Integer.toString(msgId.incrementAndGet());
gcm.send(ListCategoriesActivity.SENDER_ID + "@gcm.googleapis.com", id, data);
msg = "Sent message";
}
catch (IOException ex) {
msg = "Error :" + ex.getMessage();
}
return msg;
data.putString("my_message", "Test message");
data.putString("my_action", "com.google.android.gcm.demo.app.ECHO_NOW");
这是我的GCMinentService课程:
public class GcmIntentService extends IntentService {
private final String TAG = "GcmIntentService";
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
Set<String> keys = extras.keySet();
Log.i(TAG, "Received: " + extras.getString("my_message"));
}
...
}
公共类gcminentservice扩展了IntentService{
私有最终字符串TAG=“gcminentservice”;
公共GCMinentService(){
超级(“GCMinentService”);
}
@凌驾
受保护的手部内容无效(意图){
Bundle extras=intent.getExtras();
Set keys=extras.keySet();
Log.i(标记“Received:+extras.getString”(“我的消息”);
}
...
}
日志为“Received:null”,且extras中的密钥为:
[消息类型,google.message\u id,事件,android.support.content.wakelockid]
有什么想法吗
提前感谢。您正在尝试将上游消息(设备到云)从您的设备发送到服务器。只有当您有一台与Google的GCM CCS服务器建立XMPP连接的服务器时,它才会起作用。即使它确实有效,消息也会到达您的服务器,而不是您的设备
只有当您的服务器将消息发送到您的设备时,您的intent service才会收到消息。我也面临同样的问题。你得到答案了吗?