Skip to content

Set up web push notifications

This guide walks you through setting up web push notifications with Go-Mailer.

Web push notifications are short messages you send from your application or website to your users' browsers. They're shown even when your users aren't actively using the browser.

  • @go-mailer/web-push
  • JavaScript

Generate your API key

Before you start, generate an API key. It secures communication between your application and the Go-Mailer servers.

In your Go-Mailer account, go to Settings → Developers and click Generate API key.

Install and configure the SDK

Next, configure the Go-Mailer Web Push SDK in your JavaScript web application. It gives you simple utilities to capture notification permissions and store the permission credentials on our servers.

  1. Install the package

    $ yarn add @go-mailer/web-push
  2. Create the service worker file

    To receive and display web push notifications, create a file named gm-service-worker.js in your root or public directory and copy the following into it.

    gm-service-worker.js
    const logAction = async (tracking_id) => {
      try {
        await fetch(`https://messaging.go-mailer.com/webhooks/push/web/${tracking_id}`);
      } catch (e) {
        console.log("Unable to log action");
        return null;
      }
    };
    
    const tracker = { tracking_id: "" };
    
    self.addEventListener("push", (event) => {
      if (!event.data) return;
      const { title, body, icon, tracking_id } = event.data.json();
    
      tracker.tracking_id = tracking_id;
      const promiseChain = self.registration.showNotification(title, { body, icon });
      event.waitUntil(promiseChain);
    });
    
    self.addEventListener("notificationclick", (event) => {
      const clickedNotification = event.notification;
      clickedNotification.close();
    
      const promiseChain = logAction(tracker.tracking_id);
      event.waitUntil(promiseChain);
    });
  3. Register the service worker

    Register gm-service-worker.js by calling the SDK's register() function. You can call it anywhere in your app, but we recommend your application's root or main file.

    import GoMailerWebPush from "@go-mailer/web-push";
    GoMailerWebPush.register(<api_key>);

Collect and store your user's permission

Web push only works with contact profiles. Go-Mailer identifies your contacts by email address, so you must send the contact's email address along with the permission data.

The SDK includes a utility that collects and stores the permission. Pass in your user's email address and any extra user information.

import GoMailerWebPush from "@go-mailer/web-push";
// your code

GoMailerWebPush.requestPermission().then(subscription_token => {
  if (subscription_token) {
    GoMailerWebPush.saveSubscriptionToken(subscription_token, { email });
  }
});

// your code

If the user doesn't exist in your Go-Mailer account yet, a new contact is created.

That's it: your app is now set up for Go-Mailer web push notifications.