React Native SDK for OneSignal's push notification, email, SMS, and in-app messaging service
—
Comprehensive user identification and data management including aliases, tags, email, SMS subscriptions, and state tracking.
Monitor changes to user state including OneSignal ID and external ID changes.
/**
* Add a callback that fires when the OneSignal user state changes.
* Important: When using the observer to retrieve the onesignalId, check the externalId
* as well to confirm the values are associated with the expected user.
* @param event - Must be 'change'
* @param listener - Callback function that receives user state changes
*/
function addEventListener(event: 'change', listener: (event: UserChangedState) => void): void;
/**
* Clears current user state observers.
* @param event - Must be 'change'
* @param listener - The callback function to remove
*/
function removeEventListener(event: 'change', listener: (event: UserChangedState) => void): void;Usage Example:
import { OneSignal } from "react-native-onesignal";
// Listen for user state changes
OneSignal.User.addEventListener('change', (event) => {
console.log('User state changed:', {
onesignalId: event.current.onesignalId,
externalId: event.current.externalId
});
});Retrieve user identifiers for the current user session.
/**
* Get the nullable OneSignal Id associated with the user.
* @returns Promise resolving to OneSignal user ID or null
*/
function getOnesignalId(): Promise<string | null>;
/**
* Get the nullable External Id associated with the user.
* @returns Promise resolving to external user ID or null
*/
function getExternalId(): Promise<string | null>;Usage Example:
// Get current user IDs
const onesignalId = await OneSignal.User.getOnesignalId();
const externalId = await OneSignal.User.getExternalId();
console.log('User IDs:', { onesignalId, externalId });Set the user's language for localized messaging.
/**
* Explicitly set a 2-character language code for the user.
* @param language - ISO 639-1 two-character language code
*/
function setLanguage(language: string): void;Usage Example:
// Set user language for localized messages
OneSignal.User.setLanguage("es"); // Spanish
OneSignal.User.setLanguage("fr"); // FrenchManage user aliases for cross-platform identification and custom user mapping.
/**
* Set an alias for the current user. If this alias label already exists on this user,
* it will be overwritten with the new alias id.
* @param label - The alias label/key
* @param id - The alias value/ID
*/
function addAlias(label: string, id: string): void;
/**
* Set aliases for the current user. If any alias already exists, it will be overwritten
* to the new values.
* @param aliases - Object containing label-id pairs
*/
function addAliases(aliases: object): void;
/**
* Remove an alias from the current user.
* @param label - The alias label to remove
*/
function removeAlias(label: string): void;
/**
* Remove aliases from the current user.
* @param labels - Array of alias labels to remove
*/
function removeAliases(labels: string[]): void;Usage Examples:
// Add single alias
OneSignal.User.addAlias("customer_id", "12345");
// Add multiple aliases
OneSignal.User.addAliases({
"customer_id": "12345",
"loyalty_tier": "gold",
"region": "us-west"
});
// Remove aliases
OneSignal.User.removeAlias("region");
OneSignal.User.removeAliases(["customer_id", "loyalty_tier"]);Manage email subscriptions for multi-channel messaging.
/**
* Add a new email subscription to the current user.
* @param email - Email address to add
*/
function addEmail(email: string): void;
/**
* Remove an email subscription from the current user. Returns false if the specified
* email does not exist on the user within the SDK, and no request will be made.
* @param email - Email address to remove
*/
function removeEmail(email: string): void;Usage Example:
// Add email for multi-channel messaging
OneSignal.User.addEmail("user@example.com");
// Remove email subscription
OneSignal.User.removeEmail("user@example.com");Manage SMS subscriptions for text message marketing and notifications.
/**
* Add a new SMS subscription to the current user.
* @param smsNumber - Phone number to add
*/
function addSms(smsNumber: string): void;
/**
* Remove an SMS subscription from the current user. Returns false if the specified SMS
* number does not exist on the user within the SDK, and no request will be made.
* @param smsNumber - Phone number to remove
*/
function removeSms(smsNumber: string): void;Usage Example:
// Add SMS number for text messaging
OneSignal.User.addSms("+1234567890");
// Remove SMS subscription
OneSignal.User.removeSms("+1234567890");Manage user tags for segmentation, targeting, and personalization.
/**
* Add a tag for the current user. Tags are key:value pairs used as building blocks for
* targeting specific users and/or personalizing messages. If the tag key already exists,
* it will be replaced with the value provided here.
* @param key - Tag key
* @param value - Tag value (will be converted to string if not already)
*/
function addTag(key: string, value: string): void;
/**
* Add multiple tags for the current user. Tags are key:value pairs used as building blocks
* for targeting specific users and/or personalizing messages. If the tag key already exists,
* it will be replaced with the value provided here.
* @param tags - Object containing key-value pairs
*/
function addTags(tags: object): void;
/**
* Remove the data tag with the provided key from the current user.
* @param key - Tag key to remove
*/
function removeTag(key: string): void;
/**
* Remove multiple tags with the provided keys from the current user.
* @param keys - Array of tag keys to remove
*/
function removeTags(keys: string[]): void;
/**
* Returns the local tags for the current user.
* @returns Promise resolving to object containing current user tags
*/
function getTags(): Promise<{ [key: string]: string }>;Usage Examples:
// Add single tag
OneSignal.User.addTag("subscription", "premium");
// Add multiple tags for segmentation
OneSignal.User.addTags({
"subscription": "premium",
"interests": "sports,technology",
"age_group": "25-34",
"location": "california"
});
// Get all current tags
const userTags = await OneSignal.User.getTags();
console.log("User tags:", userTags);
// Remove specific tags
OneSignal.User.removeTag("age_group");
OneSignal.User.removeTags(["interests", "location"]);Install with Tessl CLI
npx tessl i tessl/npm-react-native-onesignal