2017-07-02 58 views
2

我註冊了Google Cloud Services帳戶,創建了新的Firebase項目並下載了我的服務憑據JSON文件。跨主流瀏覽器和移動設備的發送和接收推送通知

在這個指南: https://firebase.google.com/docs/cloud-messaging/js/client

我將此添加到我的網絡客戶端HTML(爲了獲得一個客戶端註冊令牌並接受推送通知):

<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"> 
</script> 
<script> 
firebase.initializeApp({ 
    apiKey: "_APIKEY_", 
    authDomain: "_ID_.firebaseapp.com", 
    databaseURL: "https://_ID_.firebaseio.com", 
    projectId: "_ID_", 
    storageBucket: "_ID_.appspot.com", 
    messagingSenderId: "_SENDERID_" 
}); 
/* Is the API Key supposed to be public-facing? */ 

const messaging = firebase.messaging(); 
messaging.requestPermission().then(function() { 
    console.log('Notification permission granted.'); 

    messaging.getToken().then(function(currentToken) { 
    if (currentToken) { 
     console.log(currentToken) 
    } else { 
     console.log('No Instance ID token available. Request permission to generate one.'); 
    } 
    }).catch(function(err) { 
    console.log('An error occurred while retrieving token. ', err); 
    }); 
}).catch(function(err) { 
    console.log('Unable to get permission to notify.', err); 
}); 

messaging.onTokenRefresh(function() { 
    messaging.getToken().then(function(refreshedToken) { 
    console.log(refreshedToken) 
    }).catch(function(err) { 
    console.log('Unable to retrieve refreshed token ', err); 
    }); 
}); 
</script> 

我抓起註冊令牌由客戶端生成並添加服務人員(firebase-messaging-sw.js):

importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-app.js'); 
importScripts('https://www.gstatic.com/firebasejs/3.9.0/firebase-messaging.js'); 

firebase.initializeApp({ 
    'messagingSenderId': '_ID_' 
}); 

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: '_ICON_' 
    }; 

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

然後,downloa DED的火力地堡聯繫故宮模塊和安裝使用客戶端令牌的基本推送通知測試模板:

var admin = require("firebase-admin"); 
    var serviceKey = require("./_KEY_.json"); 

    admin.initializeApp({ 
    credential: admin.credential.cert(serviceKey), 
    databaseURL: "https://_PROJECTID_.firebaseio.com" 
    }); 

    var registrationToken = "_TOKEN_"; 

    var notification = { 
    data: { 
     msg:"Hello!" 
    } 
    }; 

    admin.messaging().sendToDevice(registrationToken, notification) 
    .then(function(response) { 
     console.log("Successfully sent message:", response); 
    }) 
    .catch(function(error) { 
     console.log("Error sending message:", error); 
    }); 

現在,Node.js的告訴我,它"Successfully sent message",但我不會在Chrome網絡接收什麼客戶。在Safari中,我看到錯誤:This browser doesn't support the API's required to use the firebase SDK.

對我來說簡單的方法是通過主要瀏覽器上的推送通知簡單地向各個客戶端發送消息&移動設備?

+0

對我來說,推送通知最簡單的方式是https://onesignal.com/ – daggett

回答

0

Safari尚不支持FCM。 See here

現在爲什麼你沒有收到消息,首先你必須明白實際發生了什麼。

客戶端正在瀏覽器中生成令牌(這就是generateToken方法的作用),並且Firebase管理員正在運行fcm服務器。

但Firebase服務器不知道您的客戶端。您的客戶必須訂閱要接收通知的管理服務器。

要訂閱服務器,您必須使用服務器密鑰進行發佈請求。 see this

+0

如果我想用* Safari,Chrome,Firefox等來兼容*,我可以用什麼來代替FCM? – arby

+0

在這種情況下,Socket.io是最好的選擇 –

相關問題