Web push setup
Host the service worker, choose a permission prompt, and add the web snippet.
Web push works in Chrome, Firefox, Edge and Safari. Visitors subscribe through a prompt on your site and can receive notifications even when the site is closed.
1. Configure web push in the portal
- Open your app and go to Settings, then choose Web.
- Choose Typical Site for a quick setup, or Custom Code if you want full control of the service worker.
- Enter your Site name and Site URL, and optionally a default icon.
- For a typical site, set up a permission prompt: a slide prompt, a subscription bell, a custom link or the native browser prompt.
- Optionally turn on a welcome notification that is sent to new subscribers.
.p12 certificate under the Safari certificate setting.2. Host the service worker
Download PushFyreSDKWorker.js from the app's install page and place it in the same directory as the pages that should receive notifications. A browser can only register a service worker from your site's own origin. Prompt wording and colours are loaded from PushFyre on each visit. Download the worker again after a PushFyre SDK update, because the file on your site does not change by itself.
3. Add the snippet
Paste this inside the <head> of every page that should show the prompt:
<script src="https://cdn.example.com/web.js?v=1.0.9" defer></script>
<script>
window.PushFyreDeferred = window.PushFyreDeferred || [];
PushFyreDeferred.push(async function (PushFyre) {
await PushFyre.init({
appId: "app_49e692ad",
});
});
</script>Put web.js and web.es6.js in the same CDN directory. The install page shows this snippet with your real app id and CDN address.
4. Test it
Open your site, accept the prompt, and check that a new row appears under Audience → Subscriptions. Then send yourself a test from Test & preview.
Who the visitor is
A new visitor is anonymous. Call login after your own sign-in so later campaigns can target that person. The same id on another browser is the same person.
PushFyreDeferred.push(async function (PushFyre) {
await PushFyre.login("user-1001");
});PushFyre.getUserId() is the browser's own id. PushFyre.getSubscriptionId() is the subscription id stored by PushFyre. Tags, outcomes and a display handler use the same object:
PushFyreDeferred.push(async function (PushFyre) {
await PushFyre.sendTag("plan", "annual");
await PushFyre.sendTags({ region: "west" });
PushFyre.sendOutcome("Purchase");
PushFyre.sendUniqueOutcome("FirstPurchase");
PushFyre.notificationWillDisplay(function (event) {
event.preventDefault();
});
});Session length and named outcomes appear on that person's profile. notificationWillDisplay can stop one notification from being shown while the page is open.
Bell and auto resubscribe
Choosing the bell to unsubscribe removes the browser subscription. The next page load does not subscribe again while that choice remains. Auto resubscribe, off unless you turn it on in Web settings, creates a subscription again only when the browser no longer has one and notification permission is still Allow. Clearing site data is the usual case.
In-app messages
The web script also shows in-app messages created in the portal. A message set to appear on app open shows when the page loads, and again when the site moves to another path without a full page load. Web pages on the campaign can limit that to paths such as /pricing or /blog/*. An empty list allows every page. To show one on an action, call PushFyre.trigger("event_name", properties) and set the campaign's In-App Trigger key to event_name with value true. That call is also stored as a custom event. See In-app messages and Custom events.
PushFyreDeferred.push(function (PushFyre) {
// Action name click actions from in-app messages
PushFyre.onInAppClick(function (event) {
if (event.actionId === "open_cart") openCart();
});
});Method reference
| Method | What it does |
|---|---|
init({ appId, environment? }) | Starts the SDK. environment is test or prod. |
subscribe(), unsubscribe() | Shows the browser prompt and subscribes, or removes this browser's subscription. |
login(externalUserId) | Links this browser to your user id. |
getUserId(), getSubscriptionId() | The browser's PushFyre user id, and the subscription id. |
sendTag(key, value), sendTags(tags) | Sets tags. |
setEmail(email), removeEmail(email) | Adds or removes an email subscription. Call only after the person agrees. |
trigger(eventName, properties?) | Records a custom event and sets an in-app trigger whose key is that name and whose value is true. |
addTrigger(key, value) | Sets an in-app trigger key to any value for this visit. It does not record a custom event. |
sendOutcome(name), sendUniqueOutcome(name) | Records a named outcome. The unique version counts a name once per visit. Names are up to 80 letters, digits, spaces, or _ . : -. |
onInAppClick(handler) | Receives in-app Action name clicks as { actionId, messageId }. The pushfyre:inapp-click event on document carries the same detail. |
notificationWillDisplay(handler) | Runs before a notification is shown while the page is open. event.preventDefault() stops it. |
liveActivities.subscribe(key), unsubscribe(key) | Joins or leaves a Live Activity. |
See it, then create an account
The demo shows a sample notification. It does not send a message. Create an account when you are ready to register a device.