2017-02-17 85 views
1

我試圖使用FCM(Firebase Cloud Messaging)進行Web推送通知。 當Web應用程序選項卡處於焦點時,我已成功收到消息。FCM服務人員setBackgroundMessageHandler

但是當我關閉標籤時,應該調用服務人員(在FCM示例中爲firebase-messaging-sw.js)。但它根本不叫setBackgroundMessageHandler

const messaging = firebase.messaging(); 
messaging.setBackgroundMessageHandler(function(payload) { 
    console.log('[firebase-messaging-sw.js] Received background message ', payload); 
    const notificationTitle = 'Background Message Title'; 
    const notificationOptions = { 
    body: 'Background Message body.', 
    icon: 'firebase-logo.png' 
    }; 

    return self.registration.showNotification(notificationTitle, 
     notificationOptions); 
}); 
+1

您是否設置了'messagingSenderId'?因爲除此之外,您的代碼看起來與我所擁有的相同 - 並且這很有效。 : -/ –

+0

是否關閉了該選項卡?我認爲它適用於選項卡未關閉且不活動的情況 –

回答

0

包括以下在manifest.json文件: {"gcm_sender_id": "103953800507"}gcm_sender_id是所有FCM用戶獨特的,它不是具體到項目中。

在您的服務工作者代碼中添加您的項目特定的發件人ID。 服務人員代碼的工作示例是here

當您的網站選項卡未處於焦點/關閉狀態並且瀏覽器處於打開狀態時,它應顯示後臺通知。