简体   繁体   English

无需托管的Firebase云消息传递(Web / JavaScript)

[英]Firebase Cloud Messaging without hosting (Web/JavaScript)

When working through the official video tutorial for Firebase Cloud Messaging, I am not able to get a messaging token without hosting the application. 在浏览Firebase Cloud Messaging的官方视频教程时,如果不托管应用程序,则无法获得消息传递令牌。

Here is my app.js file: 这是我的app.js文件:

/* global firebase */

// Initialize Firebase
var config = {
  apiKey: 'AIzaSyBYfb9HAi_oE-PKqFNkRQcxAgLU-nm8sIE',
  authDomain: 'web-quickstart-c0309.firebaseapp.com',
  databaseURL: 'https://web-quickstart-c0309.firebaseio.com',
  projectId: 'web-quickstart-c0309',
  storageBucket: 'web-quickstart-c0309.appspot.com',
  messagingSenderId: '713880824056'
}
firebase.initializeApp(config)

const messaging = firebase.messaging()
messaging.requestPermission()
  .then(() => {
    console.log('Permission granted.')
    return messaging.getToken()
      .then(token => {
        console.log('messaging token test:', token)
        return token
      })
      .catch(error => {
        console.log('messaging error:', error)
      })
  })
  .then(token => {
    console.log('permission token test:', token)
  })
  .catch(error => {
    console.log('permission error:', error)
  })

I have a firebase-messaging-sw.js file in my root directory. 我的根目录中有一个firebase-messaging-sw.js文件。

When I load the index.html file directly in my browser and accept the dialog, I receive an undefined value for the token. 当我直接在浏览器中加载index.html文件并接受对话框时,我收到该令牌的未定义值。 The full console output is: 完整的控制台输出为:

16:20:35.744 app.js:17 Permission granted.
16:20:35.750 app.js:20 messaging token test: null
16:20:35.751 app.js:28 permission token test: null

If I host the application by editing the firebase.json file to read: 如果我通过将firebase.json文件编辑为以下内容来托管应用程序:

{
  "hosting": {
    "public": "./"
  }
}

And then run firebase serve -p 8081 , open http://localhost:8081 , and accept the dialog, I do receive a token. 然后运行firebase serve -p 8081 ,打开http://localhost:8081 ,并接受对话框,我确实收到了令牌。 The full output is: 完整的输出是:

16:23:42.902 app.js:17 Permission granted.
16:23:43.059 app.js:20 messaging token test: eyd1EaFwULQ:APA91bGUZr9fAGcCaYVtXTPjk55AmpWLNdaqGapMa1S1GWTYeJwtJraEKuhAPpSM-v-2xPaSJQgTKRVosTN-0KRPHCccjdRZNDkegtW2HMC_mSbdap9h5TeH7KKQSbN4QrjVmIl7VZlu
16:23:43.060 app.js:28 permission token test: eyd1EaFwULQ:APA91bGUZr9fAGcCaYVtXTPjk55AmpWLNdaqGapMa1S1GWTYeJwtJraEKuhAPpSM-v-2xPaSJQgTKRVosTN-0KRPHCccjdRZNDkegtW2HMC_mSbdap9h5TeH7KKQSbN4QrjVmIl7VZlu

Is this a documented constraint? 这是有记录的约束吗? Is there a way to receive a token without hosting the application? 是否可以在不托管应用程序的情况下接收令牌?

FCM work only in pages served by https protocol FCM仅在https协议提供的页面中工作

as in the document 文件中所示

The FCM SDK is supported only in pages served over HTTPS. 仅在通过HTTPS服务的页面中支持FCM SDK。 This is due to its use of service workers, which are available only on HTTPS sites. 这是由于使用了服务工作者,仅在HTTPS站点上可用。 Need a provider? 需要提供者? Firebase Hosting is an easy way to get free HTTPS hosting on your own domain. Firebase托管是一种在您自己的域上获取免费HTTPS托管的简便方法。

and if you wonder why localhost work that because pages in localhost work just like they served by https 以及您是否想知道为什么localhost可以工作,因为localhost中的页面就像由https服务一样

so Is there a way to receive a token without hosting the application? 所以 有没有办法在不托管应用程序的情况下接收令牌?

The answer is No your page should be served by https protocol 答案是您的网页不应由https协议提供服务

I wouldn't say this is the exact alternative but an alternative solution to the problem , you can use the ngrok , which will provide you with the Https access for alternative way of representing the localhost . 我不会说这是确切的替代方案,但是是解决该问题的替代方案,您可以使用ngrok ,它将为您提供Https访问权限,以表示localhost替代方法。

So after installing use whatever URL it outputs on the terminal from ngrok in the FCM. 因此,安装后,请使用FCM中ngrok在终端上输出的任何URL。

Note : ngrok outputs both http and https URLs, so use Only Https . 注意 :ngrok同时输出httphttps URL,因此请使用Only Https They still map to your local server. 它们仍然映射到您的本地服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM