问题
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?
回答1:
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!
来源:https://stackoverflow.com/questions/61300018/typeerror-firebase-default-messaging-is-not-a-function-in-firebase-default-m