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 Attributes Description
sdk.channel UaSDK.Channel <nullable>

The channel object for the current browser if registered.

sdk.contact Promise.<UaSDK.Contact>

The contact object for the current browser.

Note: Contacts are not currently enabled by default. The contact Promise will reject with an error if contacts are not enabled in your SDK.

sdk.preferenceCenters PreferenceCenterManager

interface for retrieving preference center definitions.

sdk.featureFlags FeatureFlagManager

interface for determining feature flag eligibility

sdk.locale UaSDK.LocaleManager

interface for managing the SDK's locale settings

sdk.errors UaSDK.Errors

errors which may be thrown by the SDK

sdk.CustomEvent CustomEvent

The constructor to make custom events.

sdk.canRegister boolean

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

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.permission string <nullable>

The browser permission state to display notifications.

Secure contexts return 'granted', 'denied', or 'default'.

Insecure contexts always return undefined.

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) 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) 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>

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
  })