Swish
Swish

Reputation: 359

How to tell when use has answered iOS Notification Permissions request in React-Native

in iOS, in order to allow push notifications, the user must do 1 of the following 2 options.

  1. answer the original prompt which asks permission for notifications. If they don't do this, you can't bring up the permission request again, so you must redirect them to the settings page.

  2. Go to the settings page and manually change the permissions.

Here's my flow now:

Check permissions: if they have them, move to the next screen. if they don't, show an alert stating why notifications are important for our app. If they click ok, it shows the real notification screen, if not it just waits to ask later.

If the user already saw the request for notifications screen, I want to show a dialog that asks them to go the settings to allow notifications.

Is there a way of knowing whether the user said yes or no to the original permission box? Is there a way to know when they've answered it?

The way I'm checking now doesn't wait for the user to click on an option in the original dialogue box, so it doesn't wait to check permissions and find s them the exact same as they were before.

is there any way to check whether they've had the request for permissions already and whether they said yes or no?

Here's the code I'm using: (only relevant imports)

import {PushNotificationIOS} from 'react-native
import PushNotification 'react-native-push-notification'

const requestPermissionsIOS = navigateForward => {
  PushNotification.requestPermissions().then( ({alert}) => {
    if (!alert) {
      settingsAlertIOS(navigateForward)
    } else {
        configurePushNotification()
        scheduleNotifications()
        navigateForward()
      }
    }).catch( err => {
      settingsAlertIOS(navigateForward)
    })
}  

const configurePushNotification = () => {
  PushNotification.configure({
    onRegister: (token) => {
    },
    onNotification: (notification) => {
      onNotification: notification => handleNotification(notification)
    },
    permissions: {
      alert: true,
      badge: true,
      sound: true
    },
    popInitialNotification: true,
    requestPermissions: false,
  });
}


const settingsAlertIOS = (navigateForward) => {
  Alert.alert(
    'Go to settings page?',
    'In order to receive important notifications, please enable them in the settings page \(leaves app\)',
    [
      {text: 'Cancel', onPress: () => {Mixpanel.track('Decline change notifications from settings alert'); scheduleNotifications(); navigateForward()}},
      {text: 'Settings', onPress: () => {configurePushNotification(); scheduleNotifications(); navigateForward(); Linking.openURL('app-settings:')}},
    ]
  )
}

Upvotes: 1

Views: 2888

Answers (2)

Shep Sims
Shep Sims

Reputation: 1560

using the react-native-permissions package you can call checkNotifications() to get the status like so:

import { checkNotifications } from 'react-native-permissions';

let notificationStatus = await checkNotifications();

console.log('notification permissions: ', notificationStatus);

Which will print this object:

notification permissions:  {"settings": {"alert": true, "lockScreen": true, "notificationCenter": true, "providesAppSettings": false, "provisional": false, "sound": true}, "status": "granted"}

To get just the status, which will be from {granted, unavailable, denied, blocked} simply call .status on that returned object like so:

console.log('status:', notificationStatus.status)

which will log

status: granted

or whatever the status is

Upvotes: 1

Purnima Naik
Purnima Naik

Reputation: 449

To listen to the user's decision.

PushNotification.requestPermissions().then((response: any) => {
  if (response && response.alert !== 0) {
    // Allow
    return; 
  }
  // Decline
});

Credit - jose920405

Upvotes: 1

Related Questions