Skip to content

Instantly share code, notes, and snippets.

@kundansviet
Created January 26, 2017 16:10
Show Gist options
  • Save kundansviet/b9281dc97bdc605c4d5ff982d2e0392b to your computer and use it in GitHub Desktop.
Save kundansviet/b9281dc97bdc605c4d5ff982d2e0392b to your computer and use it in GitHub Desktop.
Class to show notification from firebase cloud messaging service
package com.cb.androidwarriorfcmdemo;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
/**
* Created by kundan on 1/26/2017.
*/
public class MyMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String message = remoteMessage.getNotification().getBody();
showSimpleNotification(message);
}
/**
* method to show notification from message retrived from firebase cloud messaging
* @param message
*/
void showSimpleNotification(String message) {
NotificationManager mNotificationManager = (NotificationManager) this.getApplicationContext().getSystemService(this.NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Pending intent to the notification manager
PendingIntent resultPending = stackBuilder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Building the notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher) // notification icon
.setContentTitle("Android warriors") // main title of the notification
.setContentText(message) // notification text
.setContentIntent(resultPending); // notification intent
// mId allows you to update the notification later on.
mNotificationManager.notify(10, mBuilder.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment