将 Service Worker 中的数据写入 Cloud Firestore [英] Writing data in Service Worker to Cloud Firestore

本文介绍了将 Service Worker 中的数据写入 Cloud Firestore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 Firebase 项目,我想在我的 javascript 项目中的 Service Worker 中接收 Firebase 云消息(从 Node.js 服务器发送).这工作正常,但现在我想在收到通知后从我的服务工作者那里保存一些数据到我的 firebase cloud firestore 中.问题来了,我运行时出现以下错误:

 Uncaught ReferenceError: XMLHttpRequest is not defined at Ln.(xmlhttp.js:167) 在我 (channelrequest.js:512) 在 Ce (webchannelbase.js:1249) 在 Kn.xa (webchannelbase.js:1251) 在 re (run.js:124)

几天以来,我一直在尝试查找错误,但直到现在才找到解决方案.因此,我希望您现在可以帮助我.我已尝试将数据保存在有效的 firebase 实时数据库中,但由于我需要离线持久性,我不得不切换到运行上述错误的云 Firestore.

这是我的node.js服务器发送通知的代码(userID和registrationToken之前定义):

payload = {数据: {标题":这是一个通知",body":这是通知消息的正文."},};选项 = {优先级:高",生存时间:7200};//向注册令牌发送消息admin.messaging().sendToDevice(registrationToken, payload, options).then(function(response) {admin.firestore().collection("Logging").doc(userID).set({active: "true"});}).catch(函数(错误){console.log("发送消息时出错:", error);});

这是在我的 firebase-messagins 服务工作者文件中接收通知的代码(用户 ID 之前定义):

self.addEventListener('notificationclick', function(event) {event.notification.close();const keepAlive = async() =>{firebase.firestore().collection("Logging").doc(userID).update({"open": "true"});}event.waitUntil(keepAlive());});

有人可以帮我吗?我没有更多的想法来解决我的问题:/

解决方案

您不能在 Service Worker 内部使用 XMLHttpRequest.相反,您需要使用Fetch API.>

根据您的代码,Firebase 客户端库在底层使用 XMLHttpRequest.我不认为您对此有任何控制权(至少不是基于我在 Firebase 的当前 JavaScript 文档中看到的内容).

您可以尝试使用 REST API 直接使用 fetch() 发送您的 HTTP 请求.您将负责身份验证和设置任何用户 ID 等,否则客户端库会为您处理这些事情,但我认为这是您在 Service Worker 中的最佳选择.

I'm working on a Firebase project and I want to receive firebase cloud messages (send from a Node.js server) in my service worker in my javascript project. This works fine, but now I want to save some data in my firebase cloud firestore from my service worker after receiving the notification. And there is the problem, I'm running in the error listed below:

 Uncaught ReferenceError: XMLHttpRequest is not defined at Ln.<anonymous> (xmlhttp.js:167) at Me (channelrequest.js:512) at Ce (webchannelbase.js:1249) at Kn.xa (webchannelbase.js:1251) at re (run.js:124)

Since a few days I'm trying to find the error, but could not find a solution until now. Therefore I hope that you can help me now. I've tried to save the data in the firebase realtime database which works, but because I need to have offline persistence, I had to switch to cloud firestore which runs in the error above.

Here is the code from my node.js server for sending the notification (userID and registrationToken is defined before):

payload = {
     data: {
     "title": "This is a Notification",
     "body": "This is the body of the notification message."
     },
};

options = {
     priority: "high",
     timeToLive: 7200
};

// send message to the registration token
admin.messaging().sendToDevice(registrationToken, payload, options).then(function(response) {
     admin.firestore().collection("Logging").doc(userID).set({active: "true"});
}).catch(function(error) {
     console.log("Error sending message:", error);
}); 

And here is the code for receiving the notification in my firebase-messagins service worker file (userID is defined before):

self.addEventListener('notificationclick', function(event) {
  event.notification.close();
  const keepAlive = async() => {
      firebase.firestore().collection("Logging").doc(userID).update({"open": "true"}); 
  }
  event.waitUntil(keepAlive()); 
});

Can anyone help me, please? I have no more ideas for solving my problem :/

解决方案

You can't use XMLHttpRequest inside of a service worker. Instead, you need to use the Fetch API.

Based on your code, it's the Firebase client library that's using XMLHttpRequest under the hood. I don't think that you have any control over that (at least not based on what I see in the current JavaScript documentation for Firebase).

Instead of using the Firebase client library in your service worker, you could try using the REST API directly, using fetch() to send your HTTP requests. You'll be responsible for things like authentication and setting whatever user ids, etc. that the client library would otherwise handle for you, but I think that's your best bet inside of a service worker.

这篇关于将 Service Worker 中的数据写入 Cloud Firestore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆