Reputation: 660
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 .
Upvotes: 0
Views: 232
Reputation: 660
finally i solve it with handler and looper and 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);
}
Upvotes: 1