代码之家  ›  专栏  ›  技术社区  ›  amin mahmoudi

将对象添加到继承FirebaseMessagingService继承服务(FCM)的类上的领域数据库

  •  0
  • amin mahmoudi  · 技术社区  · 8 年前

    大家好,我在公共构造函数(onMessageReceived)调用下面的方法(SaveMessage) FirebaseMessagingService 对于域中的保存字符串,但在此表中获取日期时,在获取通知后为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();
                       }
                   });
    
    
    }
    }
    

    我在一个片段中为这个领域表添加了更改侦听器。

    谢谢你的阅读。

    1 回复  |  直到 8 年前
        1
  •  1
  •   amin mahmoudi    8 年前

    最后,我使用handler和looper以及RealmChangeListener work perefect解决了这个问题:

    public void SaveMessage(final String message){
    handler = new Handler(Looper.getMainLooper());
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                final Realm realm = Realm.getDefaultInstance();
                realm.executeTransactionAsync(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        SimpleDateFormat simpleDateFormatTime = new SimpleDateFormat("YY/MM/dd 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); // using insert AP
                       // EventBus.getDefault().postSticky(true);
                    }},
                        new Realm.Transaction.OnSuccess() {
                            @Override
                            public void onSuccess() {
                                Log.e("service", "onSuccess: " );
                                realm.close();
                            }
                        },new Realm.Transaction.OnError() {
                         @Override
                          public void onError(Throwable error) {
                             Log.e("service", "onError: ",error );
                         }}
                );
               //handler.postDelayed(this, 500);
    
            }
        };
        handler.postDelayed(runnable, 500);
     }