UaSDK

UaSDK

The main Web SDK object. It can not be instantiated manually. It is returned by the async loader.

Constructor

new UaSDK()

Properties:
Name Type Description
sdk.channel UaSDK.Channel

The channel object for the current browser if registered.

sdk.contact UaSDK.Contact

The contact object for the current browser.

sdk.components ComponentManager

SDK components, such as Feature Flags and Preference Centers

sdk.locale LocaleManager

interface for managing the SDK's locale settings

sdk.errors UaSDK.Errors

errors which may be thrown by the SDK

analytics AnalyticsManager

access analytics methods

sdk.CustomEvent CustomEvent

The constructor to make custom events.

sdk.isSupported boolean

This is true if the browser supports the necessary features for basic operation of the SDK such as event collection. To know if the browser supports web push specifically, use the sdk.isWebPushSupported method.

sdk.isWebPushSupported boolean

This is true if the browser is supported and has web push abilities.

sdk.isAllowedPushRegistrationContext boolean

This is true if the browser is in a context that allows push registration.

Example
// You will always use this through the async loader
UA.then(function(sdk) {// use the sdk here})

Classes

Channel
Contact
Errors
Plugins

Methods

(async) canRegister()

Check if the browser can register for push notifications.

This is true if the browser isSupported and the page is secure and Notifications have not been blocked at the browser level.

Returns:

A promise that resolves to true if the browser can register for push notifications, otherwise false.

Example
if (await sdk.canRegister()) {
  // prompt for registration
}

(async) create() → {Promise.<ChannelRegistrationResult>}

Create a channel for this browser, enabling the SDK and making the browser known to Airship but without prompting them to opt in for push; this will:

  • Collect browser information for out of the box tag segmentation.
  • Register with Urban Airship and resolve the returned channel object.

This method is similar to UaSDK#register, except that it will:

  • Not prompt the user to allow notifications
  • Create an opted-out channel

If you wish to emit events about this browser, or associate it with a user in Airship, but without requiring opt in to web push notifications, you may use this method. You may later call the UaSDK#register method should the browser opt in to web push notifications.

If you have already called register() for this browser, there is no need to call this method.

Rejects with an error if registration fails, or if the browser is not a secure context. See UaSDK#canRegister.

Fires:
Returns:
Type:
Promise.<ChannelRegistrationResult>

(async) getNotificationPermission() → {Promise.<(PermissionState|undefined)>}

Get the browser permission state to display notifications. Return value will be one of 'granted', 'denied', or 'prompt'.

In contexts where notifications are not supported, or if the browser does not support notifications, the returned promise will resolve to undefined.

Returns:
Type:
Promise.<(PermissionState|undefined)>
Example
const permission = await sdk.getNotificationPermission()
if (permission === 'denied') {
  // prompt the user to change their browser settings
}

isDataCollectionEnabled() → {boolean}

Retrieves current data collection enabled state.

Returns:
Type:
boolean

the current enabled state

Example
const enabled = await sdk.isDataCollectionEnabled()
if (enabled) {
  // collect some data
}

(async) register() → {Promise.<ChannelRegistrationResult>}

Register the current browser with Airship, creating an opted in channel for the browser after prompting the user for permission if necessary; this will:

  • Fetch the browser's subscription object, prompting the user for permission if necessary.
  • Collect browser information for out of the box tag segmentation.
  • Register with Urban Airship
  • Opt a channel back in to push if it has opted out.

Rejects with an error if registration fails, or if the browser is not a secure context. See UaSDK#canRegister.

You are responsible for providing a UI to register a user.

<button id="register">Sign up for notifications</button>
<script>
document.getElementByID('register').addEventListener('click', function () {
    return UA.then(sdk => sdk.register())
})
</script>

Should you wish to register the browser with Airship but not prompt for notification opt-in, see the UaSDK#create method.

Fires:
Returns:
Type:
Promise.<ChannelRegistrationResult>

setDataCollectionEnabled(enabled) → {Promise.<void>}

Sets the data collection enabled state.

Parameters:
Name Type Description
enabled

state to set

Returns:
Type:
Promise.<void>
Example
// disable data collection for the current browser
await sdk.setDataCollectionEnabled(false)
// enable data collection for the current browser
await sdk.setDataCollectionEnabled(true)

Events

channel

The main SDK object fires a channel event when a channel is registered, loaded or changed.

Properties:
Name Type Description
ev.detail Channel

The channel that changed

ev.channel Channel

The channel that changed (Deprecated, please use ev.detail)

Example
// NOTE: if you intend to set attributes, tags, named user, or anything that could modify
// the channel within your event listener, make sure to set the `once` option to `true`
// in order to prevent an infinite loop.

  sdk.addEventListener('channel', ev => {
    ev.detail === sdk.channel
  }, { once: true })

push

If you are on an https page that is in-scope of your push-worker.js: The main SDK object fires a push event when the browser receives a push.

Properties:
Name Type Description
ev.push object

the push payload

Example
sdk.addEventListener('push', ev => {
    // ev.push is the push payload object
  })