Expo具有用于发送推送通知的Notification API。但是,当应用程序处于后台时,发送推送通知如何工作?它们可以从服务器触发还是可以预先安排?
答案 0 :(得分:2)
发送推送通知是从您的服务器完成的,因此您完全可以随意发送通知,并随时安排工作来处理它。
但是,如果应用程序已后台运行且用户未选择通知,则您将无法在客户端上处理通知的接收。根据{{3}}:
您不能在后台处理推送通知。这是一部作品 进行中。
但是,如果应用程序处于前台状态或用户选择了通知,则通知将被传递到应用程序中的侦听器。
答案 1 :(得分:-2)
我们可以在后台处理推送通知。
为此,如果AppState为“有效”并且起点===“已接收”,我们必须取消通知。
这是完整的代码。
import React from 'react';
import { Text, View, Vibration, AppState } from 'react-native';
import { Notifications } from 'expo';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
notification: {}
}
}
componentDidMount() {
this._notificationSubscription = Notifications.addListener(
this._handleNotification
);
}
_handleNotification = notification => {
if (AppState.currentState == 'active' && notification.origin === 'received') {
Notifications.dismissNotificationAsync(notification.notificationId);
} else {
Vibration.vibrate()
this.setState({ notification: notification });
}
};
render() {
return (
<View><Text>Push Notifications Sample</Text></View>
);
}
}