Reputation: 21
I am using Realm as ORM to manage database in an Android Application, everything is ok with this. But when I catch a push notification and then I try to save notification data using Realm an error occurs. the following is the error:
java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
This is my class that extends from FirebaseMessagingService:
public class SMovilFirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if (remoteMessage.getNotification() != null)
{
saveDataNotification(remoteMessage.getData().get("resourceId"), remoteMessage.getNotification().getTitle());
showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
}
}
private void showNotification(String title, String body) {
Intent intent = new Intent(this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
private void saveDataNotification(String resourceId, String message){
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
Notifications notification = realm.createObject(Notifications.class, setUniqueId(realm));
notification.setMessage(message);
notification.set_state("1");
notification.set_linkResource(resourceId);
realm.commitTransaction();
}
}
This is the class where I init Realm, this class extends from Application and BaseApplication is the name of my application:
public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(config);
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
This the location of my files into the project:
I need to use Realm to save the received info in my database, but this error appears in this non activity file.
Hope you can help me. Regards.
Upvotes: 2
Views: 1028
Reputation: 1457
When you receive a push notification, the Android operating system spins up your FirebaseMessagingService whether you have the Activity/Application open or not. This means that you are not just inside a different thread, you are inside a different process altogether.
So, you must send the data to the Activity/Application process via an Intent. Usually, and as is the case for you, this is best done by sending the whole RemoteMessage as an Extra in an Intent, similar to what you have already done with the PendingIntent for the Notification.
Then you have to handle the incoming intent in your (Home) Activity.
Upvotes: 1