我是React Native的新手,对后端不太熟悉,所以我将expo和firebase一起用于推送通知。 我所有的代码运行良好,没有任何错误,但是每当我运行它时,它都不会将我的令牌存储到firebase中。这是我的componentDidMount():
componentDidMount() {
var currentUser
var that = this
listener => db.auth().onAuthStateChanged(function (user) {
if (user != null) {
currentUser = user
that.registerForPushNotificationsAsync(currentUser)
}
listener();
});
db.database().ref('/posts').on('child_added', function (data) {
var newData = [...that.state.listViewData]
newData.push(data)
that.setState({ listViewData: newData })
})
}
```
And further my expo function for permissions and storing token is as follow:
```
registerForPushNotificationsAsync = async (currentUser) => {
const { existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
let finalStatus = existingStatus;
// only ask if permissions have not already been determined, because
// iOS won't necessarily prompt the user a second time.
if (existingStatus !== 'granted') {
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}
// Stop here if the user did not grant permissions
if (finalStatus !== 'granted') {
return;
}
// Get the token that uniquely identifies this device
let token = await Notifications.getExpoPushTokenAsync();
// POST the token to our backend so we can use it to send pushes from there
var updates = {}
updates['/expoToken'] = token
await db.database().ref('/coordinator/' + currentUser.uid).update(updates)
//call the push notification
}
```
[![This is my db on which expo token has to be stored][1]][1]
[1]: https://i.stack.imgur.com/mT2A2.png