nani10
nani10

Reputation: 321

Get the status of the notification permission and programmatically turn on the notification by the settings screen of the app

So I have a settings screen that has a switch on it that can toggle if the notification is ON/OFF. I can access the status of the notification but what I want to do is if I toggle the switch from OFF -> ON I want it to programmatically turn ON the notification settings by not opening the settings of the phone. Im using expo for this one. Should I use askAsync?? if I use this, the notification permission will popup I dont want this one to be displayed.

Here is where I can get the status of the NOTIFICATION

const { status, permissions } = await Permissions.getAsync(
        Permissions.NOTIFICATIONS
      ); ```

Upvotes: 1

Views: 3771

Answers (1)

Rajshekhar
Rajshekhar

Reputation: 614

You can ask permission by toggleing in only one case if the user has clicked on the no or deny option if he has selected don't ask again then it is not possible(in ios only). If he has disabled you have to go setting page.

 async function requestPermissionForNotification() {
try {
  const permission = await requestNotifications(["alert", "badge", "sound"]);
  if (permission.status === "granted") {
    setupNotification();
  }
} catch (e) {
  console.log(e)
}

To achieve this you can use the various libraries.

Link import {Linking} from 'react-native';

Linking.openSettings();

You can also use react-native-permission. Link [https://www.npmjs.com/package/react-native-permissions]

Upvotes: 1

Related Questions