React Native SDK for OneSignal's push notification, email, SMS, and in-app messaging service
—
Push notification subscription handling including permission status, subscription state, and opt-in/opt-out controls.
Monitor changes to push subscription state including subscription ID, token, and opt-in status.
/**
* Add a callback that fires when the OneSignal subscription state changes.
* @param event - Must be 'change'
* @param listener - Callback function that receives subscription state changes
*/
function addEventListener(event: 'change', listener: (event: PushSubscriptionChangedState) => void): void;
/**
* Clears current subscription observers.
* @param event - Must be 'change'
* @param listener - The callback function to remove
*/
function removeEventListener(event: 'change', listener: (event: PushSubscriptionChangedState) => void): void;Usage Example:
import { OneSignal } from "react-native-onesignal";
// Listen for subscription changes
OneSignal.User.pushSubscription.addEventListener('change', (event) => {
console.log('Subscription changed:', {
previous: {
id: event.previous.id,
token: event.previous.token,
optedIn: event.previous.optedIn
},
current: {
id: event.current.id,
token: event.current.token,
optedIn: event.current.optedIn
}
});
});Retrieve subscription identifiers and tokens for the current device.
/**
* Get the push subscription ID asynchronously.
* @returns Promise resolving to subscription ID or null
*/
function getIdAsync(): Promise<string | null>;
/**
* The readonly push subscription token.
* @returns Promise resolving to subscription token or null
*/
function getTokenAsync(): Promise<string | null>;Usage Example:
// Get current subscription details
const subscriptionId = await OneSignal.User.pushSubscription.getIdAsync();
const subscriptionToken = await OneSignal.User.pushSubscription.getTokenAsync();
console.log('Subscription details:', {
id: subscriptionId,
token: subscriptionToken
});Check and manage the user's opt-in status for push notifications.
/**
* Gets a boolean value indicating whether the current user is opted in to push notifications.
* This returns true when the app has notifications permission and optOut is not called.
* Note: Does not take into account the existence of the subscription ID and push token.
* This boolean may return true but push notifications may still not be received by the user.
* @returns Promise resolving to opt-in status
*/
function getOptedInAsync(): Promise<boolean>;Usage Example:
// Check if user is opted in to push notifications
const isOptedIn = await OneSignal.User.pushSubscription.getOptedInAsync();
console.log('User opted in to push:', isOptedIn);Control the user's push notification subscription status.
/**
* Disable the push notification subscription to OneSignal.
*/
function optOut(): void;
/**
* Enable the push notification subscription to OneSignal.
*/
function optIn(): void;Usage Examples:
// User opts out of push notifications
OneSignal.User.pushSubscription.optOut();
// User opts back in to push notifications
OneSignal.User.pushSubscription.optIn();
// Check status after change
const newStatus = await OneSignal.User.pushSubscription.getOptedInAsync();
console.log('New opt-in status:', newStatus);The following methods are deprecated but still available for backward compatibility:
/**
* @deprecated This method is deprecated. It has been replaced by getIdAsync.
*/
function getPushSubscriptionId(): string;
/**
* @deprecated This method is deprecated. It has been replaced by getTokenAsync.
*/
function getPushSubscriptionToken(): string;
/**
* @deprecated This method is deprecated. It has been replaced by getOptedInAsync.
*/
function getOptedIn(): boolean;Migration Example:
// OLD (deprecated) - synchronous methods
const oldId = OneSignal.User.pushSubscription.getPushSubscriptionId();
const oldToken = OneSignal.User.pushSubscription.getPushSubscriptionToken();
const oldOptedIn = OneSignal.User.pushSubscription.getOptedIn();
// NEW - async methods with better error handling
const newId = await OneSignal.User.pushSubscription.getIdAsync();
const newToken = await OneSignal.User.pushSubscription.getTokenAsync();
const newOptedIn = await OneSignal.User.pushSubscription.getOptedInAsync();interface PushSubscriptionState {
id?: string;
token?: string;
optedIn: boolean;
}
interface PushSubscriptionChangedState {
previous: PushSubscriptionState;
current: PushSubscriptionState;
}Install with Tessl CLI
npx tessl i tessl/npm-react-native-onesignal