【问题标题】:Android - How to use Realm in FirebaseMessagingService class (non activity)Android - 如何在 FirebaseMessagingService 类中使用 Realm(非活动)
【发布时间】:2017-04-11 07:24:32
【问题描述】:

我使用 Realm 作为 ORM 来管理 Android 应用程序中的数据库,一切正常。但是,当我收到推送通知,然后尝试使用 Realm 保存通知数据时,会发生错误。 以下是错误:

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

这是我从 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();
}

}

这是我初始化 Realm 的类,这个类从 Application 扩展而来,BaseApplication 是我的应用程序的名称:

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);
}

}

这是我的文件进入项目的位置:

enter image description here

我需要使用 Realm 将接收到的信息保存在我的数据库中,但是这个非活动文件中出现了这个错误。

希望你能帮助我。 问候。

【问题讨论】:

  • 你需要为不同的线程获取一个单独的Realm实例。见realm.io/docs/java/latest/#threading
  • 一个堆栈跟踪会很好,还有setUniqueId()的代码
  • 请从您访问 Realm 的活动中获取代码。
  • 我们需要堆栈跟踪和来自setUniqueId 的代码才能提供帮助。

标签: android firebase notifications realm firebase-cloud-messaging


【解决方案1】:

当您收到推送通知时,无论您是否打开了 Activity/Application,Android 操作系统都会启动您的 FirebaseMessagingService。这意味着您不仅在不同的线程中,而且在完全不同的进程中。

因此,您必须通过 Intent 将数据发送到 Activity/Application 进程。通常,就像您的情况一样,这最好通过将整个 RemoteMessage 作为 Intent 中的 Extra 发送来完成,类似于您已经为通知的 PendingIntent 所做的。

然后你必须在你的 (Home) Activity 中处理传入的 Intent。

【讨论】:

    猜你喜欢
    • 2015-04-14
    • 2014-09-02
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2011-06-19
    • 2011-08-15
    相关资源
    最近更新 更多