Javascript 将Firebase与我的Web应用程序集成以发送通知
我被firebase困住了,任何帮助都将不胜感激。 我的目标是,无论谁打开我的页面,都会收到允许/阻止的通知。无论谁允许通知,我都想在未来的云消息应用程序中向他发送通知 我已经取得了一些成就,当我通过https打开我的网页时,我收到了我想允许还是不允许的通知 在我允许之后,当我尝试通过云向项目的所有参与者发送消息时,我什么也得不到 首先,我设置firebase配置Javascript 将Firebase与我的Web应用程序集成以发送通知,javascript,firebase,firebase-cloud-messaging,Javascript,Firebase,Firebase Cloud Messaging,我被firebase困住了,任何帮助都将不胜感激。 我的目标是,无论谁打开我的页面,都会收到允许/阻止的通知。无论谁允许通知,我都想在未来的云消息应用程序中向他发送通知 我已经取得了一些成就,当我通过https打开我的网页时,我收到了我想允许还是不允许的通知 在我允许之后,当我尝试通过云向项目的所有参与者发送消息时,我什么也得不到 首先,我设置firebase配置 <script> // Your web app's Firebase configuration var
<script>
// Your web app's Firebase configuration
var config = {
apiKey: "AIzaSyDX5nRZCEI-wxp7L7TzI3lYQXlFlIZq7Rw",
authDomain: "braintech-6f3cf.firebaseapp.com",
databaseURL: "https://xxxx.firebaseio.com",
projectId: "braintech-6f3cf",
storageBucket: "",
messagingSenderId: "xxxx",
appId: "1:747770571255:web:da58976e57ebeb0835a6f2",
measurementId: "G-73HKECM57P"
};
// Initialize Firebase
firebase.initializeApp(config);
// Retrieve Firebase Messaging object.
const messaging = firebase.messaging();
function setTokenSentToServer(sent) {
window.localStorage.setItem('sentToServer', sent ? '1' : '0');
}
function sendTokenToServer(currentToken) {
if (!isTokenSentToServer()) {
console.log('Sending token to server...');
// TODO(developer): Send the current token to your server.
setTokenSentToServer(true);
} else {
console.log('Token already sent to server so won\'t send it again ' +
'unless it changes');
}
}
function isTokenSentToServer() {
return window.localStorage.getItem('sentToServer') === '1';
}
function getRegisterToken(){
// Get Instance ID token. Initially this makes a network call, once retrieved
// subsequent calls to getToken will return from cache.
messaging.getToken().then((currentToken) => {
if (currentToken) {
sendTokenToServer(currentToken);
console.log(currentToken);
updateUIForPushEnabled(currentToken);
} else {
// Show permission request.
console.log('No Instance ID token available. Request permission to generate one.');
// Show permission UI.
updateUIForPushPermissionRequired();
setTokenSentToServer(false);
}
}).catch((err) => {
console.log('An error occurred while retrieving token. ', err);
showToken('Error retrieving Instance ID token. ', err);
setTokenSentToServer(false);
});
}
function showToken(currentToken) {
// Show token in console and UI.
const tokenElement = document.querySelector('#token');
tokenElement.textContent = currentToken;
}
不知道怎么了?任何帮助都将不胜感激
// Give the service worker access to Firebase Messaging.
// Note that you can only use Firebase Messaging here, other Firebase libraries
// are not available in the service worker.
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/6.3.4/firebase-messaging.js');
// Initialize the Firebase app in the service worker by passing in the
// messagingSenderId.
var config = {
apiKey: "AIzaSyDX5nRZCEI-wxp7L7TzI3lYQXlFlIZq7Rw",
authDomain: "xxxx.firebaseapp.com",
databaseURL: "https://xxx.firebaseio.com",
projectId: "xxx",
storageBucket: "",
messagingSenderId: "xxxx",
appId: "1:747770571255:web:da58976e57ebeb0835a6f2",
measurementId: "G-73HKECM57P"
};
// Initialize Firebase
firebase.initializeApp(config);
// Retrieve Firebase Messaging object.
// Retrieve an instance of Firebase Messaging so that it can handle background
// messages.
const messaging = firebase.messaging();
messaging.setBackgroundMessageHandler(function(payload) {
console.log('[firebase-messaging-sw.js] Received background message ', payload);
// Customize notification here
const notificationTitle = 'Background Message Title';
const notificationOptions = {
body: 'Background Message body.',
icon: '/firebase-logo.png'
};
return self.registration.showNotification(notificationTitle,
notificationOptions);
});