gcm.GcmMessageHandler.java Source code

Java tutorial

Introduction

Here is the source code for gcm.GcmMessageHandler.java

Source

/**
 * Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
 * See License.txt for license information.
 */
package gcm;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import com.google.android.gms.gcm.GcmListenerService;
import com.nganthoi.salai.tabgen.ConversationActivity;
import com.nganthoi.salai.tabgen.R;

public class GcmMessageHandler extends GcmListenerService {

    public static final int MESSAGE_NOTIFICATION_ID = 435345;

    @Override
    public void onMessageReceived(String from, Bundle data) {
        createNotification("Mattermost", data.getString("message"));
        super.onMessageReceived(from, data);
    }

    @Override
    public void onDeletedMessages() {
        super.onDeletedMessages();
    }

    @Override
    public void onMessageSent(String msgId) {
        super.onMessageSent(msgId);
    }

    @Override
    public void onSendError(String msgId, String error) {
        super.onSendError(msgId, error);
    }

    private void createNotification(String title, String body) {
        Context context = getBaseContext();

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

        Intent notificationIntent = new Intent(context, ConversationActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent contentIndent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher).setContentTitle(title).setContentText(body)
                .setDefaults(defaults).setStyle(new NotificationCompat.BigTextStyle().bigText(body))
                .setContentIntent(contentIndent).setAutoCancel(true);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(MESSAGE_NOTIFICATION_ID, mBuilder.build());
    }

}