react-native-wonderpush
Version:
Mobile push notification delivery with the WonderPush service. The WonderPush ReactNative SDK is compatible with iOS and Android.
332 lines (309 loc) • 10 kB
JavaScript
;
import NativeWonderPush from "./NativeWonderPush.js";
// Global delegate storage
let currentDelegate = null;
// Set up event listeners once during static initialization
NativeWonderPush.onNotificationReceived(data => {
if (currentDelegate?.onNotificationReceived) {
currentDelegate.onNotificationReceived(data.notification);
}
});
NativeWonderPush.onNotificationOpened(data => {
if (currentDelegate?.onNotificationOpened) {
currentDelegate.onNotificationOpened(data.notification, data.buttonIndex);
}
});
NativeWonderPush.onUrlForDeeplink(data => {
if (currentDelegate?.urlForDeeplink) {
try {
currentDelegate.urlForDeeplink(data.url, modifiedUrl => {
NativeWonderPush.urlForDeeplinkCallback(data.callbackId, modifiedUrl);
});
} catch (e) {
console.error('[RNWonderPush] Error in urlForDeeplink delegate', e);
// On error, return the original URL
NativeWonderPush.urlForDeeplinkCallback(data.callbackId, data.url);
}
} else {
// No delegate set, return the original URL
NativeWonderPush.urlForDeeplinkCallback(data.callbackId, data.url);
}
});
export default class WonderPush {
// Initialization
static async setLogging(enable) {
return NativeWonderPush.setLogging(enable);
}
static async initialize(clientId, clientSecret) {
return NativeWonderPush.initialize(clientId, clientSecret);
}
static async initializeAndRememberCredentials(clientId, clientSecret) {
return NativeWonderPush.initializeAndRememberCredentials(clientId, clientSecret);
}
static async getRememberedClientId() {
return NativeWonderPush.getRememberedClientId();
}
static async isInitialized() {
return NativeWonderPush.isInitialized();
}
// Subscribing users
static async subscribeToNotifications(fallbackToSettings = false) {
return NativeWonderPush.subscribeToNotifications(fallbackToSettings);
}
static async unsubscribeFromNotifications() {
return NativeWonderPush.unsubscribeFromNotifications();
}
static async isSubscribedToNotifications() {
return NativeWonderPush.isSubscribedToNotifications();
}
// Segmentation
static async trackEvent(type, attributes = {}) {
return NativeWonderPush.trackEvent(type, attributes);
}
// Tags
static async addTag(...tags) {
let tagArray;
if (tags.length > 0 && Array.isArray(tags[0])) {
tagArray = tags[0];
} else {
tagArray = tags;
}
return NativeWonderPush.addTag(tagArray);
}
static async removeTag(...tags) {
let tagArray;
if (tags.length > 0 && Array.isArray(tags[0])) {
tagArray = tags[0];
} else {
tagArray = tags;
}
return NativeWonderPush.removeTag(tagArray);
}
static async removeAllTags() {
return NativeWonderPush.removeAllTags();
}
static async hasTag(tag) {
return NativeWonderPush.hasTag(tag);
}
static async getTags() {
return NativeWonderPush.getTags();
}
// Properties
static async getPropertyValue(property) {
return NativeWonderPush.getPropertyValue(property);
}
static async getPropertyValues(property) {
return NativeWonderPush.getPropertyValues(property);
}
static async addProperty(property, values) {
const valuesArray = Array.isArray(values) ? values : [values];
return NativeWonderPush.addProperty(property, valuesArray);
}
static async removeProperty(property, values) {
const valuesArray = Array.isArray(values) ? values : [values];
return NativeWonderPush.removeProperty(property, valuesArray);
}
static async setProperty(property, values) {
const valuesArray = Array.isArray(values) ? values : [values];
return NativeWonderPush.setProperty(property, valuesArray);
}
static async unsetProperty(property) {
return NativeWonderPush.unsetProperty(property);
}
static async putProperties(properties) {
return NativeWonderPush.putProperties(properties);
}
static async getProperties() {
return NativeWonderPush.getProperties();
}
// Localization
static async getCountry() {
return NativeWonderPush.getCountry();
}
static async setCountry(country) {
return NativeWonderPush.setCountry(country);
}
static async getCurrency() {
return NativeWonderPush.getCurrency();
}
static async setCurrency(currency) {
return NativeWonderPush.setCurrency(currency);
}
static async getLocale() {
return NativeWonderPush.getLocale();
}
static async setLocale(locale) {
return NativeWonderPush.setLocale(locale);
}
static async getTimeZone() {
return NativeWonderPush.getTimeZone();
}
static async setTimeZone(timeZone) {
return NativeWonderPush.setTimeZone(timeZone);
}
// User IDs
static async getUserId() {
return NativeWonderPush.getUserId();
}
static async setUserId(userId) {
return NativeWonderPush.setUserId(userId);
}
// Installation info
static async getDeviceId() {
return NativeWonderPush.getDeviceId();
}
static async getInstallationId() {
return NativeWonderPush.getInstallationId();
}
static async getPushToken() {
return NativeWonderPush.getPushToken();
}
static async getAccessToken() {
return NativeWonderPush.getAccessToken();
}
// Privacy
static async setRequiresUserConsent(value) {
return NativeWonderPush.setRequiresUserConsent(value);
}
static async getUserConsent() {
return NativeWonderPush.getUserConsent();
}
static async setUserConsent(value) {
return NativeWonderPush.setUserConsent(value);
}
static async disableGeolocation() {
return NativeWonderPush.disableGeolocation();
}
static async enableGeolocation() {
return NativeWonderPush.enableGeolocation();
}
static async setGeolocation(lat, lon) {
return NativeWonderPush.setGeolocation(lat, lon);
}
static async clearEventsHistory() {
return NativeWonderPush.clearEventsHistory();
}
static async clearPreferences() {
return NativeWonderPush.clearPreferences();
}
static async clearAllData() {
return NativeWonderPush.clearAllData();
}
static async downloadAllData() {
return NativeWonderPush.downloadAllData();
}
// Deep linking
static async getInitialURL() {
return NativeWonderPush.getInitialURL();
}
// Delegate (event-based handling)
static setDelegate(delegate) {
// Simply store the current delegate - event listeners are already set up
currentDelegate = delegate;
// Flush any queued notifications from native side
NativeWonderPush.flushDelegateEvents();
}
}
/**
* WonderPushUserPreferences provides access to notification channel preferences.
* On Android, this controls notification channels and channel groups.
* On iOS, these methods are no-ops (except getters which return default values).
*/
export class WonderPushUserPreferences {
/**
* Get the default channel ID.
* Returns "default" on iOS, the actual default channel ID on Android.
*/
static async getDefaultChannelId() {
return NativeWonderPush.getDefaultChannelId();
}
/**
* Set the default channel ID.
* No-op on iOS, sets the default channel on Android.
*/
static async setDefaultChannelId(id) {
return NativeWonderPush.setDefaultChannelId(id);
}
/**
* Get a channel group by ID.
* Returns null on iOS, the actual channel group on Android if it exists.
*/
static async getChannelGroup(groupId) {
const result = await NativeWonderPush.getChannelGroup(groupId);
return result;
}
/**
* Get a channel by ID.
* Returns null on iOS, the actual channel on Android if it exists.
*/
static async getChannel(channelId) {
const result = await NativeWonderPush.getChannel(channelId);
return result;
}
/**
* Set all channel groups, replacing existing ones.
* No-op on iOS, replaces all channel groups on Android.
*/
static async setChannelGroups(channelGroups) {
// Validate that all channel groups have an id
channelGroups.forEach((group, index) => {
if (!group.id || typeof group.id !== 'string') {
throw new Error(`Channel group at index ${index} must have a valid 'id' string property`);
}
});
return NativeWonderPush.setChannelGroups(channelGroups);
}
/**
* Set all channels, replacing existing ones.
* No-op on iOS, replaces all channels on Android.
*/
static async setChannels(channels) {
// Validate that all channels have an id
channels.forEach((channel, index) => {
if (!channel.id || typeof channel.id !== 'string') {
throw new Error(`Channel at index ${index} must have a valid 'id' string property`);
}
});
return NativeWonderPush.setChannels(channels);
}
/**
* Add or update a channel group.
* No-op on iOS, adds or updates the channel group on Android.
*/
static async putChannelGroup(channelGroup) {
// Validate that the channel group has an id
if (!channelGroup.id || typeof channelGroup.id !== 'string') {
throw new Error("Channel group must have a valid 'id' string property");
}
return NativeWonderPush.putChannelGroup(channelGroup);
}
/**
* Add or update a channel.
* No-op on iOS, adds or updates the channel on Android.
*/
static async putChannel(channel) {
// Validate that the channel has an id
if (!channel.id || typeof channel.id !== 'string') {
throw new Error("Channel must have a valid 'id' string property");
}
return NativeWonderPush.putChannel(channel);
}
/**
* Remove a channel group by ID.
* No-op on iOS, removes the channel group on Android.
*/
static async removeChannelGroup(groupId) {
return NativeWonderPush.removeChannelGroup(groupId);
}
/**
* Remove a channel by ID.
* No-op on iOS, removes the channel on Android.
*/
static async removeChannel(channelId) {
return NativeWonderPush.removeChannel(channelId);
}
}
// Named exports for both classes
export { WonderPush };
// Export types
//# sourceMappingURL=index.js.map