Windows通知托盘中的通知事件不起作用

时间:2018-12-24 11:09:40

标签: javascript firebase firebase-cloud-messaging

您好!这是我的第一个查询,请帮忙。

我已成功将FCM通知用于我的angular 6应用程序。甚至重定向到特定主页都可以正常工作。但是,当我关闭通知并稍后单击通知以从Windows通知托盘中调用通知时,通知事件将无法正常工作。通知处于活动状态时它正在工作。下面的屏幕截图将给出一个清晰的主意。

工作一个 enter image description here

不工作的人 enter image description here

我的代码

self.addEventListener('notificationclick', function (event) {
  console.log('On notification click: ', event.notification);
  // event.notification.close();
  var redirectUrl = null;

  console.log('event not data', event.notification.data);

  console.log('FCM_MSG', event.notification.data.FCM_MSG);

  var order_data = event.notification.data.FCM_MSG.data;
  console.log('order data', order_data);

  var business_id = order_data.business_id;
  console.log('b id', business_id);

  if (event.notification.data) {
    if (event.notification.data.FCM_MSG) { //seems that background notification shown by system sends data this way
       redirectUrl = event.notification.data.FCM_MSG.data ? event.notification.data.FCM_MSG.data.click_action : null 
      console.log('redirect url in if', redirectUrl);
    } else { //show manually using showNotification
      redirectUrl = event.notification.data ? event.notification.data.click_action : null
      console.log('redirect url in else', redirectUrl);
    }
  }
  if (redirectUrl) {
    // This looks to see if the current is already open and
    // focuses if it is
    event.waitUntil(clients.matchAll({
      type: "window"
    }).then(function (clientList) {
      for (var i = 0; i < clientList.length; i++) {
        var client = clientList[i];
        if (client.url == redirectUrl && 'focus' in client)
          return client.focus();
      }
      if (clients.openWindow) {
        const res = clients.openWindow(redirectUrl);
        return res;
      }
    }));
  }
});

// fired
self.addEventListener("notificationclose", function (event) {
  console.log('notification close');
  // log send to server
});

0 个答案:

没有答案