Home add object to realm database on class that Inherited FirebaseMessagingService Inherited service (FCM)
Reply: 0

add object to realm database on class that Inherited FirebaseMessagingService Inherited service (FCM)

user8467
1#
user8467 Published in September 19, 2018, 9:10 am

hello everybody i call below method (SaveMessage )at Public Constructor (onMessageReceived) FirebaseMessagingService for save String in realm but after get notification when get date from this table its null

   public class FireBaseService extends FirebaseMessagingService {


    public static  int NOTIFICATION_ID = 1;
    Handler handler;
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    generateNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle());

    SaveMessage(remoteMessage.getNotification().getBody());
    //AddList(remoteMessage.getData().get("message"));
    //StartActvity(Integer.valueOf(remoteMessage.getData().get("key_1")));

  }

 public void SaveMessage(final String message){


           Realm realm = Realm.getDefaultInstance();
               realm.executeTransactionAsync(new Realm.Transaction() {
                   @Override
                   public void execute(Realm realm) {
                       SimpleDateFormat simpleDateFormatTime = new SimpleDateFormat("HH:mm", Locale.getDefault());
                       String strTime = simpleDateFormatTime.format(new Date());
                       Number currentIdNum = realm.where(MessagePojo.class).max("id");

                       int nextId;
                       if (currentIdNum == null) {
                           nextId = 1;
                       } else {
                           nextId = currentIdNum.intValue() + 1;
                       }
                       MessagePojo messagePojo =  realm.createObject(MessagePojo.class,nextId);
                       messagePojo.setDate(strTime);
                       messagePojo.setMessage(message);
                       realm.insertOrUpdate(messagePojo); 
                       EventBus.getDefault().postSticky(true);
                       realm.close();
                   }
               });


}
}

and i add change listener for this Realm table in one fragment.

thank you for your reading .

share|improve this question
  • @EpicPandaForce What is your comment communicating with my problem :| ?? and i close realm in last line ! – amin mahmoudi Apr 19 at 15:57
  • Because you are closing the background thread Realm before the transaction is committed, instead of closing the Realm that started the async transaction. So your transaction is never committed. – EpicPandaForce Apr 19 at 16:43

1 Answer 1

active oldest votes
up vote 0 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.682371 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO