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)

user2128
1#
user2128 Published in April 23, 2018, 9:20 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 .

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO