如何解析从 GCM 收到的消息?

How to Parse the Message received from GCM?

我收到来自服务器的通知,但我无法正确解析数据。

这是从 extras.toString();

得到的

Bundle[{messageType=1,contentText=内容body,from=23105361257,contentTitle=内容标题,message=测试消息,android.support.content.wakelockid =1, collapse_key=do_not_collapse, tickerText=代码短信}]

我只想在通知中打印捆绑包中的消息值。 而且如果我收到另一个通知.. 我想在同一个通知中以 Bigview 样式在下面显示它。这怎么可能???

例如

测试消息

另一条测试消息

public class GCMNotificationIntentService extends IntentService {
    public static final int notifyID = 9001;
    public static final String TAG = "GCM Demo";

    public GCMNotificationIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        if (!extras.isEmpty()) {
            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());
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE
                    .equals(messageType)) {
                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());
                sendNotification(extras.toString());
                Log.i(TAG, "Received: " + extras.toString());
            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }


    private void sendNotification(String msg) {
        Intent resultIntent = new Intent(this, HomeActivity.class);
        resultIntent.putExtra("message", msg);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
                resultIntent, PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder mNotifyBuilder;
        NotificationManager mNotificationManager;

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotifyBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("Notification")
                .setContentText("New Notification")
                .setSmallIcon(R.drawable.launcher_icons)
                .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(msg));

        mNotifyBuilder.setContentIntent(resultPendingIntent);

        int defaults = 0;
        defaults = defaults | Notification.DEFAULT_LIGHTS;
        defaults = defaults | Notification.DEFAULT_VIBRATE;
        defaults = defaults | Notification.DEFAULT_SOUND;

        mNotifyBuilder.setDefaults(defaults);
        mNotifyBuilder.setContentText(msg);
        mNotifyBuilder.setAutoCancel(true);
        mNotificationManager.notify(notifyID, mNotifyBuilder.build());
    }
}

你必须像这样发送:

sendNotification(extras.getString("message"));

改为

sendNotification(extras.toString());

所以你会在sendNotification()的参数中得到测试消息然后你可以在Intent

中传递它

希望你能得到它。谢谢