简体   繁体   English

当设备未收到 Firebase 通知时执行操作

[英]Perform an action when Firebase Notification is not received by the device

I am trying to check if a function or method is being invoked in the server and act accordingly.我正在尝试检查服务器中是否正在调用 function 或方法并采取相应措施。

I am creating a basic app that notifies people about their financial transactions.我正在创建一个基本的应用程序来通知人们他们的金融交易。 I am using Flutter for the app, NodeJS for the server side of things, Firebase Cloud Messaging for Push Notification and React in order to send the data.我在应用程序中使用 Flutter,在服务器端使用 NodeJS,在推送通知和 React 中使用 Firebase Cloud Messaging 来发送数据。 So far, I've been successful in sending the Notification and displaying a list of notification for the user when using the mobile application.到目前为止,我已成功发送通知并在使用移动应用程序时为用户显示通知列表。 The whole system works something like this:整个系统的工作原理如下:

  1. The message is composed and sent from the react website,消息由反应网站组成并发送,
  2. The website calls a POST request from the server to send a notification,该网站从服务器调用 POST 请求以发送通知,
  3. The POST request then calls the getMessaging() function from the Firebase API that sends the notification to that device POST 请求然后从 Firebase API 调用getMessaging() function 将通知发送到该设备
  4. The mobile application listens for a notification using onMessage() as well as onBackgroundMessage() .移动应用程序使用onMessage()onBackgroundMessage()监听通知。 When a notification is received it again calls the server to store some notification data to the database.当收到通知时,它再次调用服务器将一些通知数据存储到数据库中。

The reason I am storing the notification in the database after it is received by the device is so that I know the status of the message I've sent ie "delivered".我在设备收到通知后将通知存储在数据库中的原因是,我知道我发送的消息的状态,即“已送达”。 Now, I am aiming to send an SMS to the device if the notification is not "delivered" after a certain period of time.现在,如果通知在一段时间后没有“送达”,我的目标是向设备发送短信。 However, looking at the workflow, sending the notification and storing notification are two complete separate methods and thus, I am out of ideas.但是,查看工作流程,发送通知和存储通知是两个完全独立的方法,因此,我没有想法。 Is there any way to know if a method/function is being invoked and act accordingly.有什么方法可以知道是否正在调用方法/函数并采取相应的行动。 Or should I take some other approach to this scenario?或者我应该对这种情况采取其他方法吗? (If yes, could you give some ideas?) (如果是,你能提供一些想法吗?)

I am just trying to aid my future self in need.我只是想帮助我未来有需要的自己。 Thanks GrahamD for providing me with some ideas.感谢GrahamD为我提供了一些想法。 The process of how my code manages it is below:我的代码如何管理它的过程如下:

sendNotification() : sendNotification()

This is a POST method that sends notification to the mobile application as well as store the data from that notification to a database.这是一个POST方法,它向移动应用程序发送通知并将来自该通知的数据存储到数据库中。 When storing to database, I modify my data model to take two more fields:存储到数据库时,我修改我的数据 model 以增加两个字段:

  1. status : with values like sent & delivered status :具有已sentdelivered等值
  2. messageID : unique ID for each message generated by uuid . messageID :由uuid生成的每条消息的唯一 ID。

The action is still running with getMessaging.send().then() below.该操作仍在使用下面的getMessaging.send().then()运行。

updateNotificationStatus()

Instead of saving the notification data after being received by the application, I update just the status field using this function. Inside the Flutter app, whenever the application receives a message ( onMessage or onBackgroundMessage from Firebase Cloud Messaging), I call this function as a PUT method to update the status from sent to delivered by using the messageId that was sent with the notification.我没有在应用程序收到通知数据后保存通知数据,而是使用这个 function 更新status字段。在 Flutter 应用程序中,每当应用程序收到一条消息(来自 Firebase 云消息传递的onMessageonBackgroundMessage )时,我将此 function 称为PUT方法使用随通知发送的messageId将状态从已sent更新为已delivered

getMessaging().send().then()

Inside the sendNotification() , the callback .then() calls another method after the notification is sent.sendNotification()内部,回调.then()在发送通知后调用另一个方法。 This middleware function gets invoked after the set period of time (set inside the code) to check the status of the messageId that was just sent.这个中间件 function 在设置的时间段(在代码中设置)后被调用,以检查刚刚发送的messageIdstatus If the status has been changed to delivered , no action is taken.如果status已更改为delivered ,则不执行任何操作。 However, if the status remains unchanged (ie: sent ), then I can code the action I want to perform.但是,如果status保持不变(即:已sent ),那么我可以编写我想要执行的操作。

The back and forth of function invocations is still a headache for me to explain as well as understand. function 的来回调用还是让我头疼,解释不明白。 Hopefully, this is understandable and can help out others looking for the answers.希望这是可以理解的,并且可以帮助其他人寻找答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM