# Push Notifications on Web for Programmable Chat

> \[!CAUTION]
>
> Programmable Chat has been deprecated and is no longer supported. Instead, we'll be focusing on the next generation of chat: Twilio Conversations. Find out more about the [EOL process here](https://www.twilio.com/en-us/changelog/programmable-chat-end-of-life-notice).
>
> If you're starting a new project, please visit the [Conversations Docs](/docs/conversations) to begin. If you've already built on Programmable Chat, please visit our [Migration Guide](/docs/conversations/migrating-chat-conversations) to learn about how to switch.

## Push Notifications on Web

Push notifications are an important part of the web experience. Users have grown accustomed to having push notifications be a part of virtually every app that they use. The JavaScript Programmable Chat SDK is built to have Firebase Cloud Messaging (FCM) push notifications integrated into it. Managing your push credentials is necessary as your registration token is required for the Chat SDK to be able to send any notifications through FCM. Let's go through the process of managing your push credentials.

## Step 1 - Enable push notifications for your Service instance

**IMPORTANT:** The default enabled flag for new Service instances for all Push Notifications is `false`. This means that Push will be disabled until you explicitly enable it. Follow [this guide](/docs/chat/push-notification-configuration) to do so.

## Step 2 - Configure Firebase

The developer must configure Firebase Cloud Messaging (FCM) before configuring notifications. Google provides a [Firebase Console](https://console.firebase.google.com/) to manage Firebase services and configurations.

### Create a project on Firebase

To use push notifications for your Android apps, you will need to create a project on the [Firebase Console](https://console.firebase.google.com/):

![Firebase project creation form with fields for project name and country selection.](https://docs-resources.prod.twilio.com/ba69aca291a7222738a751abdca1019cf40d7557c75a6af24f589bba3453589d.png)

### Get project's configuration

The Firebase Cloud Messaging (FCM) requires configuration to initialize. The Firebase console has a way to generate this configuration.

After you create a Firebase project, you can select option to add Firebase to your web app:

![Options to add Firebase to iOS, Android, or web apps.](https://docs-resources.prod.twilio.com/7f1d0b91eaf598e2422f2569dc145737278b08b6d2ba7056670611dec40fac72.png)

Clicking the right-most link ("**Add Firebase to your web app**") will bring up this dialog:

![Code snippet for initializing Firebase in a web app with a copy button.](https://docs-resources.prod.twilio.com/d003813eb6efe3ffecda04b0ed655324d973f3c6f943420b34b2d7e0469fe9a0.png)

This dialog contains sample JavaScript code with filled-in parameters that you can use in your newly created project.

Save this sample code with configuration - we will use it later in this guide.

## Step 3 - Upload your API Key to Twilio

Now that we have our app configured to receive push notifications, let's upload our API Key by creating a Credential resource. Check out [the Credentials page in the Twilio console](/console/notify/credentials/create) page to generate a credential SID using your API key.

![Console interface for adding an FCM push credential with fields for friendly name and FCM secret.](https://docs-resources.prod.twilio.com/b09ddcd12f62f4086f185762b51acf1cc2e6bd98d7eae5addf056bc57ec3c31e.png)

## Step 4 - Pass the API Credential Sid in your Access Token

This step is to ensure that your Chat JS SDK client Access Token includes the correct `credential_sid` - the one you created in Step 3 above. Each of the Twilio SDKs enables you to add the `push_credential_sid`. To learn how your preferred SDK handles the credential, consult its documentation. The following example shows the Node.js Twilio SDK:

```js
var chatGrant = new ChatGrant({
  serviceSid: ChatServicesSid,
  pushCredentialSid: FCM_Credential_Sid,
});
```

## Step 5 - Initialize Firebase in your web app

Now it's time to initialize the Firebase with sample code from Step 2 above.

In your web app's early initialization sequence, call the sample code (and do not forget to include/import the Firebase library provided by Google). We recommend including an additional check for the correct import of the Firebase libraries.

```javascript
// Initialize Firebase
var config = {
  apiKey: "...",
  authDomain: "...",
  databaseURL: "...",
  projectId: "...",
  storageBucket: "...",
  messagingSenderId: "...",
};
if (firebase) {
  firebase.initializeApp(config);
}
```

## Step 6 - Request push permissions from the user and get your FCM token

In this step, we are requesting permission from the user to subscribe to and to display notifications. Again, we recommend adding checks for the correct initialization of Firebase.

```javascript
if (firebase && firebase.messaging()) {
  // requesting permission to use push notifications
  firebase
    .messaging()
    .requestPermission()
    .then(() => {
      // getting FCM token
      firebase
        .messaging()
        .getToken()
        .then((fcmToken) => {
          // continue with Step 7 here
          // ...
          // ...
        })
        .catch((err) => {
          // can't get token
        });
    })
    .catch((err) => {
      // can't request permission or permission hasn't been granted to the web app by the user
    });
} else {
  // no Firebase library imported or Firebase library wasn't correctly initialized
}
```

## Step 7 - Pass the FCM token to the Chat JS SDK and register an event listener for new push arrival

If you got to this step, then you have Firebase correctly configured and an FCM token ready to be registered with Chat SDK.

This step assumes that you have Chat Client created with correct Access Token from Step 4.

```javascript
// passing FCM token to the `chatClientInstance` to register for push notifications
chatClientInstance.setPushRegistrationId("fcm", fcmToken);

// registering event listener on new message from firebase to pass it to the Chat SDK for parsing
firebase.messaging().onMessage((payload) => {
  chatClientInstance.handlePushNotification(payload);
});
```

> \[!NOTE]
>
> Make sure to register service workers for multiple Chat channels in different
> tabs. The service worker needs to be running in order to get push
> notifications via FCM when a Chat tab is in the background.

Next: [Webhook Events](/docs/chat/webhook-events)
