【发布时间】:2021-07-07 00:46:51
【问题描述】:
我在 React Native 中使用 Expo 的应用程序正在使用下面的代码向存储在 Firebase 中的所有令牌发送精美的通知。现在,当我在 Expo.io 网站上发布我的应用程序后,它就无法运行,并且根本不会向任何物理设备推送任何通知。我试图在我的本地机器上重建项目并从我的计算机上在我的手机上运行 expo,我控制台记录了信息并且所有的 expo 令牌信息都是正确的并且被正确地提取。为什么通知现在没有推送到任何物理设备?请帮忙。下面的 try 块执行没有错误,但为什么不推送通知呢?我的组件确实 mount 也调用了 loadInformation 和 generateToken,而我的渲染和返回只是在用户按下发送通知按钮时调用了 sendPushNotif 函数。
我的 this.state.finalArray 控制台记录以下内容:“数组 [
目的 { “身体”:“测试”, "title": "这是一个测试", “到”:数组 [ "ExponentPushToken[.......]", "ExponentPushToken[........]", ], }, ]"
constructor() {
super()
this.state = {
tokenArray: [],
finalArray: [],
}
}
loadInformation = async () => {
var tokens = []
firebase.database().ref('tokens/`).once('value', snapshot => {
const token = Object.values(snapshot.val());
token.map((item) => {
tokens.push(item.data)
return this.setState({ tokenArray: tokens })
})
})
}
generateToken = async () => {
this.state.finalArray = []
this.state.finalArray.push({
to: this.state.tokenArray,
title: this.state.title,
body: this.state.notification
})
}
sendPushNotification = async () => {
//these functions load all the token info from firebase and stores it into an array
this.loadInformation();
this.generateToken();
try {
let response = await fetch("https://exp.host/--/api/v2/push/send", {
method: "POST",
headers: {
Accept: "application/json",
"Accept-encoding": "gzip, deflate",
"Content-Type": "application/json",
},
body: JSON.stringify(this.state.finalArray), //final array has all the tokens and title and body info for the notif. i console logged this and it outputs the correct info
});
alert("Your notification was sent!");
} catch (error) {
console.log(error);
alert("Error! Your notification was not sent.");
}
};
【问题讨论】:
-
显示你的令牌....我的意思是告诉我
this.state.finalArray因为我认为你发送推送通知到ExpoPushToken而不是DevicePushToken -
@KartikeyVaish 我添加了初始化此数组的方式以及如何从 firebase 获取信息。我还放了 this.state.finalArray 显示的内容(我取出了实际的推送令牌,但它是正确的)。如果您能提供帮助,请告诉我。
标签: react-native push-notification expo