[英]Firebase Cloud Messaging - Error calling firebase.messaging()
I am getting an error when calling firebase.messaging()
from the html page (not the service worker) that makes no sense. 从html页面(不是服务工作者)调用
firebase.messaging()
时出现错误,这没有任何意义。
Messaging: Please ensure that 'messagingSenderId' is set correctly in the options passed into firebase.initializeApp().
消息传递:请确保在传递给firebase.initializeApp()的选项中正确设置了“ messagingSenderId”。 (messaging/bad-sender-id).
(消息/坏发送者ID)。
The app IS initialized with the correct messagingSenderId
in a <script>
further up in the <head>
. 应用程序是用正确的初始化
messagingSenderId
在一个<script>
进一步向上在<head>
The default app is shown in the console log: 默认应用程序显示在控制台日志中:
function deviceRegistration() {
console.log('in deviceRegistration()');
console.log("firebase.apps:", firebase.apps);
console.log("firebase.apps.length: ", firebase.apps.length);
if (firebase.apps.length == 0) {
// firebase.initializeApp({{ fcm_config.messagingSenderId }});
firebase.initializeApp({"messagingSenderId": "1234567890"});
}
console.log("after initializeApp"); // prints to console
var messaging = firebase.messaging(); // error happens here
console.log("after firebase.messaging()"); // doesn't print to console
The reason I only call firebase.initializeApp
if there are no apps in firebase.apps
is because when I call firebase.initializeApp
without first performing this check I get the following error: 如果在
firebase.apps
中没有应用程序,我之所以只调用firebase.initializeApp
,是因为当我在不首先执行此检查的情况下调用firebase.initializeApp
时,出现以下错误:
Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).
Firebase:名为“ [DEFAULT]”的Firebase应用已存在(应用/重复应用)。
I thought I'm supposed to be able to call initializeApp
repeatedly (on every page) and it would register the service worker if necessary... 我以为我应该能够(在每个页面上)重复调用
initializeApp
并且如果需要的话,它将注册服务工作者。
Can someone please help me figure out what's going on? 有人可以帮我弄清楚发生了什么吗?
Oh silly me... 噢,愚蠢的我...
I populated the config variable with a (django) template tag and context variable that I incorrectly referenced. 我用(django)模板标记和错误引用的上下文变量填充config变量。 I did this:
我这样做:
var config = {
apiKey: "{{ fcm_config.apiKey }}",
authDomain: "{{ fcm_config.authDomain }}",
databaseURL: "{{ fcm_config.databaseURL }}",
projectId: "{{ fcm_config.projectId }}",
storageBucket: "{{ fcm_config.storageBucket }}",
messagingSenderId: "{{ fcm_config.messagingSenderId }}"
};
firebase.initializeApp(config);
When I meant to do this: 当我打算这样做时:
var config = {
apiKey: "{{ fcm_config.config.apiKey }}",
authDomain: "{{ fcm_config.config.authDomain }}",
databaseURL: "{{ fcm_config.config.databaseURL }}",
projectId: "{{ fcm_config.config.projectId }}",
storageBucket: "{{ fcm_config.config.storageBucket }}",
messagingSenderId: "{{ fcm_config.config.messagingSenderId }}"
};
firebase.initializeApp(config);
So firebase was right to complain about a bad sender ID, since I was passing in an object equivalent to: 因此,firebase抱怨发件人ID错误是正确的,因为我传入的对象等同于:
firebase.initializeApp({
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
});
Now that part works and I'm on to the next error! 现在这部分工作了,我继续下一个错误!
(if you're wondering why I put those variables in fcm_config.config.*
, which seems redundantly redundant, it's because I also store fcm_config.VAPID
) (如果您想知道为什么将这些变量放在
fcm_config.config.*
,这似乎是多余的,那是因为我还存储了fcm_config.VAPID
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.