将Firebase与我的Web应用程序集成以发送通知

时间:2019-10-02 23:55:20

标签: javascript firebase firebase-cloud-messaging

我有点受不了Firebase的帮助,我们将不胜感激。 我的目标是无论打开我的页面的人都获得允许/阻止的通知。无论谁允许通知,我都希望以后通过https://console.firebase.google.com-云消息传递应用程序向他发送通知。

我已经取得了一些成就,当我通过https打开我的网页时,我得到了通知,我愿意还是不允许

enter image description here

允许后,当我尝试通过云消息向项目的所有参与者发送消息时,我什么也没得到。

首先,我设置Firebase配置

<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;
  }

在控制台日志中,我收到一条消息,指示令牌已发送到服务器。但是,当我尝试发送消息时,什么也没发生。 也在与索引文件位于同一文件夹

的firebase-messaging-sw.js中
// 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);
});

不确定出什么问题了吗?任何帮助将不胜感激

0 个答案:

没有答案