Skip to content

Instantly share code, notes, and snippets.

@n1lesh
Created December 18, 2017 08:33
Show Gist options
  • Save n1lesh/8398361f7fae4553fb9bbb11c34f0507 to your computer and use it in GitHub Desktop.
Save n1lesh/8398361f7fae4553fb9bbb11c34f0507 to your computer and use it in GitHub Desktop.
Sample Code for Push Notifications (FCM) on Android - Message Receiver
package com.mynotificationsapp.android;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
/**
* Created by What's That Lambda on 11/6/17.
*/
public class MessageReceiver extends FirebaseMessagingService {
private static final int REQUEST_CODE = 1;
private static final int NOTIFICATION_ID = 6578;
public MessageReceiver() {
super();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
final String title = remoteMessage.getData().get("title");
final String message = remoteMessage.getData().get("body");
showNotifications(title, message);
}
private void showNotifications(String title, String msg) {
Intent i = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, REQUEST_CODE,
i, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setContentText(msg)
.setContentTitle(title)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher_round)
.build();
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, notification);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment