无法从FirebaseMessagingService向Realm保存通知信息

时间:2017-05-02 18:49:13

标签: android notifications realm

从技术上讲,它来自BroadcastReceiver,但广播正在从FirebaseMessagingService的实现中启动。

我正在尝试使用Realm在接收推送通知之间存储通知数据,然后在创建InboxStyle通知时使用。

我的问题是RealmObject似乎没有存储到文件系统。 每次收到推送通知时,我都会将通知版本保存到领域,然后查询以查看当前保存的通知。结果总是1,我刚刚保存的那个。 我已经将Realm文件拉下来并使用Realm Browser打开,它显示0个ChatNotifications,即Realm对象。

在处理通知和从通知中打开应用程序时,我没有看到日志中的任何错误。

我正在使用不同的Realm数据库作为主要数据存储而没有问题。

领域对象

public class ChatNotification extends RealmObject {

    public static final String FIELD_DATE = "messageDate";
    public static final String FIELD_CONVERSATION_ID = "conversationId";
    public static final String FIELD_USERNAME = "userName";

    @PrimaryKey
    private String messageId;
    private String conversationId;
    private String userName;
    private String messageText;
    private Date messageDate;

    public String getMessageId() {
        return messageId;
    }

    public void setMessageId(final String messageId) {
        this.messageId = messageId;
    }

    public String getConversationId() {
        return conversationId;
    }

    public void setConversationId(final String conversationId) {
        this.conversationId = conversationId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(final String userName) {
        this.userName = userName;
    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(final String messageText) {
        this.messageText = messageText;
    }

    public Date getMessageDate() {
        return messageDate;
    }

    public void setMessageDate(final Date messageDate) {
        this.messageDate = messageDate;
    }
}

RealmModule

@RealmModule(classes = {ChatNotification.class}, library = true)
public class NotificationModule {
}

领域配置

public class NotificationRealmConfiguration {

    private static final String realmName = "notification.realm";

    public static RealmConfiguration getInstance(){
        return new RealmConfiguration.Builder().name(realmName).modules(new NotificationModule()).build();
    }

    public static Realm getNotificationRealmInstance(){
        return Realm.getInstance(getInstance());
    }
}

切断广播接收器 我也尝试过添加Realm.init(context);也没有成功。

@Override
public void onReceive(final Context context, final Intent intent) {
    final Realm realm = NotificationRealmConfiguration.getNotificationRealmInstance();
    realm.beginTransaction();
    final ChatNotification chatNotification = realm.createObject(ChatNotification.class, messageId.toString());

    chatNotification.setConversationId(conversationId.toString());
    chatNotification.setMessageDate(new Date());
    chatNotification.setUserName("Me");
    chatNotification.setMessageText("Save me");
    realm.commitTransaction();

    final RealmQuery<ChatNotification> chatQuery = realm.where(ChatNotification.class);
    long queryCount = chatQuery.count();
    realm.close();
}

0 个答案:

没有答案