代码之家  ›  专栏  ›  技术社区  ›  iRyanBell

跨主要浏览器和移动设备发送和接收推送通知

  •  3
  • iRyanBell  · 技术社区  · 9 年前

    我注册了一个谷歌云服务帐户,创建了一个新的Firebase项目,并下载了我的服务凭据JSON文件。

    遵循本指南: https://firebase.google.com/docs/cloud-messaging/js/client

    我将其添加到我的web客户端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);
    });
    

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

    现在,节点。js告诉我 "Successfully sent message" ,但我在Chrome网络客户端中没有收到任何信息。在Safari中,我看到了错误: This browser doesn't support the API's required to use the firebase SDK.

    对于我来说,最简单的方法是通过推送通知在主要浏览器中发送消息给各个客户端;移动设备?

    1 回复  |  直到 9 年前
        1
  •  1
  •   Manpreet singh    9 年前

    Safari尚不支持FCM。 See here

    现在,关于为什么你没有收到信息,首先你必须了解实际发生了什么。

    但Firebase server不了解您的客户端。您的客户端必须订阅要接收通知的管理服务器。

    要订阅服务器,您必须使用服务器密钥发出post请求。 see this