Android Open Source - HMKCODE-GCM Gcm Message Handler






From Project

Back to project page HMKCODE-GCM.

License

The source code is released under:

Apache License

If you think the Android project HMKCODE-GCM listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.eventbooking.android.gcm;
//from  w  w w .j  a v a 2  s . c  o m
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.gcm.GoogleCloudMessaging;

/**
 * This class defines what to do with the received message.
 */
public class GcmMessageHandler extends IntentService {
    private String message;
    private Handler handler;

    public GcmMessageHandler() {
        super("GcmMessageHandler");
    }

    @Override
    public void onCreate() {
        // TODO: Auto-generated method stub.

        super.onCreate();

        handler = new Handler();
    }

    /**
     * Processes the message received from GCM.
     *
     * @param intent The intent to be handled.
     */
    @Override protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();

        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

        String messageType = gcm.getMessageType(intent);

        message = extras.getString("title");

        showToast();

        Log.i(MainActivity.TAG, "Received: (" + messageType + ") " + message);

        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    /**
     * Shows a toast notification.
     */
    public void showToast() {
        handler.post(new Runnable() {
            @Override public void run() {
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
            }
        });
    }
}




Java Source Code List

com.eventbooking.android.gcm.ApplicationTest.java
com.eventbooking.android.gcm.GcmBroadcastReceiver.java
com.eventbooking.android.gcm.GcmMessageHandler.java
com.eventbooking.android.gcm.MainActivity.java