Android Open Source - AndroidPushNotifications Gcm Message Handler






From Project

Back to project page AndroidPushNotifications.

License

The source code is released under:

Apache License

If you think the Android project AndroidPushNotifications 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;
/* w  ww.  j av  a 2  s.c o  m*/
import android.app.IntentService;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.NotificationCompat;
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 title;
    private String message;
    private Handler handler;

    public static final int NOTIFICATION_ID = 1;
    NotificationCompat.Builder builder;

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

    @Override
    public void onCreate() {
        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);

        if (!extras.isEmpty()) {
            if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                message = "Send error: " + extras.toString();
                sendNotification();
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
                message = "Deleted messages on server: " + extras.toString();
                sendNotification();
            } else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                title = extras.getString("title");
                message = extras.getString("message");

                sendNotification();
                showToast();

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

        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

    @SuppressWarnings("UnusedDeclaration")
    private void showToast() {
        handler.post(new Runnable() {
            @Override public void run() {
                Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
            }
        });
    }

    private void sendNotification() {
        Log.i(MainActivity.TAG, "Preparing to send notification: " + message);

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

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

        builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.common_ic_googleplayservices)
                .setContentTitle("GCM Notification")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setContentTitle(title)
                .setContentText(message);

        builder.setContentIntent(contentIntent);
        notificationManager.notify(NOTIFICATION_ID, builder.build());
        Log.i(MainActivity.TAG, "Notification sent successfully.");
    }
}




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