gpt4 book ai didi

react-native - Expo 推送通知 - 当应用程序处于前台时,它会导致 iOS 崩溃

转载 作者:行者123 更新时间:2023-12-04 05:11:04 32 4
gpt4 key购买 nike

我有一个 Expo 应用程序,我正在尝试处理该应用程序在前台时发送的推送通知。它在 Android 中运行良好,但在 iOS 中它在收到应用程序时崩溃了。

我有一个从 Rails 服务器发送的推送通知:

            params = ({
to: token.expo_push_token,
title: user.first_name,
sound: "default",
body: msg.body,
})


puts params
puts params.class

x = Net::HTTP.post_form(URI.parse('https://exp.host/--/api/v2/push/send'), params)
puts x.body

我可以在它发送的服务器中看到:
Hash
app[worker.1]: {"data":{"id":"9058abf3-7352-4181-a69d-0b5fc8a8525c","status":"ok"}}
4 TID-godk4ew98 ExpoPushWorker JID-51b823f8feeaf42c313e392e INFO: done: 2.005 sec

如果应用程序关闭,推送通知会出现在锁定屏幕上。如果应用程序在前台打开,则不会发生任何事情。

我想在应用程序打开时监听通知,我在 App.js 中有这个:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import MessagesScreenRouter from './app/components/Router/MessagesScreenRouter';
import Sentry from 'sentry-expo';
import reducers from './app/reducers';

import { Provider } from 'react-redux';
import { createStore } from 'redux';
import { Notifications } from 'expo';


export default class App extends React.Component {

constructor(props) {
super(props)
this.state = {
notification: {},
}
this._handleNotification = this._handleNotification.bind(this)
}

_handleNotification = (notification) => {
console.log(notification)
this.setState({notification: notification});
};

componentWillUnmount(){
this._notificationSubscription && this._notificationSubscription.remove()
}

componentDidMount() {
this.notificationSubscription = Notifications.addListener(
(notification) => this._handleNotification(notification),
);
}


render() {
return (
<View style={{flex:1}}>
<StatusBar hidden={true} />

<View style={{height: 50, justifyContent: 'center', alignItems: 'center'}}>
<Text>Origin: {this.state.notification.origin}</Text>
<Text>Data: {JSON.stringify(this.state.notification)}</Text>
</View>

<Provider store={createStore(reducers)}>
<MessagesScreenRouter/>
</Provider>
</View>
);
}
}

我一整天都尝试了教程中的许多建议,但我无法让它发挥作用。我在这里缺少什么?

最佳答案

正如世博会文档所述,您无法在模拟器上对其进行测试

Note: iOS and Android simulators cannot receive push notifications. To test them out you will need to use a real-life device. Additionally, when calling Permissions.askAsync on the simulator, it will resolve immediately with "undetermined" as the status, regardless of whether you choose to allow or not.



就跑 exp publish并在世博客户端上进行测试。此外,您必须使用 Permissions.askAsync 获得许可。首先。

Doc 的示例工作就像一个魅力,请查看: https://docs.expo.io/versions/v28.0.0/guides/push-notifications#__next

关于react-native - Expo 推送通知 - 当应用程序处于前台时,它会导致 iOS 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52014006/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com