简体   繁体   English

当我的应用程序被杀死时如何更新字段(Firebase)

[英]How to update a field when my app is killed(Firebase)

I have an apps where I need to track my user active or not.我有一个应用程序需要跟踪我的用户是否活跃。 So I create a service class to update user active status.Here is my service class code所以我创建了一个服务 class 来更新用户活动状态。这是我的服务 class 代码

class ServiceClass : Service() {
override fun onBind(p0: Intent?): IBinder? {
    return null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
   Repository.updateActiveStatus(true)
    return START_NOT_STICKY
}

override fun onDestroy() {
    super.onDestroy()
  Repository.updateActiveStatus(false)
}

override fun onTaskRemoved(rootIntent: Intent?) {
    super.onTaskRemoved(rootIntent)
    Log.d("TAGService", "onTaskRemoved: false")
    Firebase.firestore.collection("User").document(Firebase.auth.uid.toString())
        .update("active", true).addOnSuccessListener {
            Log.d("TAGService", "onTaskRemoved: updated")
        }
    stopSelf()
}}

here is my manifest code这是我的清单代码

<service android:name=".utils.ServiceClass" android:stopWithTask="false"></service>

But when I open my apps its update its active status but when I destroy or killed my code its not updating.但是,当我打开我的应用程序时,它会更新其活动状态,但当我销毁或终止我的代码时,它不会更新。

If you want to check user is active or not then it better to check at application level instead of service level.如果您想检查用户是否处于活动状态,那么最好在应用程序级别而不是服务级别进行检查。 And if you like to update the UI you can try Event bus .如果你想更新 UI,你可以试试Event bus Here is the sample code.这是示例代码。

        public class AppOpenManager :Application.ActivityLifecycleCallbacks, LifecycleObserver{
      .....
      @OnLifecycleEvent(ON_START)
        public fun onStart() {
            Log.d(LOG_TAG, "onStart");
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        public fun onAppBackgrounded() {
            Log.d(LOG_TAG,"APP BACKGROUNDED");
           
        }

         ....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FireBase - 使用用户身份验证时更新字段 - FireBase - update field when using user authentication REACT JS FIREBASE FIRESTORE- 我如何更新文档中的字段? - REACT JS FIREBASE FIRESTORE- how can i update my field in a document? 当应用程序处于后台或被杀死时,我想在 Android 中显示自定义 firebase 推送通知 state - I want to show a customize firebase push notification in Android when app is in background or killed state 如何更新 firebase 列表中的字段? - How to update a field inside a list in firebase? 如何解决您的 function 被杀死是因为它在我的 Firebase 模拟器中引发了未处理的错误? - how to solve Your function was killed because it raised an unhandled error in my Firebase emulator? 应用程序被终止时,设备未收到推送通知 - push notification not received to devices when app is killed 应用程序被杀死时 FCM 通知不会触发 - FCM Notification not firing when app is killed firebase 推送通知在某些设备 (Android) 中被杀死的应用程序不起作用 state - firebase push notification is not working on app killed state in some devices (Android) Firebase app在后台时如何处理通知 - How to handle notification when app in background in Firebase Firebase:如何在应用检查中注册我的 Flutter 应用? - Firebase: How do I register my Flutter app in app check?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM