title: FirebaseMessagingService - How to update Database or SharedPreferences from the background?
body: <p>I am trying to update the app database after receiving a push notification from Firebase Console. It works when the app is running, but when the app is in the background or process is killed, I can't change the data in SharedPreferences.</p>
  
  <p>But sending notification with <code>NotificationManager</code> is working. Is there any way to update database when retrieving push notification from Firebase?</p>
  
  <pre><code> private void sendNotification(String messageBody) {
      Intent intent = new Intent(this, EventListActivityDrawerNewMenu.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_launcher)
              .setContentTitle("FCM Message")
              .setContentText(messageBody)
              .setAutoCancel(true)
              .setSound(defaultSoundUri)
              .setContentIntent(pendingIntent);
  
      NotificationManager notificationManager =
              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  
      notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
  }
  
  private void saveDataToPreferences(String data) {
      Context context = this;
      SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(UpdateDataService.MY_PREFS_NAME, MODE_PRIVATE);
      SharedPreferences.Editor editor = sharedPreferences.edit();
      editor.putString(UpdateDataService.MY_PREFS_NAME, data);
      editor.commit();
  }
  
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
      android.os.Debug.waitForDebugger();
  
      Log.d(TAG, "From: " + remoteMessage.getFrom());
  
      if (remoteMessage.getData().size() &gt; 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().getBody());
      }
  
      saveDataToPreferences(remoteMessage.getNotification().getBody());
  
  }
  </code></pre>