Skip to contentSkip to navigationSkip to topbar
Page toolsOn this page
Looking for more inspiration?Visit the

Generate a device token for web browsers


(new)

Beta

The Push Notifications API is currently available as a Private Beta product. The information contained in this document is subject to change. You acknowledge and agree that your use of the Twilio Push Notifications API is subject to the terms of the Services in Private Beta(link takes you to an external page). This means that some features are not yet implemented and others may be changed before the product is declared as Generally Available. Private Beta products are not covered by the Twilio Support Terms or Twilio Service Level Agreement.

To send push notifications to web browsers with Twilio, your web app needs a Firebase Cloud Messaging (FCM) registration token. This token uniquely identifies the browser instance. You then send this token to Twilio by including it in the token field when sending a notification or registering a device.

FCM supports all major browsers, including Chrome, Firefox, Edge, Opera, and Safari.


Prerequisites

prerequisites page anchor
  • A web app served over HTTPS (required for service workers).
  • Firebase added to your web app. Follow the Add Firebase to your web project(link takes you to an external page) guide if you haven't already.
  • A VAPID key pair configured in the Firebase console. Go to Project Settings > Cloud Messaging > Web Push certificates and click Generate Key Pair if you don't have one.

Install the Firebase SDK and initialize Cloud Messaging in your app:

1
import { initializeApp } from "firebase/app";
2
import { getMessaging } from "firebase/messaging";
3
4
const firebaseConfig = {
5
// Your Firebase project configuration
6
};
7
8
const app = initializeApp(firebaseConfig);
9
const messaging = getMessaging(app);

Create the service worker

create-the-service-worker page anchor

FCM requires a service worker file named firebase-messaging-sw.js in the root of your domain. Create the file with the following content:

1
importScripts("https://www.gstatic.com/firebasejs/11.8.1/firebase-app-compat.js");
2
importScripts("https://www.gstatic.com/firebasejs/11.8.1/firebase-messaging-compat.js");
3
4
firebase.initializeApp({
5
// Your Firebase project configuration
6
});
7
8
const messaging = firebase.messaging();
(information)

Info

Update the Firebase SDK version numbers in the importScripts URLs to match the version you use in your app.


Request notification permission

request-notification-permission page anchor

Before retrieving a token, request notification permission from the user. The browser displays a permission prompt the first time this is called.

1
const permission = await Notification.requestPermission();
2
if (permission === "granted") {
3
console.log("Notification permission granted.");
4
}

Retrieve the FCM registration token

retrieve-the-fcm-registration-token page anchor

After the user grants permission, call getToken() with your VAPID key to retrieve the registration token:

1
import { getMessaging, getToken } from "firebase/messaging";
2
3
const messaging = getMessaging();
4
getToken(messaging, { vapidKey: "YOUR_PUBLIC_VAPID_KEY" }).then((currentToken) => {
5
if (currentToken) {
6
// Send this token to Twilio using the DeviceRegistrations API
7
// or include it directly in a PushNotifications request.
8
} else {
9
console.log("No registration token available. Request permission to generate one.");
10
}
11
}).catch((err) => {
12
console.log("An error occurred while retrieving token.", err);
13
});

Replace YOUR_PUBLIC_VAPID_KEY with the public key from your Firebase console under Project Settings > Cloud Messaging > Web Push certificates.


FCM may rotate the registration token at any time. Call getToken() on each page load to ensure you always have the current token, and send any updated tokens to your server.


Send the token to Twilio

send-the-token-to-twilio page anchor

After you retrieve the token, include it in Twilio Push Notifications API requests using "provider": "FCM". You can either: