Android GCM intent.getExtras--如何仅显示特定数据?

Android GCM intent.getExtras--如何仅显示特定数据?,android,android-intent,google-cloud-messaging,Android,Android Intent,Google Cloud Messaging,在启用GCM的应用程序上工作,我可以接收消息。但是,格式显示为Message:Bundle[{Message=test,android.support.content.wakelock=3,collapse\u key=do\u not\u collapes,from=3423423}] 如何指定仅显示消息数据密钥对 GCM收到消息意图 protected void onHandleIntent(Intent intent) { Bundle extras = intent.g

在启用GCM的应用程序上工作,我可以接收消息。但是,格式显示为Message:Bundle[{Message=test,android.support.content.wakelock=3,collapse\u key=do\u not\u collapes,from=3423423}]

如何指定仅显示消息数据密钥对

GCM收到消息意图

 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)) {
                // This loop represents the service doing some work.
                for (int i = 0; i < 5; i++) {
                    Log.i(TAG, "Working... " + (i + 1)
                            + "/5 @ " + SystemClock.elapsedRealtime());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                    }
                }
                Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                // Post notification of received message.
                sendNotification("Message: " + extras.toString());
                Log.i(TAG, "Message: " + extras.toString());
            }
        }
        // Release the wake lock provided by the WakefulBroadcastReceiver.
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }
protectedvoid onHandleIntent(意图){
Bundle extras=intent.getExtras();
GoogleCloudMessaging gcm=GoogleCloudMessaging.getInstance(this);
//getMessageType()意图参数必须是您收到的意图
//在你的收音机里。
字符串messageType=gcm.getMessageType(intent);
如果(!extras.isEmpty()){//具有解包的效果
/*
*根据消息类型筛选消息。因为GCM可能会
*在将来使用新的消息类型进行扩展,只需忽略您正在使用的任何消息类型
*不感兴趣,或者你不认识。
*/
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)){
sendNotification(“发送错误:+extras.toString());
}else if(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)){
sendNotification(“服务器上的已删除邮件:+extras.toString());
//如果是常规GCM消息,请执行一些操作。
}else if(GoogleCloudMessaging.MESSAGE\u TYPE\u MESSAGE.equals(messageType)){
//此循环表示正在执行某些工作的服务。
对于(int i=0;i<5;i++){
Log.i(标记“正在工作…”+(i+1)
+“/5@”+SystemClock.elapsedRealtime());
试一试{
睡眠(5000);
}捕捉(中断异常e){
}
}
Log.i(标记“Completed work@”+SystemClock.elapsedRealtime());
//接收到消息的Post通知。
sendNotification(“消息:+extras.toString());
Log.i(标记,“Message:+extras.toString());
}
}
//释放唤醒接收器提供的唤醒锁。
GcmBroadcastReceiver.completeWakefulIntent(intent);
}

extras
是一个
捆绑包
,通过键访问单个数据段的方法,非常类似于
HashMap
。如果您只需要
消息
,请在
extras
上调用
getString(“message”)
它是一个json字符串,您可以解析并仅获取“message”键。

在gcminentservice类中,您可以在方法中解析所需的键

@Override
    protected void onMessage(Context context, Intent intent) {
        Log.i(TAG, "Received message");
        String message =   intent.getExtras().getString("BUY");

        displayMessage(context, message);
        // notifies user
        generateNotification(context, message);
    }

当服务器发送一个JSON字符串时,该JSON字符串中的名称/值对被转换为GCM客户端代码所看到的
包中的条目。是的,在GCMinentService类中,您可以在method@Override protected void onMessage(上下文上下文,意图){Log.i(标记,“Received message”)中解析所需的键;String message=intent.getExtras().getString(“购买”);displayMessage(上下文,消息);//通知用户生成通知(上下文,消息);}