Docs

  • Channels Channels
  • Beams Beams
  • Developers
  • Support
  • Blog
  • Sign up
    • Search powered by Algolia
    • Sign in
    • Sign up
    • Channels
    • Beams
    • Getting started
      • Android
        • 1. Configure FCM
        • 2. Integrate SDK
        • 3. Initialize Beams
        • 4. Publish Notifications
      • iOS
        • 1. Configure APNS
        • 2. Integrate SDK
        • 3. Publish Notifications
      • Web
        • 1. SDK integration
        • 2. Safari configuration
      • Flutter
        • 1. Configure FCM and APNS
        • 2. Integrate SDK
        • 4. Publish Notifications
    • Concepts
      • Subscribers
      • Device interests
      • Authenticated users
      • Insights
      • Webhooks
    • Guides
      • Handle incoming notifications
        • Android
        • iOS
        • Web
        • Flutter
      • Publishing to multiple devices
      • Publish to specific user
        • Android
        • iOS
        • Web
        • Flutter
      • Web push guides
        • Using an existing service worker
        • Web notification permissions in Firefox
        • Handling Safari certificate expiration
    • Reference
      • Client SDKs
        • Android
        • iOS
        • Web
      • All Libraries
      • Server SDKs
        • Go
        • PHP
        • Node.js
        • Python
        • Java/Kotlin
        • Ruby
        • Swift
      • API
        • Publish API
        • Customer API
        • Device API
        • Reporting API
        • Webhooks
      • Platform Publish Formats
    • Pusher lab

    Handle incoming notifications: Web

    By default, the Beams web SDK will always display a notification when one arrives and will open a new tab based on the deep_link property provided in the publish payload. We offer two different methods of customizing this behavior.

    ∞ Overriding default SDK behavior

    If you would like to take complete control of how notifications are handled and displayed, you can provide a custom onNotificationReceived handler. This will be triggered when a notification arrives and will disable the default SDK behavior.

    importScripts("https://js.pusher.com/beams/service-worker.js");

    PusherPushNotifications.onNotificationReceived = ({ pushEvent, payload }) => {
    // NOTE: Overriding this method will disable the default notification
    // handling logic offered by Pusher Beams. You MUST display a notification
    // in this callback unless your site is currently in focus
    // https://developers.google.com/web/fundamentals/push-notifications/subscribing-a-user#uservisibleonly_options

    // Your custom notification handling logic here 🛠️
    // https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification
    pushEvent.waitUntil(
    self.registration.showNotification(payload.notification.title, {
    body: payload.notification.body,
    icon: payload.notification.icon,
    data: payload.data,
    })
    );
    };

    ∞ Adding additional custom logic, keeping default behavior

    If you would like to add custom logic when a notification arrives but still make use of the default implementation, you can call the handleNotification callback in your onNotificationReceived handler. For example, you could choose to only call handleNotification when your site isn’t in focus.

    importScripts("https://js.pusher.com/beams/service-worker.js");

    PusherPushNotifications.onNotificationReceived = ({
    pushEvent,
    payload,
    handleNotification,
    }
    ) => {
    // Your custom notification handling logic here 🛠️
    // This method triggers the default notification handling logic offered by
    // the Beams SDK. This gives you an opportunity to modify the payload.
    // E.g. payload.notification.title = "A client-determined title!"
    pushEvent.waitUntil(handleNotification(payload));
    };

    Contents

    • Overriding default SDK behavior
    • Adding additional custom logic, keeping default behavior

    Spotted something that isn’t quite right? Create an issue on GitHub.

    Copyright © 2024 Pusher Ltd. All rights reserved.

    • Support,
    • Status
    • Follow Pusher on Twitter Twitter
    • Subscribe to Pusher’s channel on YouTube
    • Follow Pusher on LinkedIn
    • Follow Pusher on Github GitHub
    • Follow Pusher on Twitch Twitch
    • Follow Pusher on Discord Discord
    OSZAR »