簡體   English   中英

PMA Service Worker 通知點擊不起作用

[英]PMA Service worker notification click isn't working

我正在嘗試顯示通知並在單擊時執行某些操作。 顯示的部分運行良好,正在從服務器接收數據,但是,單擊通知的功能不起作用,我已經完成了文檔說明的所有內容,我在網上和這里找到的內容,所有內容都相同我已經實現但它似乎不起作用的功能。 我做了虛構的做法,只要數據不是從服務器加載的,就會觸發點擊,否則,數據不會。

誰能告訴我我做錯了什么? 我真的很感激一些幫助,我有兩天的時間。

self.addEventListener('push', (e) => {
  let notification = e.data.json();
  const title = 'My app ' + notification.title;
  const options = {
      body: notification.msg,
      actions: [
        { action: 'yes', title: 'Aprobar' },
        { action: 'no', title: 'Rechazar' }
      ]
  };
  self.registration.showNotification(title, options);
}); //to show the notification with server info

self.addEventListener('notificationclick', function (event) {
   console.log(event);
   var noti = event.notification.data;
   let r = event.notification.data.json();
   console.log(noti); }, 
false); //handling the click

我也嘗試過使用 notificationclose 來查看它是否捕捉到了點擊,但它也不起作用。

重要的是要注意它不顯示任何錯誤或警告,它可以簡單地做任何事情。 找到了解決辦法! 請參閱第一個答案。

我找到了解決方案! 在玩弄代碼並閱讀其他一些文檔后,事實證明,如果我的服務器上的服務是線程類型,則在我的 js 中必須等待答案。 如果有人需要的話。

let notification = '';
self.addEventListener('push', (e) => {
 notification = e.data.json();
 const title = 'My app' + notification.title;
 const options = {
    body: notification.msg,
    actions: [
        { action: 'yes', title: 'Aprobar' },
        { action: 'no', title: 'Rechazar' }
    ]
};
 e.waitUntil(self.registration.showNotification(title, options)); }); //waitUntil to show the notification

self.addEventListener('notificationclick', function (event) {
console.log('[Service Worker] Notification click Received.');
event.notification.close();
let response = event.action;
event.waitUntil(
    fetch(`api/authorizations/processor?uuid=${notification.uuid}&response=${response}&account=${notification.user}`,
        {
            method: 'GET',
            mode: 'cors',
            cache: 'default'
        }).then((result) => {
            if (!result.ok)
                throw result;
            return result.json();
        }).then(function (data) {
            console.log(data);
        }).catch(function (error) {
            console.log(errow);
        })
); }); // waitUntil to expect a action

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM