Permissions.askAsync 未按预期工作 [英] Permissions.askAsync not working as expected
问题描述
我在使用 Permissions.askAsync 时遇到问题.
I am having an issue with Permissions.askAsync for notifications.
const status = await Permissions.askAsync(Permissions.NOTIFICATIONS)
当使用 Permissions.askAsync 通知状态为未确定"时,我希望看到提示对话框告诉用户打开通知以继续.但是,当我使用 Permissions.getAsync 获得通知的状态时,如果它不是授予"的,我会使用 Permissions.askAsync 但什么都没有发生(不显示通知对话框)
When notification status is "undetermined" using Permissions.askAsync I expect to be presented with prompt dialog telling user to turn on notifications in order to continue. But, when I get status of the notifications with Permissions.getAsync, if it's not "granted" I use Permissions.askAsync but nothing is happening(not showing the dialog for notifications)
以下环境:
Target: iOS
Expo CLI 3.8.0 environment info:
System:
OS: macOS 10.14.6
Shell: 5.3 - /bin/zsh
Binaries:
Node: 12.13.1 - /usr/local/bin/node
npm: 6.12.1 - /usr/local/bin/npm
Watchman: 4.7.0 - /usr/local/bin/watchman
IDEs:
Android Studio: 3.4 AI-183.6156.11.34.5692245
Xcode: 11.2.1/11B500 - /usr/bin/xcodebuild
npmPackages:
expo: ^35.0.0 => 35.0.1
react: 16.11.0 => 16.11.0
react-native: https://github.com/expo/react-native/archive/sdk-35.0.0.tar.gz => 0.59.8
react-navigation: ^3.11.0 => 3.12.1
npmGlobalPackages:
expo-cli: 3.8.0
示例如下:
import * as Permissions from "expo-permissions"
componentDidMount(){
this.checkPushNotificationState()
}
checkPushNotificationState = async () => {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
)
if (existingStatus !== "granted") {
const status = await Permissions.askAsync(Permissions.NOTIFICATIONS)
statusNotifications = status.status
}
}
推荐答案
使用应用设置链接打开手动权限设置,因为如果用户拒绝权限,ios 不允许我们从提示中授予权限
use app-setting link to open setting for manual permission because ios do not allow us to give permission from prompt if user reject permission
Linking.openURL('app-settings:')
喜欢这个
import React, { Component } from "react";
import { Text, StyleSheet, View, Linking, Alert } from "react-native";
import * as Permissions from "expo-permissions";
export default class App extends Component {
componentDidMount() {
this.checkPushNotificationState();
}
checkPushNotificationState = async () => {
let { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
if (existingStatus !== "granted") {
const status = await Permissions.askAsync(Permissions.NOTIFICATIONS);
existingStatus = status.status;
}
if (existingStatus !== "granted") {
Alert.alert(
"No Notification Permission",
"please goto setting and on notification permission manual",
[
{ text: "cancel", onPress: () => console.log("cancel") },
{ text: "Allow", onPress: () => Linking.openURL("app-settings:") },
],
{ cancelable: false }
);
return;
}
};
注意:您应该对 向用户请求移动权限的正确方法
这篇关于Permissions.askAsync 未按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!