【问题标题】:Service Worker tracking in push notification推送通知中的 Service Worker 跟踪
【发布时间】:2017-12-27 04:02:42
【问题描述】:

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

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);
 })

【问题讨论】:

  • 你能分享一些你到目前为止写的代码吗?您能描述一下您遇到的问题吗?
  • 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); });

标签: javascript push-notification service-worker


【解决方案1】:

您可以在 Service Worker 中处理 NotificationEvent

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多