title: how to get notification using firebase cloud messaging when app is closed
body: <p>I want to use firebase cloud messaging in my app to get notifications.</p>
  
  <p>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.</p>
  
  <p>Please guide me. Thanks in advance.</p>
  
  <p>I am using this class to get notifications.</p>
  
  <pre><code>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());
      }
  } 
  </code></pre>