Skip to content

Instantly share code, notes, and snippets.

@yusufcakal
Created December 21, 2017 07:59
Show Gist options
  • Save yusufcakal/02a2150c69d666de76bd16a6f1d98222 to your computer and use it in GitHub Desktop.
Save yusufcakal/02a2150c69d666de76bd16a6f1d98222 to your computer and use it in GitHub Desktop.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
sendNotification(remoteMessage.getNotification());
}
private void sendNotification(RemoteMessage.Notification message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notifications_black_24dp)
.setContentText(message.getBody())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setVibrate(new long[]{1000,1000})
.setContentIntent(pendingIntent);
if(message.getTitle()!=null){
notificationBuilder.setContentTitle(message.getTitle());
}else{
notificationBuilder.setContentTitle(getResources().getString(R.string.app_name));
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment