首页 > 解决方案 > 世博会推送通知不推送到物理设备

问题描述

我在 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.");
  }
};

标签: react-nativepush-notificationexpo

解决方案


这是答案,因为 expo 处理 IOS 的通知注册,但对于 android,您必须使用 firebase 注册您的应用程序

因此,您必须为此向 firebase 注册您的应用程序单击此处以获取完整信息 https://docs.expo.dev/push-notifications/using-fcm/ 提及所有步骤或视频信息单击此处 https://www .youtube.com/watch?v=oRNwNintDW0


推荐阅读