小编Aka*_*bey的帖子

Android:将 FCM 通知保存到 Room DB

我正在使用 firebase FCM 来获取通知。我想要如果收到通知,它应该存储在我的 RoomDatabase 中。稍后,当用户打开应用程序时,将在通知部分显示所有通知

我正在使用 MVVM,我试图通过ViewModel进行保存,但它不起作用。这是我目前的代码。

public class MessagingService extends FirebaseMessagingService {

    @Override
    public void onNewToken(String s) {
        super.onNewToken(s);
        Log.d("MessagingService", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        // Check if message contains a notification payload.

        if (remoteMessage.getData().isEmpty()){
            showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        } else {
            showNotification(remoteMessage.getData());
        }

    }

    private void showNotification(Map<String, String> data) {


        Bitmap bitmap;

        String title = data.get("title").toString();
        String body = data.get("body").toString();
        String imageUri = data.get("image").toString();
        String TrueOrFalse = data.get("AnotherActivity").toString();

        bitmap = getBitmapfromUrl(imageUri); …
Run Code Online (Sandbox Code Playgroud)

android android-sqlite firebase-cloud-messaging android-mvvm android-room

4
推荐指数
1
解决办法
2936
查看次数