react-native-onesignal
Version:
React Native OneSignal SDK
533 lines (496 loc) • 24.4 kB
TypeScript
declare interface AndroidNotificationData extends BaseNotificationData {
groupKey?: string;
groupMessage?: string;
ledColor?: string;
priority?: number;
smallIcon?: string;
largeIcon?: string;
bigPicture?: string;
collapseId?: string;
fromProjectNumber?: string;
smallIconAccentColor?: string;
lockScreenVisibility?: string;
androidNotificationId?: number;
}
declare interface BaseNotificationData {
body: string;
sound?: string;
title?: string;
launchURL?: string;
rawPayload: object | string;
actionButtons?: object[];
additionalData?: object;
notificationId: string;
}
declare interface EventListenerMap {
[]: (event: boolean) => void;
[]: (event: PushSubscriptionChangedState) => void;
[]: (event: UserChangedState) => void;
[]: (event: NotificationWillDisplayEvent) => void;
[]: (event: NotificationClickEvent) => void;
[]: (event: InAppMessageClickEvent) => void;
[]: (event: InAppMessageWillDisplayEvent) => void;
[]: (event: InAppMessageWillDismissEvent) => void;
[]: (event: InAppMessageDidDismissEvent) => void;
[]: (event: InAppMessageDidDisplayEvent) => void;
}
declare const IN_APP_MESSAGE_CLICKED = "OneSignal-inAppMessageClicked";
declare const IN_APP_MESSAGE_DID_DISMISS = "OneSignal-inAppMessageDidDismiss";
declare const IN_APP_MESSAGE_DID_DISPLAY = "OneSignal-inAppMessageDidDisplay";
declare const IN_APP_MESSAGE_WILL_DISMISS = "OneSignal-inAppMessageWillDismiss";
declare const IN_APP_MESSAGE_WILL_DISPLAY = "OneSignal-inAppMessageWillDisplay";
export declare interface InAppMessage {
messageId: string;
}
export declare interface InAppMessageClickEvent {
message: InAppMessage;
result: InAppMessageClickResult;
}
export declare interface InAppMessageClickResult {
closingMessage: boolean;
actionId?: string;
url?: string;
urlTarget?: string;
}
export declare interface InAppMessageDidDismissEvent {
message: InAppMessage;
}
export declare interface InAppMessageDidDisplayEvent {
message: InAppMessage;
}
declare type InAppMessageListeners = ['click', EventListenerMap['OneSignal-inAppMessageClicked']] | ['willDisplay', EventListenerMap['OneSignal-inAppMessageWillDisplay']] | ['didDisplay', EventListenerMap['OneSignal-inAppMessageDidDisplay']] | ['willDismiss', EventListenerMap['OneSignal-inAppMessageWillDismiss']] | ['didDismiss', EventListenerMap['OneSignal-inAppMessageDidDismiss']];
export declare interface InAppMessageWillDismissEvent {
message: InAppMessage;
}
export declare interface InAppMessageWillDisplayEvent {
message: InAppMessage;
}
declare interface iOSNotificationData extends BaseNotificationData {
badge?: string;
badgeIncrement?: string;
category?: string;
threadId?: string;
subtitle?: string;
templateId?: string;
templateName?: string;
attachments?: object;
mutableContent?: boolean;
contentAvailable?: string;
relevanceScore?: number;
interruptionLevel?: string;
}
/**
* The setup options for `OneSignal.LiveActivities.setupDefault`.
*/
declare type LiveActivitySetupOptions = {
/**
* When true, OneSignal will listen for pushToStart tokens for the `OneSignalLiveActivityAttributes` structure.
*/
enablePushToStart: boolean;
/**
* When true, OneSignal will listen for pushToUpdate tokens for each start live activity that uses the
* `OneSignalLiveActivityAttributes` structure.
*/
enablePushToUpdate: boolean;
};
export declare enum LogLevel {
None = 0,
Fatal = 1,
Error = 2,
Warn = 3,
Info = 4,
Debug = 5,
Verbose = 6
}
declare const NOTIFICATION_CLICKED = "OneSignal-notificationClicked";
declare const NOTIFICATION_WILL_DISPLAY = "OneSignal-notificationWillDisplayInForeground";
export declare interface NotificationClickEvent {
result: NotificationClickResult;
notification: OSNotification;
}
export declare interface NotificationClickResult {
actionId?: string;
url?: string;
}
declare type NotificationListeners = ['click', EventListenerMap['OneSignal-notificationClicked']] | [
'foregroundWillDisplay',
EventListenerMap['OneSignal-notificationWillDisplayInForeground']
] | ['permissionChange', EventListenerMap['OneSignal-permissionChanged']];
export declare class NotificationWillDisplayEvent {
notification: OSNotification;
constructor(displayEvent: OSNotification);
preventDefault(): void;
getNotification(): OSNotification;
}
export declare namespace OneSignal {
/** Initializes the OneSignal SDK. This should be called during startup of the application. */
export function initialize(appId: string): void;
/**
* If your integration is user-centric, or you want the ability to identify the user beyond the current device, the
* login method should be called to identify the user.
*/
export function login(externalId: string): void;
/**
* Once (or if) the user is no longer identifiable in your app (i.e. they logged out), the logout method should be
* called.
*/
export function logout(): void;
/** For GDPR users, your application should call this method before setting the App ID. */
export function setConsentRequired(required: boolean): void;
/**
* If your application is set to require the user's privacy consent, you can provide this consent using this method.
* Indicates whether privacy consent has been granted. This field is only relevant when the application has opted
* into data privacy protections.
*/
export function setConsentGiven(granted: boolean): void;
export namespace Debug {
/**
* Enable logging to help debug if you run into an issue setting up OneSignal.
* @param {LogLevel} nsLogLevel - Sets the logging level to print to the Android LogCat log or Xcode log.
*/
export function setLogLevel(nsLogLevel: LogLevel): void;
/**
* Enable logging to help debug if you run into an issue setting up OneSignal.
* @param {LogLevel} visualLogLevel - Sets the logging level to show as alert dialogs.
*/
export function setAlertLevel(visualLogLevel: LogLevel): void;
}
export namespace LiveActivities {
/**
* Indicate this device has entered a live activity, identified within OneSignal by the `activityId`.
*
* Only applies to iOS
*
* @param activityId: The activity identifier the live activity on this device will receive updates for.
* @param token: The activity's update token to receive the updates.
**/
export function enter(activityId: string, token: string, handler?: Function): void;
/**
* Indicate this device has exited a live activity, identified within OneSignal by the `activityId`.
*
* Only applies to iOS
*
* @param activityId: The activity identifier the live activity on this device will no longer receive updates for.
**/
export function exit(activityId: string, handler?: Function): void;
/**
* Indicate this device is capable of receiving pushToStart live activities for the
* `activityType`. The `activityType` **must** be the name of the struct conforming
* to `ActivityAttributes` that will be used to start the live activity.
*
* Only applies to iOS
*
* @param activityType: The name of the specific `ActivityAttributes` structure tied
* to the live activity.
* @param token: The activity type's pushToStart token.
*/
export function setPushToStartToken(activityType: string, token: string): void;
/**
* Indicate this device is no longer capable of receiving pushToStart live activities
* for the `activityType`. The `activityType` **must** be the name of the struct conforming
* to `ActivityAttributes` that will be used to start the live activity.
*
* Only applies to iOS
*
* @param activityType: The name of the specific `ActivityAttributes` structure tied
* to the live activity.
*/
export function removePushToStartToken(activityType: string): void;
/**
* Enable the OneSignalSDK to setup the default`DefaultLiveActivityAttributes` structure,
* which conforms to the `OneSignalLiveActivityAttributes`. When using this function, the
* widget attributes are owned by the OneSignal SDK, which will allow the SDK to handle the
* entire lifecycle of the live activity. All that is needed from an app-perspective is to
* create a Live Activity widget in a widget extension, with a `ActivityConfiguration` for
* `DefaultLiveActivityAttributes`. This is most useful for users that (1) only have one Live
* Activity widget and (2) are using a cross-platform framework and do not want to create the
* cross-platform <-> iOS native bindings to manage ActivityKit.
*
* Only applies to iOS
*
* @param options: An optional structure to provide for more granular setup options.
*/
export function setupDefault(options?: LiveActivitySetupOptions): void;
/**
* Start a new LiveActivity that is modelled by the default`DefaultLiveActivityAttributes`
* structure. The `DefaultLiveActivityAttributes` is initialized with the dynamic `attributes`
* and `content` passed in.
*
* Only applies to iOS
*
* @param activityId: The activity identifier the live activity on this device will be started
* and eligible to receive updates for.
* @param attributes: A dynamic type containing the static attributes passed into `DefaultLiveActivityAttributes`.
* @param content: A dynamic type containing the content attributes passed into `DefaultLiveActivityAttributes`.
*/
export function startDefault(activityId: string, attributes: object, content: object): void;
}
export namespace User {
export namespace pushSubscription {
/** Add a callback that fires when the OneSignal subscription state changes. */
export function addEventListener(_event: 'change', listener: (event: PushSubscriptionChangedState) => void): void;
/** Clears current subscription observers. */
export function removeEventListener(_event: 'change', listener: (event: PushSubscriptionChangedState) => void): void;
/**
* @deprecated This method is deprecated. It has been replaced by {@link getIdAsync}.
*/
export function getPushSubscriptionId(): string;
export function getIdAsync(): Promise<string | null>;
/**
* @deprecated This method is deprecated. It has been replaced by {@link getTokenAsync}.
*/
export function getPushSubscriptionToken(): string;
/** The readonly push subscription token */
export function getTokenAsync(): Promise<string | null>;
/**
* @deprecated This method is deprecated. It has been replaced by {@link getOptedInAsync}.
*/
export function getOptedIn(): boolean;
/**
* 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.
*/
export function getOptedInAsync(): Promise<boolean>;
/** Disable the push notification subscription to OneSignal. */
export function optOut(): void;
/** Enable the push notification subscription to OneSignal. */
export function optIn(): void;
}
/**
* 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.
*/
export function addEventListener(_event: 'change', listener: (event: UserChangedState) => void): void;
/** Clears current user state observers. */
export function removeEventListener(_event: 'change', listener: (event: UserChangedState) => void): void;
/** Get the nullable OneSignal Id associated with the user. */
export function getOnesignalId(): Promise<string | null>;
/** Get the nullable External Id associated with the user. */
export function getExternalId(): Promise<string | null>;
/** Explicitly set a 2-character language code for the user. */
export function setLanguage(language: string): void;
/** 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. */
export 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. */
export function addAliases(aliases: object): void;
/** Remove an alias from the current user. */
export function removeAlias(label: string): void;
/** Remove aliases from the current user. */
export function removeAliases(labels: string[]): void;
/** Add a new email subscription to the current user. */
export 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.
*/
export function removeEmail(email: string): void;
/** Add a new SMS subscription to the current user. */
export 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.
*/
export function removeSms(smsNumber: string): void;
/**
* 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.
*/
export 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.
*/
export function addTags(tags: object): void;
/** Remove the data tag with the provided key from the current user. */
export function removeTag(key: string): void;
/** Remove multiple tags with the provided keys from the current user. */
export function removeTags(keys: string[]): void;
/** Returns the local tags for the current user. */
export function getTags(): Promise<{
[]: string;
}>;
}
export namespace Notifications {
/**
* @deprecated This method is deprecated. It has been replaced by {@link getPermissionAsync}.
*/
export function hasPermission(): boolean;
/**
* Whether this app has push notification permission. Returns true if the user has accepted permissions,
* or if the app has ephemeral or provisional permission.
*/
export 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.
*/
export 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.
*/
export function canRequestPermission(): Promise<boolean>;
/**
* Instead of having to prompt the user for permission to send them push notifications, your app can request provisional authorization.
* For more information: https://documentation.onesignal.com/docs/ios-customizations#provisional-push-notifications
* @param {(response:{accepted:boolean})=>void} handler
*/
export 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
* */
export function permissionNative(): Promise<OSNotificationPermission>;
/**
* Add listeners for notification click and/or lifecycle events. */
export function addEventListener(...[event, listener]: NotificationListeners): void;
/**
* Remove listeners for notification click and/or lifecycle events. */
export function removeEventListener(...[event, listener]: NotificationListeners): void;
/**
* Removes all OneSignal notifications.
*/
export function clearAll(): void;
/**
* Android Only.
* Removes a single OneSignal notification based on its Android notification integer id.
*/
export function removeNotification(id: number): void;
/**
* Android Only.
* Removes all OneSignal notifications based on its Android notification group Id.
* @param {string} id - notification group id to cancel
*/
export function removeGroupedNotifications(id: string): void;
}
export namespace InAppMessages {
/**
* Add listeners for In-App Message click and/or lifecycle events.
*/
export function addEventListener(...[event, listener]: InAppMessageListeners): void;
/**
* Remove listeners for In-App Message click and/or lifecycle events.
*/
export function removeEventListener(...[event, listener]: InAppMessageListeners): void;
/**
* Add a trigger for the current user. Triggers are currently explicitly used to determine whether a specific IAM should be
* displayed to the user.
*/
export function addTrigger(key: string, value: string): void;
/**
* Add multiple triggers for the current user. Triggers are currently explicitly used to determine whether a specific IAM should
* be displayed to the user.
*/
export function addTriggers(triggers: {
[key: string]: string;
}): void;
/** Remove the trigger with the provided key from the current user. */
export function removeTrigger(key: string): void;
/** Remove multiple triggers from the current user. */
export function removeTriggers(keys: string[]): void;
/** Clear all triggers from the current user. */
export function clearTriggers(): void;
/**
* Set whether in-app messaging is currently paused.
* When set to true no IAM will be presented to the user regardless of whether they qualify for them.
* When set to 'false` any IAMs the user qualifies for will be presented to the user at the appropriate time.
*/
export function setPaused(pause: boolean): void;
/** Whether in-app messaging is currently paused. */
export function getPaused(): Promise<boolean>;
}
export namespace Location {
/** Prompts the user for location permissions to allow geotagging from the OneSignal dashboard. */
export function requestPermission(): void;
/** Disable or enable location collection (defaults to enabled if your app has location permission). */
export function setShared(shared: boolean): void;
/**
* Checks if location collection is enabled or disabled.
* @param {(value: boolean) => void} handler
*/
export function isShared(): Promise<boolean>;
}
export namespace Session {
/** Increases the "Count" of this Outcome by 1 and will be counted each time sent. */
export function addOutcome(name: string): void;
/** Increases "Count" by 1 only once. This can only be attributed to a single notification. */
export function addUniqueOutcome(name: string): void;
/**
* Increases the "Count" of this Outcome by 1 and the "Sum" by the value. Will be counted each time sent.
* If the method is called outside of an attribution window, it will be unattributed until a new session occurs.
*/
export function addOutcomeWithValue(name: string, value: string | number): void;
}
}
export declare class OSNotification {
body: string;
sound?: string;
title?: string;
launchURL?: string;
rawPayload: object | string;
actionButtons?: object[];
additionalData?: object;
notificationId: string;
groupKey?: string;
groupMessage?: string;
ledColor?: string;
priority?: number;
smallIcon?: string;
largeIcon?: string;
bigPicture?: string;
collapseId?: string;
fromProjectNumber?: string;
smallIconAccentColor?: string;
lockScreenVisibility?: string;
androidNotificationId?: number;
badge?: string;
badgeIncrement?: string;
category?: string;
threadId?: string;
subtitle?: string;
templateId?: string;
templateName?: string;
attachments?: object;
mutableContent?: boolean;
contentAvailable?: string;
relevanceScore?: number;
interruptionLevel?: string;
constructor(receivedEvent: OSNotificationData);
display(): void;
}
declare type OSNotificationData = AndroidNotificationData | iOSNotificationData;
export declare enum OSNotificationPermission {
NotDetermined = 0,
Denied = 1,
Authorized = 2,
Provisional = 3,// only available in iOS 12
Ephemeral = 4
}
declare const PERMISSION_CHANGED = "OneSignal-permissionChanged";
export declare interface PushSubscriptionChangedState {
previous: PushSubscriptionState;
current: PushSubscriptionState;
}
export declare interface PushSubscriptionState {
id?: string;
token?: string;
optedIn: boolean;
}
declare const SUBSCRIPTION_CHANGED = "OneSignal-subscriptionChanged";
declare const USER_STATE_CHANGED = "OneSignal-userStateChanged";
export declare interface UserChangedState {
current: UserState;
}
export declare interface UserState {
externalId?: string;
onesignalId?: string;
}
export { }