Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permissions.askAsync not working as expected

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

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

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)

Environment below:

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

Sample below:

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
  }
}
like image 402
Nikasv Avatar asked Jan 30 '20 06:01

Nikasv


1 Answers

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

like image 70
Muhammad Numan Avatar answered Nov 15 '22 12:11

Muhammad Numan