【问题标题】:GCM intent.getExtras -- how to display only specific data?GCM intent.getExtras——如何只显示特定数据?
【发布时间】:2013-10-02 09:35:30
【问题描述】:

在支持 GCM 的应用上工作,我可以接收消息。但是格式显示为 Message: Bundle[{message=test, android.support.content.wakelock=3, collapse_key=do_not_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);
    }

【问题讨论】:

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


    【解决方案1】:

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

    @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);
        }
    

    【讨论】:

      【解决方案2】:

      它是一个json字符串,你只能解析得到“message”键。

      【讨论】:

      • 当服务器发送 JSON 字符串时,该 JSON 字符串中的名称/值对将转换为 GCM 客户端代码所见的 Bundle 中的条目。
      • 是的,在 GCMIntentService 类中,您可以在方法内解析所需的键 @Override protected void onMessage(Context context, Intent intent) { Log.i(TAG, "Received message");字符串消息 = intent.getExtras().getString("BUY");显示消息(上下文,消息); // 通知用户 generateNotification(context, message); }
      【解决方案3】:

      extras 是一个BundleBundle is a Java class,带有 like getString() 方法,用于按键访问单个数据,很像 HashMap。如果您只想要message,请在extras 上致电getString("message")

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-19
        • 1970-01-01
        相关资源
        最近更新 更多