因此,在我的导航抽屉中,我有一个开关,据说该开关可以打开和关闭通知。显然,如果用户在第一个弹出窗口上授予对通知的权限,则通知状态将为true,并且将起作用。但是,如果他们最初意外拒绝它或想要关闭通知,则会将他们定向到应用程序设置,以便他们可以手动更改权限。我要这样做,以便当他们回到我的应用程序时,开关将打开。当他们从应用程序设置中恢复时,应调用“ getNotificationStatus”函数,以便更新开关。我该怎么办?
import React, { useState, useEffect } from 'react';
import { View, StyleSheet, Linking, Alert } from 'react-native';
import * as Permissions from 'expo-permissions'
import {Notifications} from 'expo'
import {
Switch
} from 'react-native-paper';
import firebase from 'firebase'
export function DrawerContent(props) {
const [notificationStatus, setNotificationStatus] = useState(false
const user = firebase.auth().currentUser;
useEffect(()=>{
getNotificationStatus()
},[])
const getNotificationStatus = async()=>{
if(user){
const { status } = await Permissions.getAsync(Permissions.NOTIFICATIONS);
if(status==='granted')
{
setNotificationStatus(true)
let token = await Notifications.getExpoPushTokenAsync()
firebase.database().ref('users/'+user.uid+'/push_token')
.set(token)
}
else{
setNotificationStatus(false)
}
}
}
const changeNotificationPermissions = async() =>{
if(notificationStatus==false){
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
if(status==='granted')
{
setNotificationStatus(true)
let token = await Notifications.getExpoPushTokenAsync()
firebase.database().ref('users/'+user.uid+'/push_token')
.set(token)
}
else{
Alert.alert("Notifications", "Allow us to access push notifications.",[
{
text: "Cancel",
style: "cancel"
},
{ text: "OK", onPress: () =>{ Linking.openURL('app-settings:');} }
],
{ cancelable: false })
}
}
else{
Alert.alert("Notifications", "Turn off push notifications?",[
{
text: "Cancel",
style: "cancel"
},
{ text: "OK", onPress: () => {Linking.openURL('app-settings:')} }
],
{ cancelable: false })
}
}
return(
Switch onValueChange={()=>changeNotificationPermissions()} value={notificationStatus} />
);
}