Exponea Event
Exponea Notification Service
Description
- Purpose: Manages the user subscription state for browser push notifications using the Exponea (Bloomreach) notifications API.
- Functionality Includes:
- Check if notifications are supported and available.
- Manage the user's subscription status (subscribe/unsubscribe).
- Ensure actions are performed only in client-side (browser) environments.
Utility Functions
checkAvailbility()- Description: Verifies if browser push notifications are available and supported on the current client.
- Behaviour:
- Executes only on the client.
- Calls exponea.notifications.isAvailable() to determine availability.
- If notifications are available, proceeds to check the current subscription status by calling updateStatus().
updateStatus()- Description:
- Checks whether the user is currently subscribed to notifications or has explicitly denied them.
- Behaviour:
- Executes only on the client.
- Uses
exponea.notifications.isSubscribed()to fetch the current subscription state. - If the user is not subscribed and has not denied notifications, it automatically calls
subscribe()to subscribe them.
- Description:
subscribe()- Description:
- Attempts to subscribe the user to browser notifications.
- Behaviour:
- Executes only on the client.
- Calls
exponea.notifications.subscribe(). - Once complete, it rechecks the userβs subscription status by calling
updateStatus().
- Description:
unsubscribe()- Description:
- Unsubscribes the user from receiving browser notifications.
- Behaviour:
- Executes only on the client.
- Calls
exponea.notifications.unsubscribe(). - Once the action completes, it also calls
updateStatus()to refresh the user's state.
- Description:
Client Side Guards
- Each function starts with a guard clause:
if (!import.meta.client || !window || !window.exponea) return; - Ensures the code runs only in the browser (never on the server).
- Prevents errors in server-side rendering (SSR) environments or if Exponea is not loaded.
Conclusion
- This module provides a lightweight and client-safe interface for handling push notification subscriptions with Exponea.
- It ensures smooth interaction flow:
- Check availability β Check status β Subscribe or skip.
- Automatically refreshes the subscription state after each action.
- Safeguards all logic to only execute on the client and when Exponea is present.