3
我試圖使用Firebase將數據發送到Android應用程序。如果應用程序正在運行後臺,我可以發送數據並將其成功保存。但是,如果應用程序被終止並且用戶獲得數據通知,則不會保存數據。Android的Firebase發送通知給用戶段來保存數據
我使用FirebaseMessagingService.onMessageReceived
方法捕獲數據並將其保存在SharedPreference
文件中。我也閱讀了firebase雲消息。但我沒有服務器,我只是使用Firebase控制檯。
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();
}
謝謝你的澄清。 – Isuru