I'm trying to send the data to android app using Firebase. I can send the data and save it successfully if app is running background. But if the app is killed and user gets the notification with data, it won't save the data.
I'm using FirebaseMessagingService.onMessageReceived
method to capture the data and save it in SharedPreference
file. I read on firebase cloud messaging as well. But I don't have a server, I'm just using firebase console.
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body Verse: " + remoteMessage.getData().get("verse"));
}
//sendNotification(remoteMessage.getNotification().getBody());
SharedPreferences sharedPref = this.getSharedPreferences(getString(R.string.preference_file_key),
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
Log.d("FIREBASE NOTIF SERVICE", remoteMessage.getNotification().getTitle());
Log.d("FIREBASE NOTIF SERVICE", remoteMessage.getData().get("verse"));
editor.putString(getString(R.string.preference_verse_title_key), remoteMessage.getNotification().getTitle());
editor.putString(getString(R.string.preference_verse_content_key), remoteMessage.getData().get("verse"));
editor.commit();
}
Firebase console currently only allows you to send notification messages. Notification messages do not result in FirebaseMessagingService.onMessageReceived being called if the app is in the background.
If you want FirebaseMessagingService.onMessageReceived to be called both when the app is in the foreground and background you would have to use data messages. However you cannot send data messages from the Firebase console at this time, you will need your own server to do this.
See the docs about message types for more.