android gcm通知解析

android gcm通知解析,android,push-notification,google-cloud-messaging,Android,Push Notification,Google Cloud Messaging,我是android新手 在我的应用程序中,我想显示GCM通知。我需要显示标题和短信。我可以从服务器端得到通知。但它显示了一些bundle message=Title=example Title message=sample message和一些collapse键 我只想显示示例标题和示例消息 我怎样才能展示它??是否需要任何解析 public class GcmIntentService extends IntentService { public static final String TAG

我是android新手

在我的应用程序中,我想显示GCM通知。我需要显示标题和短信。我可以从服务器端得到通知。但它显示了一些bundle message=Title=example Title message=sample message和一些collapse键

我只想显示示例标题和示例消息

我怎样才能展示它??是否需要任何解析

public class GcmIntentService extends IntentService {
public static final String TAG = "GcmIntentService";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

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)) {
            // 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.

            String msg=extras.getString("message");
            //sendNotification("Received: " + extras.toString());
            sendNotification("Received: " +msg);

            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.
private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) this
            .getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, Dashboard.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("MY BUONA REWARDS")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setContentText(msg);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}您需要使用

String message = intent.getExtras().getString("message"); and 
String title= intent.getExtras().getString("title");
而不是onHandleIntent方法内的sendNotification方法中的extras.toString

参见示例

检查示例中的gcminentservice.java类


希望这有帮助。

这可以由发送服务器设置。请查看中的通知部分