skip to Main Content

I hope you can help me with this problem. I have a Firebase Cloud Function, which sends a message via FCM to a certain topic when a document is created on the firestore database. Here is my code for it:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();

exports.onWordpressPostCreate = functions.firestore
    .document("wpPosts/{title}")
    .onCreate((snap, context) => {

        console.log("-------------start create function-----------------");

        const data = snap.data();
        const title = data.title;
        const excerpt = data.excerpt;

        const payload = {

            notification: {
              title: `Neuer Deal: ${title}`,
              body: excerpt,
              badge: "1",
              sound: "default",
            },

        };


        return admin.messaging().sendToTopic("newDeal", payload)
            .then((response) => {

                console.log("Successfully sent message:", response);

            })
            .catch((error) => {
            console.error("FCM failed", error);
            });
    });

It all worked fine in the past, but today I received an error Message in the Logs of Firebase Cloud functions, that the endpoint would be deprecated.

"message: ‘An unknown server error was returned. Raw server response: "{"error":"Deprecated endpoint, see https://firebase.google.com/docs/cloud-messaging/migrate-v1"}"’ "

Is this a known issue? I do not know what I should change in my function, because I do not use a specific HTTP endpoint there. I only use the funtion sendToTopic(). Maybe someone had this problem too and can help me here.
In addition I have to say that sometimes the function works and sometimes I get this error message as kind of a warning that it would not be work in the future at all.

I searched Google for this problem and I found that the Legacy FCM API should be migrated to the HTTP v1. But I do not know how this can affect my function or what I have to change there. In the article of this link it says that I have to change the endpoint and the structure of the body but in Firebase Cloud Functions this is all handled by the FCM functions itself. I only have to call sendToTopic() with payload…

2

Answers


  1. the sentToTopic method has been deprecated by firebase, you should send topics using the send method and add the topic in the payload like so

    {
       topic: "a topic here",
       notification: {
                  title: `Neuer Deal: ${title}`,
                  body: excerpt,
                  badge: "1",
                  sound: "default",
      },
    
    Login or Signup to reply.
  2. The sendToTopic and sendToDevice methods are deprecated. You should use the following alternatives instead.

    for token:

    admin.messaging().send({
        token: 'fcm_token',
        notification: {
        title,
        body,
      },
      android: {
        notification: {
          sound: "default"
      }
    },
      apns: {
       payload: {
          aps: {
              sound: 'default'
          }
      }
     },
    })
    

    for topic:

    admin.messaging().send({
        topic: 'topic here',
        ...other property
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search