CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-native-onesignal

React Native SDK for OneSignal's push notification, email, SMS, and in-app messaging service

Pending
Overview
Eval results
Files

notifications.mddocs/

Notification Handling

Notification permission management, event handling, and notification control including click events and display lifecycle.

Capabilities

Permission Management

Manage notification permissions and check permission status.

/**
 * Whether this app has push notification permission. Returns true if the user has accepted 
 * permissions, or if the app has ephemeral or provisional permission.
 * @returns Promise resolving to permission status
 */
function getPermissionAsync(): Promise<boolean>;

/**
 * Prompt the user for permission to receive push notifications. This will display the native 
 * system prompt to request push notification permission. Use the fallbackToSettings parameter 
 * to prompt to open the settings app if a user has already declined push permissions.
 * @param fallbackToSettings - Whether to fallback to settings if permission was previously declined
 * @returns Promise resolving to whether permission was granted
 */
function requestPermission(fallbackToSettings: boolean): Promise<boolean>;

/**
 * Whether attempting to request notification permission will show a prompt. Returns true if 
 * the device has not been prompted for push notification permission already.
 * @returns Promise resolving to whether permission can be requested
 */
function canRequestPermission(): Promise<boolean>;

Usage Examples:

import { OneSignal } from "react-native-onesignal";

// Check current permission status
const hasPermission = await OneSignal.Notifications.getPermissionAsync();
console.log('Has notification permission:', hasPermission);

// Check if we can request permission (hasn't been asked before)
const canRequest = await OneSignal.Notifications.canRequestPermission();
if (canRequest) {
  // Request permission with fallback to settings
  const granted = await OneSignal.Notifications.requestPermission(true);
  console.log('Permission granted:', granted);
}

iOS-Specific Permission Features

Advanced iOS permission features including provisional authorization and native permission status.

/**
 * Instead of having to prompt the user for permission to send them push notifications, your app 
 * can request provisional authorization. iOS only.
 * For more information: https://documentation.onesignal.com/docs/ios-customizations#provisional-push-notifications
 * @param handler - Callback function receiving authorization result
 */
function registerForProvisionalAuthorization(handler: (response: boolean) => void): void;

/**
 * iOS Only. Returns the enum for the native permission of the device. It will be one of:
 * OSNotificationPermissionNotDetermined,
 * OSNotificationPermissionDenied,
 * OSNotificationPermissionAuthorized,
 * OSNotificationPermissionProvisional - only available in iOS 12,
 * OSNotificationPermissionEphemeral - only available in iOS 14
 * @returns Promise resolving to native permission status
 */
function permissionNative(): Promise<OSNotificationPermission>;

Usage Examples:

import { Platform } from "react-native";

// iOS-specific provisional authorization
if (Platform.OS === 'ios') {
  OneSignal.Notifications.registerForProvisionalAuthorization((granted) => {
    console.log('Provisional authorization:', granted);
  });
  
  // Get detailed iOS permission status
  const nativePermission = await OneSignal.Notifications.permissionNative();
  console.log('Native permission status:', nativePermission);
}

Notification Event Handling

Handle notification click events, display lifecycle, and permission changes.

/**
 * Add listeners for notification click and/or lifecycle events.
 * @param event - The event type to listen for
 * @param listener - Callback function for the event
 */
function addEventListener<K extends NotificationEventName>(
  event: K,
  listener: (event: NotificationEventTypeMap[K]) => void
): void;

/**
 * Remove listeners for notification click and/or lifecycle events.
 * @param event - The event type to stop listening for
 * @param listener - The callback function to remove
 */
function removeEventListener<K extends NotificationEventName>(
  event: K,  
  listener: (event: NotificationEventTypeMap[K]) => void
): void;

Usage Examples:

// Handle notification clicks
OneSignal.Notifications.addEventListener('click', (event) => {
  console.log('Notification clicked:', {
    notification: event.notification,
    actionId: event.result.actionId,
    url: event.result.url
  });
  
  // Navigate or perform action based on click
  if (event.result.url) {
    // Open URL or navigate to screen
  }
});

// Handle notifications that will display in foreground
OneSignal.Notifications.addEventListener('foregroundWillDisplay', (event) => {
  console.log('Notification will display:', event.notification);
  
  // Optionally prevent display
  // event.preventDefault();
});

// Handle permission changes
OneSignal.Notifications.addEventListener('permissionChange', (granted) => {
  console.log('Permission changed:', granted);
  
  if (granted) {
    // Permission was granted - user can now receive notifications
  } else {
    // Permission was denied or revoked
  }
});

Notification Management

Clear and remove notifications from the notification center.

/**
 * Removes all OneSignal notifications.
 */
function clearAll(): void;

/**
 * Android Only. Removes a single OneSignal notification based on its Android notification integer id.
 * @param id - Android notification ID to remove
 */
function removeNotification(id: number): void;

/**
 * Android Only. Removes all OneSignal notifications based on its Android notification group Id.
 * @param id - Notification group ID to cancel
 */
function removeGroupedNotifications(id: string): void;

Usage Examples:

import { Platform } from "react-native";

// Clear all notifications
OneSignal.Notifications.clearAll();

// Android-specific notification removal
if (Platform.OS === 'android') {
  // Remove specific notification by ID
  OneSignal.Notifications.removeNotification(12345);
  
  // Remove all notifications in a group
  OneSignal.Notifications.removeGroupedNotifications("promo-group");
}

Deprecated Methods

/**
 * @deprecated This method is deprecated. It has been replaced by getPermissionAsync.
 */
function hasPermission(): boolean;

Migration Example:

// OLD (deprecated) - synchronous method
const oldHasPermission = OneSignal.Notifications.hasPermission();

// NEW - async method with better error handling  
const newHasPermission = await OneSignal.Notifications.getPermissionAsync();

Types

type NotificationEventName = 'click' | 'foregroundWillDisplay' | 'permissionChange';

type NotificationEventTypeMap = {
  click: NotificationClickEvent;
  foregroundWillDisplay: NotificationWillDisplayEvent;
  permissionChange: boolean;
};

interface NotificationClickEvent {
  result: NotificationClickResult;
  notification: OSNotification;
}

interface NotificationClickResult {
  actionId?: string;
  url?: string;
}

enum OSNotificationPermission {
  NotDetermined = 0,
  Denied,
  Authorized,
  Provisional,
  Ephemeral
}

class OSNotification {
  body: string;
  title?: string;
  notificationId: string;
  // ... additional platform-specific properties
  
  display(): void;
}

class NotificationWillDisplayEvent {
  notification: OSNotification;
  
  preventDefault(): void;
  getNotification(): OSNotification;
}

Install with Tessl CLI

npx tessl i tessl/npm-react-native-onesignal

docs

core-sdk.md

in-app-messages.md

index.md

live-activities.md

location.md

notifications.md

push-subscription.md

session.md

user-management.md

tile.json