Push notifications

Push notifications

3B Portals sends web push notifications through Firebase Cloud Messaging (FCM). Devices register from the portal, an admin triggers a send from a Flow or Apex, FCM delivers to the service worker, and the service worker displays the notification and reports back delivery and engagement.

Prerequisites

The service worker now comes from a Visualforce page

The service worker that receives and displays push is served by the PortalServiceWorker Visualforce page (<site>/b3p__PortalServiceWorker) and registered at the site root. It used to be served from the Portal static resource path (/resource/b3p__Portal/service-worker.js). It is now the same worker that provides offline support and caching, so there is one worker per portal rather than two.

This means push has a new setup dependency. A Salesforce Site only serves Visualforce pages listed under Setup, Sites, (your site), Site Visualforce Pages, and Sites do not honour page access granted through a permission set. Add PortalServiceWorker next to Portal in that list on every Site that serves a portal. Install the 11.1 package, or deploy the updated Portal static resource together with the page when working from source. If you do not, worker registration fails silently and guest and community users stop receiving push, with no error shown. See Installation and setup section 6a.

Safari and other iOS browsers only show the portal notification prompt when running as an installed app. See App installation prompts.

Device registration

  1. The portal asks permission (a confirm dialog, then the native browser prompt). A soft decline is remembered so the user is not nagged.
  2. On grant, the client registers the service worker (served by the PortalServiceWorker page at the site root) and gets an FCM token.
  3. The client calls Apex b3p.Portal action savePushToken, passing the token, timezone, and browser.
  4. The server resolves the Contact from the session key and inserts a Web_Event__c of type Push Registration carrying the FCM token, timezone, and browser. There is no separate device object; each registration is a Web Event, and the send process reads the most recent tokens per contact.

Users can also trigger registration from a page with the @setupPush action link. See Login and registration.

Suppressing the prompt per page

Tick Suppress_Push_Prompt__c on a Page__c to stop the "Would you like to receive timely notifications?" dialog appearing while that page is displayed, for pages that must stay obstruction free (or desktop only portals, where every page is ticked). Everything else about push is unchanged: an already registered device still receives notifications, but the @setupPush action link also respects prompt suppression. The flag is read at prompt time, so navigating onto a suppressed page during the short delay before the dialog also cancels it.

Sending a push (admin flow)

The entry point is the invocable Apex action "3B Send Push Notification" (PushService.send), category 3B Portals. An admin or staffing user typically calls it from a record triggered Flow.

Inputs per request:

What happens:

  1. send enqueues a queueable batch that processes at most 25 requests per job and chains itself for the rest, staying under callout limits.
  2. For each contact, it reads up to the five most recent Push Registration Web Events with a token.
  3. It mints an OAuth2 bearer token by signing a JWT with the service account private key and exchanging it at the Google token URI (the token is cached).
  4. It posts a data only message to the FCM v1 endpoint for each device token, with the title, body, click action, and a unique id.
  5. It inserts a Push_Message__c per sent message, linking the Contact and the registration, with status Sent (or the FCM error), the FCM message id, the body, the target URL, and the unique id.

Because messages are data only, the service worker is responsible for actually displaying them.

Delivery and display

The service worker (script shipped in the Portal static resource, served through the PortalServiceWorker page) initialises Firebase messaging:

Delivery and engagement tracking

The client reports status back to Apex b3p.Portal action upadtePushStatus (spelled that way in the API), which upserts the Push_Message__c on its unique id:

So each Push_Message__c carries the lifecycle Sent, Received, Clicked, keyed on the unique id minted at send time. The app badge is cleared on load and when the app becomes visible.

Troubleshooting