Unified permissions API for React Native on iOS, Android and Windows platforms
—
Specialized handling for notification permissions with granular control over notification types and settings. Provides detailed information about what notification capabilities are available and enabled.
Get the current notification permission status along with detailed settings for each notification type.
/**
* Check notification permissions and get detailed settings
* @returns Promise resolving to notification status and settings object
*/
function checkNotifications(): Promise<NotificationsResponse>;
interface NotificationsResponse {
status: PermissionStatus;
settings: NotificationSettings;
}
interface NotificationSettings {
alert?: boolean;
badge?: boolean;
sound?: boolean;
carPlay?: boolean;
criticalAlert?: boolean;
provisional?: boolean;
providesAppSettings?: boolean;
lockScreen?: boolean;
notificationCenter?: boolean;
}Usage Examples:
import { checkNotifications, RESULTS } from "react-native-permissions";
const notificationResult = await checkNotifications();
console.log("Overall status:", notificationResult.status);
if (notificationResult.status === RESULTS.GRANTED) {
const { settings } = notificationResult;
// Check basic notification types
console.log("Can show alerts:", settings.alert);
console.log("Can show badges:", settings.badge);
console.log("Can play sounds:", settings.sound);
// Check advanced capabilities (iOS)
console.log("CarPlay support:", settings.carPlay);
console.log("Critical alerts:", settings.criticalAlert);
console.log("Provisional notifications:", settings.provisional);
// Check display locations
console.log("Lock screen display:", settings.lockScreen);
console.log("Notification center:", settings.notificationCenter);
}
// Determine what notifications are effectively enabled
function getEnabledNotificationTypes(settings: NotificationSettings): string[] {
const enabled = [];
if (settings.alert) enabled.push('alert');
if (settings.badge) enabled.push('badge');
if (settings.sound) enabled.push('sound');
return enabled;
}
const enabledTypes = getEnabledNotificationTypes(notificationResult.settings);
console.log("Enabled notification types:", enabledTypes);Request notification permissions with specific notification types and optional rationale.
/**
* Request notification permissions with specific options
* @param options - Array of notification types to request (optional)
* @param rationale - Optional rationale dialog for Android
* @returns Promise resolving to notification status and settings
*/
function requestNotifications(
options?: NotificationOption[],
rationale?: Rationale
): Promise<NotificationsResponse>;
type NotificationOption =
| 'alert' // Show alert notifications
| 'badge' // Show badge on app icon
| 'sound' // Play notification sounds
| 'carPlay' // Show notifications in CarPlay (iOS)
| 'criticalAlert' // Critical alerts that bypass silent mode (iOS)
| 'provisional' // Quiet notifications delivered directly to notification center (iOS)
| 'providesAppSettings'; // Indicates app has custom notification settings (iOS)Usage Examples:
import { requestNotifications, RESULTS } from "react-native-permissions";
// Request basic notification permissions (default)
const basicRequest = await requestNotifications();
console.log("Basic notification status:", basicRequest.status);
// Request specific notification types (iOS)
const specificRequest = await requestNotifications([
'alert',
'badge',
'sound'
]);
if (specificRequest.status === RESULTS.GRANTED) {
console.log("Requested notifications granted");
console.log("Settings:", specificRequest.settings);
}
// Request advanced notification types (iOS)
const advancedRequest = await requestNotifications([
'alert',
'badge',
'sound',
'criticalAlert',
'provisional',
'carPlay'
]);
// Request with rationale (Android)
const androidRequest = await requestNotifications(
['alert', 'badge', 'sound'],
{
title: "Enable Notifications",
message: "Stay updated with important messages and app updates. You can customize notification types in settings.",
buttonPositive: "Enable Notifications",
buttonNegative: "Skip"
}
);
// Check for critical alert capability
if (advancedRequest.settings.criticalAlert) {
console.log("Critical alerts are available - important notifications will bypass Do Not Disturb");
}
// Check for provisional notifications
if (advancedRequest.settings.provisional) {
console.log("Provisional notifications enabled - quiet notifications delivered to notification center");
}These are the core notification types available on both platforms:
// Basic types supported on iOS and Android
const basicTypes: NotificationOption[] = ['alert', 'badge', 'sound'];
const basicNotifications = await requestNotifications(basicTypes);Advanced notification types available only on iOS:
// iOS-specific advanced types
const iosAdvancedTypes: NotificationOption[] = [
'carPlay', // Show in CarPlay interface
'criticalAlert', // Bypass silent mode and Do Not Disturb
'provisional', // Quiet delivery to notification center
'providesAppSettings' // App has custom notification settings
];
const iosNotifications = await requestNotifications([
...basicTypes,
...iosAdvancedTypes
]);The NotificationSettings object provides detailed information about what's enabled:
interface NotificationSettings {
// Requested notification types
alert?: boolean;
badge?: boolean;
sound?: boolean;
carPlay?: boolean;
criticalAlert?: boolean;
provisional?: boolean;
providesAppSettings?: boolean;
// System-determined display locations
lockScreen?: boolean; // Can appear on lock screen
notificationCenter?: boolean; // Can appear in notification center
}
// Example usage
const result = await checkNotifications();
const { settings } = result;
// Check if notifications will be visible to user
const isVisible = settings.alert || settings.lockScreen || settings.notificationCenter;
// Check if notifications will make sound/vibration
const isAudible = settings.sound;
// Check if app icon will show badge
const showsBadge = settings.badge;
console.log(`Notifications visible: ${isVisible}, audible: ${isAudible}, shows badge: ${showsBadge}`);// iOS allows granular control over notification types
const iosResult = await requestNotifications([
'alert',
'badge',
'sound',
'provisional' // iOS-specific: quiet delivery
]);
// iOS users can partially grant - some types enabled, others disabled
if (iosResult.status === RESULTS.GRANTED) {
if (iosResult.settings.provisional && !iosResult.settings.alert) {
console.log("User granted quiet notifications only");
}
}
// Critical alerts require special entitlement
const criticalResult = await requestNotifications(['criticalAlert']);
if (criticalResult.settings.criticalAlert) {
console.log("Critical alerts enabled - will bypass Do Not Disturb");
}// Android treats notifications more as all-or-nothing
const androidResult = await requestNotifications(
['alert', 'badge', 'sound'],
{
title: "Stay Connected",
message: "Enable notifications to receive important updates and messages from your contacts.",
buttonPositive: "Allow",
buttonNegative: "Not now"
}
);
// Android notification channels provide additional control
// (handled by the native implementation)Request basic notifications first, then advanced features:
import { requestNotifications, RESULTS } from "react-native-permissions";
async function setupNotifications() {
// Start with basic notifications
let result = await requestNotifications(['alert', 'badge', 'sound']);
if (result.status === RESULTS.GRANTED) {
console.log("Basic notifications enabled");
// Request advanced features if basic ones are granted
const advancedResult = await requestNotifications([
'alert', 'badge', 'sound', // Include previously granted
'provisional', // Add new advanced feature
'criticalAlert'
]);
if (advancedResult.settings.provisional) {
console.log("Provisional notifications available");
}
return advancedResult;
} else {
console.log("Basic notifications denied");
return result;
}
}Analyze notification capabilities for app features:
import { checkNotifications } from "react-native-permissions";
async function analyzeNotificationCapabilities() {
const result = await checkNotifications();
const capabilities = {
canShowAlerts: result.settings.alert === true,
canShowBadges: result.settings.badge === true,
canPlaySounds: result.settings.sound === true,
hasQuietDelivery: result.settings.provisional === true,
hasCriticalAlerts: result.settings.criticalAlert === true,
appearsOnLockScreen: result.settings.lockScreen === true,
appearsInNotificationCenter: result.settings.notificationCenter === true
};
console.log("Notification capabilities:", capabilities);
// Adapt app behavior based on capabilities
if (!capabilities.canShowAlerts && capabilities.appearsInNotificationCenter) {
console.log("Notifications delivered quietly to notification center");
}
if (capabilities.hasCriticalAlerts) {
console.log("Can send emergency notifications that bypass Do Not Disturb");
}
return capabilities;
}import { checkNotifications, requestNotifications, openSettings, RESULTS } from "react-native-permissions";
async function ensureNotificationsEnabled(): Promise<boolean> {
let result = await checkNotifications();
if (result.status === RESULTS.GRANTED) {
console.log("Notifications already enabled");
return true;
}
if (result.status === RESULTS.DENIED) {
// Try to request permission
result = await requestNotifications(['alert', 'badge', 'sound']);
if (result.status === RESULTS.GRANTED) {
console.log("Notifications enabled by user");
return true;
}
}
if (result.status === RESULTS.BLOCKED) {
// Direct user to settings
console.log("Notifications blocked - directing to settings");
await openSettings('notifications');
return false;
}
console.log("Notifications not available or denied");
return false;
}import { requestNotifications } from "react-native-permissions";
async function safeRequestNotifications(options?: NotificationOption[]) {
try {
const result = await requestNotifications(options);
return { success: true, result };
} catch (error) {
console.error("Failed to request notifications:", error);
// Provide fallback behavior
return {
success: false,
error,
result: {
status: 'unavailable' as const,
settings: {}
}
};
}
}
// Usage
const response = await safeRequestNotifications(['alert', 'badge']);
if (response.success) {
console.log("Notification request completed:", response.result);
} else {
console.log("Notification request failed, continuing without notifications");
}Install with Tessl CLI
npx tessl i tessl/npm-react-native-permissions