2016-12-21 17 views
3

FCMを使用してWeb通知を送信しています。 この警告が表示され、通知をクリックしても通常の結果は得られません(通知URLを開きます)。これは、これは、これはクロム版55上に、このライン108 reffersGoogle Chromeのサービスワーカー警告

self.addEventListener('notificationclick', function(event) {

警告メッセージであり、サービス・ワーカーコード

importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-app.js'); 
 
importScripts('https://www.gstatic.com/firebasejs/3.5.2/firebase-messaging.js'); 
 
firebase.initializeApp({ 
 
'messagingSenderId': '<my senderid>' 
 
}); 
 

 
const messaging = firebase.messaging(); 
 

 

 
messaging.setBackgroundMessageHandler(function(payload) { 
 

 

 
    self.addEventListener('notificationclick', function(event) { 
 
    event.notification.close(); 
 

 
    var promise = new Promise(function(resolve) { 
 
     setTimeout(resolve, 1000); 
 
    }).then(function() { 
 
     return clients.openWindow(payload.data.locator); 
 
    }); 
 

 
    event.waitUntil(promise); 
 
    }); 
 

 
    var notificationTitle = payload.data.title; 
 
    var notificationOptions = { 
 
    body: payload.data.body, 
 
    icon: payload.data.icon 
 
    }; 
 
    return self.registration.showNotification(notificationTitle, 
 
    notificationOptions); 
 
});

firebase-messaging-sw.js:108 Event handler of 'notificationclick' event must be added on the initial evaluation of worker script. 

あります。 firefoxの 、何も間違っていて、正常に正常に動作します。 ありがとうございます。

+1

のようになります、それは予想されたURLを開いていますが、クロムの各インスタンスでちょうど最初の通知。 –

答えて

9

あなたは

messaging.setBackgroundMessageHandler 

だから、あなたは一度だけのEventListenerを追加することを関数の外

self.addEventListener 

を移動する必要があります。

あなたの完全なコードは、より具体的に

const messaging = firebase.messaging(); 

self.addEventListener('notificationclick', function(event) { 
    event.notification.close(); 

    var promise = new Promise(function(resolve) { 
     setTimeout(resolve, 1000); 
    }).then(function() { 
     return clients.openWindow(event.data.locator); 
    }); 

    event.waitUntil(promise); 
    }); 

messaging.setBackgroundMessageHandler(function(payload) { 

    var notificationTitle = payload.data.title; 
    var notificationOptions = { 
    body: payload.data.body, 
    icon: payload.data.icon, 
    locator:payload.data.locator 
    }; 
    return self.registration.showNotification(notificationTitle, 
    notificationOptions); 
});