【问题标题】:Expo Push Notifications Not Pushing to Physical Device世博会推送通知不推送到物理设备
【发布时间】: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


【解决方案1】:

您在this.state.finalArray 中拥有的令牌是每个 Expo Go 应用程序都拥有的 Expo Push 令牌。所以它不能在生产环境中工作,因为每个设备都有自己的devicePushToken

ExponentPushToken[........] 令牌仅适用于 Expo Go 应用程序,不适用于生产环境。见this

【讨论】:

  • 我的代码现在可以在 Expo Go 上运行,但仅当用户不在 Expo Go 应用程序中时才发送。怎么会?啊,这太可怕了,因为我也将在 Apple 上进行部署,所以我希望我能早点浏览这个文档 :( 谢谢你,但不知道为什么如果 expo go 正在运行我的代码不会发送。跨度>
  • 实际上很奇怪,通知在为 Apple 部署后的 beta 测试阶段为我工作?但同样,它们仅在用户当前未使用该应用程序(它们在主屏幕或另一个应用程序中)时才起作用。好奇怪
【解决方案2】:

这就是答案 作为博览会处理 IOS 的通知注册,但对于 android,您必须使用 firebase 注册您的应用程序

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

【讨论】:

    猜你喜欢
    • 2021-03-02
    • 1970-01-01
    • 2022-06-19
    • 2019-08-24
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多