title: How to save data from notification received when the app is in the background
body: <p>I want to know how to save the information received from data payload of firebase cloud messaging.
  What actually I want is, I am receiving the fields called notification_type and other stuff from data payload and I want to save it somewhere when the application is in the background and retrieve it when ever I open my application and update my UI based on that. </p>
  
  <p>Till now I have tried saving it on Sharedprefrences, external file, and lastly on local db, all these worked fine and saved the data when the app was in the foreground, but they did not work when the app was in the background.</p>
  
  <p>Below is the code that I tried recently, to save the data in the local db, works fine when the app is in the foreground, but does not work when the app is in the background.</p>
  
  <p><strong>FireBaseMsgService.class</strong></p>
  
  <pre><code>public class FireBaseMsgService extends FirebaseMessagingService {
       @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
         // for normal notifications
          super.onMessageReceived(remoteMessage);
          if(remoteMessage.getData().get("Notification_type").equals("2")){
              String Org_Id=remoteMessage.getData().get("Org_Id");
              String Unit_Id=remoteMessage.getData().get("Unit_Id");
              String User_Id=remoteMessage.getData().get("User_Id");
              String PatientId=remoteMessage.getData().get("PatientId");
              String Notification_type=remoteMessage.getData().get("Notification_type");
              if (remoteMessage.getNotification() != null){
                  String Click_Action=remoteMessage.getNotification().getClickAction();
                  User_Id=remoteMessage.getNotification().getTag();
                  saveNotificationDataToDb(getApplicationContext(),Notification_type);
                  FirebaseNotificationService firebaseNotificationService=new FirebaseNotificationService(
                          getApplicationContext(),
                          remoteMessage.getNotification().getTitle(),
                          remoteMessage.getNotification().getBody(),
                          Click_Action,
                          User_Id,
                          Org_Id,
                          Unit_Id,
                          PatientId,
                          Notification_type);
                  firebaseNotificationService.createNotification(remoteMessage.getNotification().getTitle(),
                          remoteMessage.getNotification().getBody(),
                          User_Id);
                  BusProvider.postOnMain(BusProvider.getInstance(),new NotifyingEvent(Notification_type));
  
                  //sendNotificationToFilesystem(Notification_type);
  
              }
          }
          //for normal notifications
          else{
              if (remoteMessage.getNotification() != null){
                  String User_Id=remoteMessage.getNotification().getTag();
                  FirebaseNotificationService firebaseNotificationService=new FirebaseNotificationService(
                          getApplicationContext(),
                          remoteMessage.getNotification().getTitle(),
                          remoteMessage.getNotification().getBody(),
                          User_Id);
                  firebaseNotificationService.createNotification(remoteMessage.getNotification().getTitle(),
                          remoteMessage.getNotification().getBody(),
                          User_Id);
              }
          }
      }
  
      private void saveNotificationDataToDb(Context context , String notification_type) {
          RecievedNotificationDbtable recievedNotificationDbtable=new RecievedNotificationDbtable(context);
          recievedNotificationDbtable.open();
          recievedNotificationDbtable.insertEntry(notification_type);
          recievedNotificationDbtable.close();
      }
  }
  </code></pre>