react-native - 世博会推送通知不推送到物理设备
问题描述
我在 React Native 中使用 Expo 的应用程序正在使用下面的代码向存储在 Firebase 中的所有令牌发送通知。现在,当我在 Expo.io 网站上发布我的应用程序后,它就无法运行,并且根本不会向任何物理设备推送任何通知。我试图在我的本地机器上重建项目并从我的计算机上在我的手机上运行 expo,我控制台记录了信息并且所有的 expo 令牌信息都是正确的并且被正确地提取。为什么通知现在没有推送到任何物理设备?请帮忙。下面的 try 块执行没有错误,但为什么不推送通知呢?我的组件确实 mount 也调用了 loadInformation 和 generateToken,当用户按下发送通知按钮时,我的渲染和返回只是调用 sendPushNotif 函数。
我的 this.state.finalArray 控制台记录以下内容:“Array [
Object { "body": "test", "title": "this is a test", "to": Array [ "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.");
}
};
解决方案
这是答案,因为 expo 处理 IOS 的通知注册,但对于 android,您必须使用 firebase 注册您的应用程序
因此,您必须为此向 firebase 注册您的应用程序单击此处以获取完整信息 https://docs.expo.dev/push-notifications/using-fcm/ 提及所有步骤或视频信息单击此处 https://www .youtube.com/watch?v=oRNwNintDW0
推荐阅读
- gem5 - 优先读取优先于写入
- tensorflow - Keras LSTM 的维度问题
- apache-spark - VectorAssembler 强制数据进入驱动程序。我们怎样才能避免呢?
- eclipse - 我应该在 LiClipse 中注册哪些“可用软件站点”才能成功升级?
- flutter - 单击复选框或按钮时,如何检测是否按下了键?
- matlab - 图像中的平均像素(Matlab)
- r - 在 R 中使用 rjson 包读取 iso_3166_2.js
- python - 梯度下降图是如何工作的
- kotlin - 使用 Dagger Hilt 解决多个字符串的注入
- amazon-redshift - 将 `GETDATE()` Redshift 函数与 psycopg2 execute_values 一起使用