6
votes

I have a function in Firebase Functions service that send any FCM. I would to use admin.messaging().send() function, like this reference guide, but I got this error while function is triggered, not during deploy:

TypeError: admin.messaging(...).send is not a function
   at exports.sendChatNotification.functions.database.ref.onCreate.event (/user_code/lib/index.js:113:30)
   at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27)
   at next (native)
   at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
   at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
   at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36)
   at /var/tmp/worker/worker.js:700:26
   at process._tickDomainCallback (internal/process/next_tick.js:135:7)

I can see this error in Functions->Log inside Firebase console.
That's my function code:

 exports.sendChatNotification = functions.database.ref('/messages').onCreate(event => {

 var message = {
    data: {
        title: 'title',
        body: 'body',
    },
    apns: {
        header: {
            'apns-priority': '10',
            'apns-expiration':'0'
          },
        payload: {
            aps: {
                sound: 'default',
                'content-available':'1'
              }
        }
    },
    android: {
        ttl: 60*1000,
        priority: 'high'
      },
    topic: 'mytopic'
};

return admin.messaging().send(message);

});

If I use admin.messaging().sendToTopic() and (changing the message structure) it works fine. It seems Firebase doesn't support its own API.

I deploy this using Firebase tools, with command line "firebase deploy".
I have updated firebase-tools and firebase-functions and firebase-admin in my functions project.

1
Your error message shows a problem with "sendChatNotitifcation", but you're showing code for "sendMessage". Please make sure you have all the correct information here. Also please indicate which version of firebase-admin you're using.Doug Stevenson
I correct the question, I had typed by hand the first line.....there is same function.enfix
Also please indicate which version of firebase-admin you're using,Doug Stevenson
I update it before this question...."firebase-admin": "~5.4.2" (from package.json)enfix

1 Answers

10
votes

The send() function was added in firebase-admin 5.9.0. If you want to use it, you should run npm install firebase-admin@latest in your functions folder to install the latest version. At the time of this writing, the latest version is 5.9.1.