Generate a device token for web browsers
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. 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.
- A web app served over HTTPS (required for service workers).
- Firebase added to your web app. Follow the Add Firebase to your web project 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:
1import { initializeApp } from "firebase/app";2import { getMessaging } from "firebase/messaging";34const firebaseConfig = {5// Your Firebase project configuration6};78const app = initializeApp(firebaseConfig);9const messaging = getMessaging(app);
FCM requires a service worker file named firebase-messaging-sw.js in the root of your domain. Create the file with the following content:
1importScripts("https://www.gstatic.com/firebasejs/11.8.1/firebase-app-compat.js");2importScripts("https://www.gstatic.com/firebasejs/11.8.1/firebase-messaging-compat.js");34firebase.initializeApp({5// Your Firebase project configuration6});78const messaging = firebase.messaging();
Info
Update the Firebase SDK version numbers in the importScripts URLs to match the version you use in your app.
Before retrieving a token, request notification permission from the user. The browser displays a permission prompt the first time this is called.
1const permission = await Notification.requestPermission();2if (permission === "granted") {3console.log("Notification permission granted.");4}
After the user grants permission, call getToken() with your VAPID key to retrieve the registration token:
1import { getMessaging, getToken } from "firebase/messaging";23const messaging = getMessaging();4getToken(messaging, { vapidKey: "YOUR_PUBLIC_VAPID_KEY" }).then((currentToken) => {5if (currentToken) {6// Send this token to Twilio using the DeviceRegistrations API7// or include it directly in a PushNotifications request.8} else {9console.log("No registration token available. Request permission to generate one.");10}11}).catch((err) => {12console.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.
After you retrieve the token, include it in Twilio Push Notifications API requests using "provider": "FCM". You can either:
- Send directly: Include the token in the
toarray when sending a notification. - Register the device: Store the token as a DeviceRegistration so you can send to Users by
userId.
- Send a push notification using the token.
- Obtain FCM credentials to upload to Twilio.
- Review the Firebase Cloud Messaging web setup guide for additional configuration options.