1

I have implemented a simple chat using Twilio conversations API. I'm following the doc for web push notifications (https://www.twilio.com/docs/conversations/javascript/push-notifications-web) and it's not clear to me what "FCM SECRET" I need to include in my Twilio Credential. I have tried with my firebase app key pair (public and private) and also with firebase web API key, but in the Twilio logs I allways get a GCM/FCM unauthorized error: GCM/FCM API key is revoked or invalid error.

Update:

My app code:

import { initializeApp } from "https://www.gstatic.com/firebasejs/9.8.2/firebase-app.js";
import { getMessaging, getToken, onMessage } from "https://www.gstatic.com/firebasejs/9.8.2/firebase-messaging.js";

const firebaseConfig = {
    apiKey: "...",
    authDomain: "...",
    projectId: "...",
    storageBucket: "...",
    messagingSenderId: "...",
    appId: "..."
};

const firebase = initializeApp(firebaseConfig);
const messaging = getMessaging(firebase);

const serviceWorkerRegistration = await navigator
    .serviceWorker
    .register('/js/firebase/firebase-messaging-sw.js');

if (firebase && messaging) {

    // getting FCM token
    getToken(messaging, {
        vapidKey: '...',
        serviceWorkerRegistration: serviceWorkerRegistration,
    }).then(async (fcmToken) => {

        console.log('token', fcmToken)
        const conversationsClient = await getConversationsClient();

        conversationsClient.setPushRegistrationId('fcm', fcmToken);

        onMessage(messaging, payload => {
            console.log('Message received. ', payload);
            conversationsClient.handlePushNotification(payload);
        });

    }).catch((err) => {
        console.log('Error getting token', err);
    });

} else {
    // no Firebase library imported or Firebase library wasn't correctly initialized
}

The fcmToken is generated and successfuly registered in Twilio.

The Binding is created In Twilio with my credential referenced:

enter image description here

Cesar
  • 707
  • 3
  • 13

1 Answers1

1

I believe the FCM Secret should be the apiKey that you get when you register the web application and get the details like this:

Screenshot of the Firebase setup process showing the section where you add the Firebase SDK to your application. The first key in the Firebase Config is the apiKey.

You also need to ensure you are using that config when you initialize the Firebase SDK in your application.

If you are using that config, then perhaps you can share the code that you are using in your app to initialize Firebase and request an fcmToken from the user.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Same error in logs using the apiKey. On the other hand, I am almost sure that the error in the Twilio logs is when Twilio tries to send the push notification through firebase, at this stage it does not yet depend on the code of my application, but on the "FCM SECRET" that is included in the credential, or am I wrong? – Cesar Jun 03 '22 at 15:49
  • What I mean is that as far as I understand, the code in my application is to receive the push notification sent by Twilio using Firebase, but Twilio in the logs is saying that it could not send the push notification because the "FCM SECRET" entered in the Credential is not valid. – Cesar Jun 03 '22 at 15:55
  • It might be that the token you are getting from the user is revoked or invalid. Can you share how you are getting the FCM token from the user in your app? – philnash Jun 04 '22 at 04:34
  • I have updated the question with my application code – Cesar Jun 04 '22 at 16:45
  • Thanks for updating. Do you ask the user for permission to send notifications at any point? Take a look at this [Firebase example](https://github.com/firebase/quickstart-js/blob/master/messaging/index.html) where if there's no token in response to `getToken` the UI changes to ask for notification permissions. – philnash Jun 05 '22 at 23:46
  • If I send a push notification using the Firebase console, my code receives the notification. The problem is with the push notifications sent by Twilio when a new message arrives in my conversation, which according to the error in the Twilio logs, (GCM/FCM API key is revoked or invalid), could not be sent because the "FCM Secret" is revoked or invalid. – Cesar Jun 06 '22 at 17:01
  • I had to enable the legacy Cloud Messaging API and use the legacy "Server key" token as my "FCP SECRET" for Twilio to send the push notification and stop the error from appearing in the Twilio logs. But I can't find a way to get them sent using the new Firebase Cloud Messaging API – Cesar Jun 06 '22 at 19:56
  • Oh, thanks for continuing to explore this. I will follow up with the Conversations team about this to see if we can at least document this better, if not use the newer APIs. – philnash Jun 06 '22 at 21:49
  • Just a quick question, are you also in touch with Twilio support about this? – philnash Jun 06 '22 at 21:57
  • yes, I have an open ticket, but I have barely received feedback – Cesar Jun 06 '22 at 22:03
  • Just talking internally about this. Could you perhaps share your ticket number so we can check up on it? – philnash Jun 06 '22 at 22:04
  • The ticket number is 8769194 – Cesar Jun 06 '22 at 23:09
  • 1
    Ah, and I see the support team have referenced this question there too and will be working to update the documentation. Thanks for your help with this! – philnash Jun 07 '22 at 02:32
  • Any update on this? Getting the same error.. Also, even using the legacy firebase server key is not sending any push notification from twilio conversations – Pratish Shrestha Jul 27 '23 at 13:36
  • I no longer work at Twilio, so I don’t know. It’s likely best to reach out to support for help. Or ask a new question, sharing your code. – philnash Jul 28 '23 at 16:32