Permissions.askAsync not working as expected

前端 未结 1 602
臣服心动
臣服心动 2021-01-18 19:17

I am having an issue with Permissions.askAsync for notifications.

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

When

1条回答
  •  心在旅途
    2021-01-18 20:06

    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:')
    

    like this

    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;
        }
      };
    

    Note: you should use double permission for The Right Way to Ask Users for Mobile Permissions alt text

    0 讨论(0)
提交回复
热议问题