【发布时间】:2019-02-06 14:37:28
【问题描述】:
我希望能够在每次用户从任何状态打开应用程序时检查用户是否具有通知权限(如果应用程序之前从未打开过或应用程序在后台等) 我有什么现在正在通过渲染函数中的 PushNotificationIOS.checkPermissions 检查权限。但是,使用该方法会继续调用它并导致内存泄漏。我尝试的另一种方法是,每当用户想要收到通知(通过按下图标)时,都会调用 onPress 方法来检查权限,但这会导致用户按下图标两次以更新权限状态。
import {
PushNotificationIOS,
AsyncStorage,
..
} from "react-native";
export default class Bell extends React.Component {
constructor(props) {
super(props);
this.state = {
isNotifActive: null,
isBellActive: false,
alertBody:"",
fireDate: null,
LaunchStatus: "",
ID:'',
timeSelected: ""
};
this.accessKeyNotif = `${this.props.fireDate}-isNotifActive`;
}
componentDidMount = () =>{
this.setState({
set props to state....
});
AsyncStorage.getItem(this.accessKeyNotif).then(value => this.setState({ isNotifActive: JSON.parse(value) }));
}
render() {
PushNotificationIOS.checkPermissions((permissions) => {
if (permissions.alert) {
AsyncStorage.setItem(this.accessKeyNotif, JSON.stringify(true)).then(() => {
this.setState({ isNotifActive: true});
});
}
else{
AsyncStorage.setItem(this.accessKeyNotif, JSON.stringify(false)).then(() => {
this.setState({ isNotifActive: false});
});
}
});
return (
<Ionicons
name={this.state.isBellActive? "md-notifications":"md-notifications-off"}
color={"white"}
size={30}
style={styles.NotifIcon}
onPress={() => {
if(this.state.isNotifActive){
Make a notification
}
else if(!this.state.isNotifActive){
Cancel Notifications
}
}}
/>
);
}
}
Bell 组件被另一个类调用多次(20 次)以显示铃声。
【问题讨论】:
标签: reactjs react-native push-notification permissions