[英]unable to interpret message from GCM
I'm trying to understand how GCM works. 我正在尝试了解GCM的工作原理。 To do so, I copy/paste the code that http://developer.android.com/ proposes in the section "implementing a GCM client".
为此,我复制/粘贴http://developer.android.com/在“实现GCM客户端”部分中建议的代码。
I don't have a server for now, so it's just a http request that I send from eclipse. 我暂时没有服务器,所以这只是我从eclipse发送的一个http请求。
Here is the request: 这是请求:
HttpEntity entity = new ByteArrayEntity(contenu.getBytes("UTF-8"));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(this.getAdresse());
post.addHeader("Authorization", "key=AIzaSyANnvuPYJaSIDM7gorqdKh25uvpNA-4rvk");
post.addHeader("Content-Type", "application/json");
post.setEntity(entity);
HttpResponse response = client.execute(post);
where "contenu" is this String: 其中“ contenu”是此字符串:
{ "data": {
"message":"hello"
},
"registration_ids": ["4", "8", "15", "16", "23", "42"]
}
On my client app, I have this IntentService (found on the developers site): 在我的客户端应用程序上,我有以下IntentService(位于开发者网站上):
package com.test.testnotification3;
import com.google.android.gms.gcm.GoogleCloudMessaging;
import com.test.testnotification3.R;
import core.GcmBroadcastReceiver;
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class GcmIntentService extends IntentService {
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
static final String TAG = "GCMDemo";
public GcmIntentService() {
super("GcmIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) { // has effect of unparcelling Bundle
/*
* Filter messages based on message type. Since it is likely that GCM
* will be extended in the future with new message types, just ignore
* any message types you're not interested in, or that you don't
* recognize.
*/
if (GoogleCloudMessaging.
MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + extras.toString());
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
extras.toString());
// If it's a regular GCM message, do some work.
} else if (GoogleCloudMessaging.
MESSAGE_TYPE_MESSAGE.equals(messageType)) {
Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
// Post notification of received message.
sendNotification("Received: " + extras.toString());
Log.i(TAG, "Received: " + extras.toString());
}
}
// Release the wake lock provided by the WakefulBroadcastReceiver.
GcmBroadcastReceiver.completeWakefulIntent(intent);
}
// Put the message into a notification and post it.
// This is just one simple example of what you might choose to do with
// a GCM message.
public void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
long[] pattern = {0,1000};
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Coucou Matthieu")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setVibrate(pattern);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
And I got this message in my Logcat: 我在Logcat中收到了以下消息:
07-06 19:26:15.627: I/GCMDemo(5095): Received: Bundle[{msg=hello, android.support.content.wakelockid=1, collapse_key=do_not_collapse, from=472226063168}]
So my question is: how can I get just the "msg" field ? 所以我的问题是:我怎样才能只获得“ msg”字段?
Thank you for your answer(s)! 谢谢您的回答)!
You can get the data from the Bundle
associated with the Intent
. 您可以从与
Intent
关联的Bundle
中获取数据。 In this case since it's a string you can call getString()
passing the proper key ( "msg"
): 在这种情况下,因为它是一个字符串,所以您可以调用
getString()
并传递适当的键( "msg"
):
Bundle extras = intent.getExtras();
String msg = extras.getString("msg");
You can do this in the branch where you know that the GCM message is of message type: 您可以在知道GCM消息属于消息类型的分支中执行此操作:
Bundle extras = intent.getExtras();
...
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
String msg = extras.getString("msg");
// Do something with msg
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.