TypeError:firebase.default.messaging不是函数(在'_firebase.default.messaging()'中,'firebase.default.messaging未定义) [英] TypeError: firebase.default.messaging is not a function (in '_firebase.default.messaging()','firebase.default.messaging is undefined)

查看:81
本文介绍了TypeError:firebase.default.messaging不是函数(在'_firebase.default.messaging()'中,'firebase.default.messaging未定义)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Firebase云功能发送通知,所以我试图使用firebase.messaging().getToken()获取令牌,但我不断收到错误消息:

TypeError:firebase.default.messaging不是函数(在'_firebase.default.messaging()'中,'firebase.default.messaging未定义)

我已经安装了大约5种不同方式的firebase和firebase/消息,但似乎无法克服此错误,因此我认为这些方法必须已过时,或者我做错了什么.

这是我的代码:

  import *作为'firebase'中的firebaseconst Firebase = firebase.initializeApp(firebaseConfig);导出默认Firebase 

这是我得到错误的地方:

  Token:Firebase.messaging().getToken() 

我不确定是否缺少某些依赖项或任何此类依赖项,因此我们将非常感谢您的帮助.我一直在从Firebase文档中直接安装东西,因为它到目前为止已经可以正常使用.

我也正在使用EXPO管理的本机响应

谢谢

还有没有其他更好的方法可以在后台使用事件监听器发送通知?

解决方案

自您发布以来已经有一段时间了,但是我遇到了同样的错误,因此我将分享一些发现.您正在寻找的解决方案是项目符号3.

1)Firebase JavaScript SDK适用于Node.js应用程序.即使firebase sdk软件包可以像react-native/expo项目中的任何其他npm软件包一样直接安装和使用,它也可以在浏览器或node.js运行时上运行.

这特别令人困惑,因为某些firebase Api方法(例如firebase.auth和firebase.database)可以在本机应用程序上运行,而其他方法则不能.我猜这是由于某些方法仅依赖于执行HTTP请求的能力所致.

实际上,firebase sdk的官方文档页面上没有react-native部分.如果您查看

如果您仍要使用Firebase SDK,则应考虑使用诸如 React Native Firebase 之类的库,解决在node.js运行时之外使用JavaScript sdk的问题.我还没有尝试过自己,但是请访问他们的网站,似乎很难将其集成到博览会管理的工作流程中.

2)也不能导入@ firebase/messaging :如此答案,显式导入消息传递模块将无法解决react-native/expo项目的问题.同样,如上所述,因为本机应用程序不能在node.js/browser运行时上运行.实际上,这使事情变得更糟,因为它试图加载不可用的依赖项:

  [09:56:33] ReferenceError:找不到变量:IDBIndex 

3)在Expo中获取用户设备通知推送令牌:由于您已经在使用expo,因此可以使用 firebase-admin SDK .以下是一些示例代码,用于获取推送令牌并将其存储在实时数据库中(稍后将通过云功能使用):

  import *作为来自"expo-notifications"的通知;从"expo-permissions"导入*作为权限;从"firebase"导入firebase;export const getDevicePushToken =()=>{返回Permissions.getAsync(Permissions.NOTIFICATIONS).then((response)=>response.status ==='已授予'?回复:Permissions.askAsync(Permissions.NOTIFICATIONS)).then((response)=> {如果(response.status!=='granted'){返回Promise.reject(new Error('推送通知权限被拒绝'));}返回Notifications.getDevicePushTokenAsync();}).then(令牌=> {firebase.database().ref('...').update({pushToken:token.data});}).catch((错误)=> {console.log('注册设备推送令牌时出错',错误);});}; 

干杯!

I want to send notifications using firebase cloud functions so I am trying to get a token using firebase.messaging().getToken() but I keep getting the error:

TypeError: firebase.default.messaging is not a function (in '_firebase.default.messaging()','firebase.default.messaging is undefined)

I have installed firebase and firebase/messages about 5 different ways but cannot seem to get past this error so I assume that those methods must be outdated or I am doing something very wrong.

Here is my code:

import * as firebase from 'firebase'
const Firebase = firebase.initializeApp(firebaseConfig);
export default Firebase

and here is where I am getting the error:

Token : Firebase.messaging().getToken()

I am not sure if I am missing some dependency or anything of that sort so any help would be very appreciated. I have been installing things straight from the firebase documentation as web which has been working so far.

I am also using expo managed react native

Thank you

Also is there some alternative way to send notifications in the background with an event listener that may be better?

解决方案

It's been a while since you posted, but I've gone through the same error so I will share some findings about it. The solution you are looking for is bullet number 3.

1) Firebase JavaScript SDK is meant for Node.js applications. Even though the firebase sdk package can be installed and used straightaway just like any other npm package in a react-native/expo project, it's meant to run on a browser or node.js runtime.

It's specially confusing because some of the firebase Api methods (e.g. firebase.auth and firebase.database) do work on native apps while others don't. I guess that's due to the fact that some methods only rely on the hability to perform HTTP requests.

In deed the firebase sdk official documentation page doesn't have a react-native section. If you check out the JavaScript section, and click on Add Firebase to your JavaScript Project you will see that the intended uses comprehend web apps and node.js desktop apps (not sure which runtime IoT apps run on):

If you still want to use the firebase sdk you should then consider libraries such as React Native Firebase which aims to solve the issues of using the JavaScript sdk outside a node.js runtime. I haven't tried myself but having a look at their website, looks like it's not going to be easy to integrate in the expo managed workflow.

2) Importing @firebase/messaging is not an option either: As suggested in this answer, explicitly importing the messaging module will not solve the issue for react-native/expo projects. Again, as described above, because native apps do not run on a node.js/browser runtime. In fact, it makes things worst because it tries to load dependencies that aren't available:

[09:56:33] ReferenceError: Can't find variable: IDBIndex

3) Getting the user device notifications push token in Expo: Since you are already using expo, you can use the expo-notifications module to get the device push token, which will return the token you then can use from Cloud Functions to send a notification through the firebase-admin sdk. Here is some sample code to get the push token and storing it in the realtime database (to be later used from the cloud functions):

import * as Notifications from 'expo-notifications';
import * as Permissions from 'expo-permissions';
import firebase from 'firebase';

export const getDevicePushToken = () => {
    return Permissions.getAsync(Permissions.NOTIFICATIONS)
        .then((response) =>
            response.status === 'granted'
                ? response
                : Permissions.askAsync(Permissions.NOTIFICATIONS)
        )
        .then((response) => {
            if (response.status !== 'granted') {
                return Promise.reject(new Error('Push notifications permission was rejected'));
            }

            return Notifications.getDevicePushTokenAsync();
        })
        .then(token => {
            firebase.database().ref('...').update({ pushToken: token.data });
        })
        .catch((error) => {
            console.log('Error while registering device push token', error);
        });
};

Cheers!

这篇关于TypeError:firebase.default.messaging不是函数(在'_firebase.default.messaging()'中,'firebase.default.messaging未定义)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆