通过向 service worker 发出 post 请求将推送通知标记为已读

Marking push notifications as read by making post request from service worker

我使用 Service Worker 实现了推送通知。当用户单击通知时,我想在我的数据库中将通知设为 "read" 。我写了一个 API 调用来将通知标记为 "read"。如何从 Service Worker 进行此调用。我的 API 调用在我的应用程序 js 文件中。

我找到了解决方案。我们可以使用来自 service worker 的 "fetch" 事件直接调用我们的 API。如果你想在请求中包含 cookies

,则将 fetch 中的 "credentials" 选项设置为 "include"
 fetch(url, { 
                method: 'post',
                credentials: 'include',
                body: JSON.stringify({"event_id": <event-id-value>})  
              })
              .then(function (data) {
                //console.log('Request succeeded with JSON response', data);  
              })  
              .catch(function (error) {  
                //console.log('Request failed', error);  
              });