Push Notifications

Here is how to use the push notifications API in the Javascript SDK. Before you use these methods, make yourself familiar with our guide about setting up and using browser push notifications in Bloomreach Engagement.

isAvailable()

This method allows you to check whether push notifications are supported in the current browser. The provided callback is called with the answer.

Arguments

Name

Type

Description

callback

function

Callback called with the result, see below

The callback is called with the following arguments:

Name

Type

Description

available

boolean

If true, the current browser supports push notifications. If false, it does not.

Examples

exponea.notifications.isAvailable(available => {
  if (available)
    console.log("Browser supports push notifications!")
  else
    console.log("Browser does NOT support push notifications.")
})

isSubscribed()

This method checks whether the current user is subscribed to push notifications or whether they have denied access to them.

Arguments

Name

Type

Description

callback

function

Callback called with the result, see below

The callback is called with the following arguments:

Name

Type

Description

active

boolean

If true, the user is actively subscribed to push notifications. false otherwise.

denied

boolean

If true, the user has denied access to push notifications in the browser. This is mutually exclusive with the active parameter.

Examples

Here is an example how to determine whether to show a subscription prompt for the user:

exponea.notifications.isSubscribed((active, denied) => {
  if (active) {
    // the user is already subscribed
  } else if (denied) {
    // the user has already interacted with the prompt and declined
  } else {
    // show the subscription prompt (like a web layer) and then,
    // when the user confirms, execute:
    exponea.notifications.subscribe()
  }
})

For an example of a subscription web layer, take a look at our Browser Push Notifications documentation.

subscribe()

Prompts the user to subscribe to push notifications if they are not already. This shows the standard push notifications prompt in the browser, which the user can either confirm or decline.

Arguments

Name

Type

Description

callback (optional)

function

Callback called after the user confirms or declines the push notifications prompt

customerProperties (optional)

object

Customer properties to set up when the user is successfully subscribed to push notifications. This accepts the same set of properties as the update() method does.

If provided, the callback is called with the following arguments:

Name

Type

Description

status

string

Can be one of:

  • 'error' – an error has happened. The SDK also prints the error details to the console.
  • 'permission-denied' – the user has denied the permission to show push notifications
  • 'subscribed' – the user has granted the permission and has been successfully subscribed
  • 'default' – the user has not granted nor denied the request to show push notifications. Try asking them again the next time they visit the page.
  • 'unsupported' – push notifications are not supported in this browser

It can be helpful to also take a look at the MDN documentation for Notification.requestPermission because this is the browser API that the subscribe() method uses internally.

Examples

exponea.notifications.subscribe(
    status => {
    if (status === 'subscribed')
        console.log('The user has been subscribed to push notifications')
    else
        console.log('Failed to subscribe the user to push notifications:', status)
    },
    { push_notifications: true }
)

unsubscribe()

Unsubscribes the user from push notifications if they are already, or silently succeeds if the are not.

Arguments

Name

Type

Description

callback (optional)

function

Callback called with the result, see below

If provided, the callback is called with the following arguments:

Name

Type

Description

status

string

Can be one of:

  • 'error' – an error has happened. The SDK also prints the error details to the console.
  • 'unsubscribed' – the user has been successfully unsubscribed
  • 'unsupported' – push notifications are not supported in this browser

Examples

exponea.notifications.unsubscribe(status => {
  if (status === 'unsubscribed')
    console.log('The user has been unsubscribed from push notifications')
  else
    console.log('Failed to unsubscribe the user from push notifications:', status)
})

Did this page help you?