gpt4 book ai didi

react-native - Permissions.askAsync 未按预期工作

转载 作者:行者123 更新时间:2023-12-03 14:23:33 27 4
gpt4 key购买 nike

我遇到了 Permissions.askAsync 通知问题。

const status = await Permissions.askAsync(Permissions.NOTIFICATIONS)

当使用 Permissions.askAsync 通知状态为“未确定”时,我希望看到提示对话框,告诉用户打开通知以继续。但是,当我使用 Permissions.getAsync 获得通知的状态时,如果它没有被“授予”我使用 Permissions.askAsync 但什么也没有发生(不显示通知对话框)

环境如下:
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
}
}

最佳答案

使用 app-setting 链接打开手动权限设置,因为如果用户拒绝权限,ios 不允许我们从提示中授予权限

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;
}
};
注:您应该对 The Right Way to Ask Users for Mobile Permissions 使用双重权限
alt text

关于react-native - Permissions.askAsync 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59980039/

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