Permissions.askAsync无法正常工作

时间:2020-01-30 06:11:10

标签: react-native push-notification permissions expo

我在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
  }
}

1 个答案:

答案 0 :(得分:3)

使用应用设置链接打开手动权限设置,因为如果用户拒绝权限,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