簡體   English   中英

推送通知中的 Service Worker 跟蹤

[英]Service Worker tracking in 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);
 })

您可以在 Service Worker 中處理NotificationEvent

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

另請注意,您可以在通知中添加按鈕 - 現在您只能跟蹤對通知本身的點擊。

暫無
暫無

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

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