Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Functions "admin.messaging(...).send is not a function"

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.

like image 746
enfix Avatar asked Mar 05 '18 22:03

enfix


1 Answers

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.

like image 76
Doug Stevenson Avatar answered Oct 20 '22 19:10

Doug Stevenson