0

I want to use firebase cloud messaging in my app to get notifications.

I am getting notifications when the app is either in the foreground or in the background, but if the app is killed then it doesn't work. I know that it is not possible without making something like service which runs even after closing the app but I don't have any idea how to implement it.

Please guide me. Thanks in advance.

I am using this class to get notifications.

public class FcmMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        String title= remoteMessage.getNotification().getTitle();
        String message= remoteMessage.getNotification().getBody();
        Intent intent= new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent= PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder= new NotificationCompat.Builder(this);
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(message);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());
    }
} 
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
neens
  • 93
  • 7
  • Possible duplicate of [Android app not receiving Firebase Notification when app is stopped from multi-task tray](https://stackoverflow.com/questions/39504805/android-app-not-receiving-firebase-notification-when-app-is-stopped-from-multi-t) – AL. Jul 25 '17 at 03:34

1 Answers1

0

If you are sending some data payload in message from firebase console you will not get your custom notification when app is in background or killed state you will get default firebase notification if you want to get notification when the app is in killed or background state send post request from your server to https://fcm.googleapis.com/fcm/send this url checkout the complete guide here https://firebase.google.com/docs/cloud-messaging/send-message and in your on messageReceive check data

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        if (remoteMessage.getData() != null) {
            Map<String, String> dataMap = remoteMessage.getData();
            String title = dataMap.get("title");
            String message = dataMap.get("description");
        }
    }
}
Jaskaran Singh
  • 127
  • 5
  • 12