服务工作者在推送通知中跟踪

时间:2017-12-27 04:02:43

标签: javascript push-notification service-worker

我想知道如何在显示推送通知后跟踪服务工作者点击事件。 我有注册服务工作者并发回推送通知但现在我想跟踪通知点击事件,无论用户是否打开通知并丢弃它。

self.addEventListener('push', function(event) { 
    const analyticsPromise = pushReceivedTracking(); 
    const pushInfoPromise = fetch('api/subscriber/msg/')
        .then(function(response) { return response.json(); })
        .then(function(response) {
            const title = response.data.userName + ' says...'; 
            const message = response.data.message; 
            return self.registration.showNotification(title, { body: message }); 
        }); 

    const promiseChain = Promise.all([ analyticsPromise, pushInfoPromise ]); 
    event.waitUntil(promiseChain);
 })

1 个答案:

答案 0 :(得分:1)

您可以处理服务工作者中的NotificationEvent

self.addEventListener('notificationclick', function (event) {
  event.notification.close();
  // track the notification click here
});

另请注意,您可以在通知中添加按钮 - 现在您只需跟踪通知本身的点击次数。

相关问题