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

时间:2015-04-07 14:01:07

标签: android push-notification google-cloud-messaging

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

这是从extras.toString();

获得的

Bundle [{messageType = 1,contentText = content body,from = 23105361257,contentTitle = content title, message = test 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());
    }
}

1 个答案:

答案 0 :(得分:0)

您必须发送:

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

代替

sendNotification(extras.toString());

因此,您会在sendNotification()的参数中获得测试消息,然后您可以在Intent中传递

希望你能得到它。感谢。