我目前正在处理一个应用程序的更新,该应用程序具有以下工作流:
- 我们将记录上传到Firestore数据库,并
- 通过Google Cloud自动生成推送通知,该通知通过通道发送给我们的用户。设备。
- 设备接收此通知,并使用FCM的
onMessageCeived
函数,我们还与Firestore数据库触发了同步。
- 尝试与Firestore数据库同步。
- 无论此同步是否正确,推动通知仍然向用户显示。当他按下这一点时,应用程序崩溃了。
我的理论是,当Android进入DOZE模式时,步骤4给出了错误。因此该设备与Firestore数据库不同步,因此该应用程序将为用户崩溃。目前约有20%的用户发生这种情况。
然后,我发现了有关工人的信息,我相信可以帮助解决这个问题。尽管如此,我还是很新的。我当时认为一个解决方案可以是:
我专门创建一个工作Quest,并指出该设备应连接到Internet。在此工作中,我将执行此数据库同步,并向用户显示通知。
但是,这可能会导致一些延迟,因为这取决于用户何时将其设备连接到Internet。理想情况下,我相信最好的解决方案将是描述在这里。最后,就我而言,同步(计划作业)是由外部事件(FCM)触发的,但是我很难理解这是否确实是最好的解决方案。
你怎么认为?第一个解决方案会好吗?如果第二个更好,您是否有一个示例有关代码的外观(基本上是骨架,所以我可以理解如何将工人与FCM联系起来)?还是工人可能不是最好的解决方案?
非常感谢!
I currently am working on an update of an app, which has the following work flow:
- we upload a record to Firestore Database with some data
- This, through google cloud, automatically generates a push notification, which is sent through a channel to our users' devices.
- The devices receive this notification, and with the
onMessageReceived
function from FCM, we also trigger a sync with the Firestore Database.
- The attempt to sync with the Firestore Database is made.
- Regardless on whether this sync was correct, or not, the push notification is still shown to the user. When he presses on this, the app crashes.
I have the theory though that when Android enters Doze mode, step 4 gives an error; so the device does not sync with the Firestore Database, and hence the app will crash for the user. This is happening to around 20% of our users currently.
I then discovered about Workmanager, which I believe can help solve this issue. Nevertheless, I'm pretty new to this. I was thinking that one solution can be:
I specifically create a WorkRequest with a constraint stating that the device should be connected to the internet. In this WorkRequest I'd perform this database sync as well as showing the notification to the user.
Nevertheless, this can cause some delays, as it depends on when the user will connect their device to the internet. Ideally, I believe the best solution would be something like described here. In the end, in my case, the syncing (scheduled job) is triggered by an external event (FCM), but I'm having trouble to understand whether this really is the best possible solution.
What do you think? Would the first solution be good? If the second is better, do you maybe have an example on how the code would look like (a skeleton basically, so I can understand how to relate WorkManager with FCM)? Or is Workmanager maybe not the best solution for this?
Many thanks!
发布评论
评论(1)
在此处阅读:
https://develoveler.android.com/troid.com/topic/topic/performance/performance/pperformance/appstandby
在这里:
https:/https:/ /firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
但长话短说 - 如果您使用PUST-您需要高度优先消息使用户将与他们进行交互,这将将应用程序移至活动存储桶中。即使在打ze模式下,这也可以工作。
但是,如果没有用户互动:
,基本上这是我的建议:
发送优先级的推动。您不需要用户。
创建正确的约束工作以完成工作的工作。 (您可以在Wi-Fi的应用程序中放置一些设置,高/低电池,充电等)
启动工作时,您可以显示通知(它是api-foregroungreggroundinfo的一部分)同步以及工作完成时 - 您可以显示一些完成同步的通知。
另外,请记住,当您收到下一个推动时,可能会有未完成的工作。因此,寻找链接作品以及独特的作品。
例如,您可能只想拥有一项工作,并且可以使用唯一的作品 - 您可以替换旧的工作,甚至可以将其附加到它上。在此处查看:
最后,还有一个Q& a与Wm背后的家伙一起。您将了解所需的大多数内容:
Read here:
https://developer.android.com/topic/performance/appstandby
And here:
https://firebase.google.com/docs/cloud-messaging/concept-options#setting-the-priority-of-a-message
But long story short - if you use push - you need to give high priority to the messages so the user will interact with them and this will move the app to the active bucket. This can work even in doze mode.
But if there is no user interaction:
So basically this is my advice:
Send the push with low priority. You do not need the user.
Create work with the right constraints to do the job. ( you can put some settings in the application for wi-fi or not, high/low battery, charging or not, etc)
https://developer.android.com/topic/libraries/architecture/workmanager/how-to/define-work#work-constraints
When the work is started you can show a notification(it is part of the API - ForegroundInfo) for active syncing and when the work finishes - you can show some notification for finished syncing.
Also, have in mind that there might be unfinished work when you receive the next push. So look for chaining works and also unique works.
For example, you might want to have only one work and with unique - you can replace the old one or even append the new to it. Check here:
https://developer.android.com/topic/libraries/architecture/workmanager/how-to/managing-work#conflict-resolution
I would advise you to watch this playlist. In the end, there is also a Q&A with the guys behind the WM. You will understand most of the stuff you will need:
https://www.youtube.com/watch?v=NtpgWjiXEfg&list=PLWz5rJ2EKKc_J88-h0PhCO_aV0HIAs9Qk&ab_channel=AndroidDevelopers