why messaging().sendtodevice is not working sometimes?

Viewed 27

I'm using the following code to send a notification from one device to another using FCM. Everything works fine until before return admin.messaging().sendToDevice(...). The 'Token ID: ' log displays token ID of the receiver, but when I set the variable token_id to the sendToDevice function, the notification is not called, therefore the notification is not sent. Can someone tell me what's wrong?

var firebase = require("firebase-admin");
var serviceAccount = require("./julla-tutorial.json");

  console.log("enter in then Firebase Api");
  const firebaseToken = [
    'e0T6j1AiRjaa7IXweJniJq:APA91bHNznSHSIey08s-C-c3gchci6wepvhP1QxQyYbmZ8LySI3wnu64iW7Q23GhA6VCdc4yodZoCFOgynfAb5C8O8VE81OcSv_LL-K3ET1IKGZ_6h35n-_q5EKFtfJWlzOqZr4IvpiB',
    'dNWnSqyCQbufzv1JutNEWr:APA91bFcI9FDyRxHRBEcdw4791X0e-V0k1FjXcSstUA67l94hSojMRCd6LWr2b57azNEt3z_XLwLljMX4u2mc9cZDrAVm55Mw9CHGyue-09KofWnnHNR9XWBibc4T76xOV_DWX7T2RvW',
    'cq65rtuaTCKGk5lHk7UabN:APA91bFR3kAArg6lhuBq7ktNuBk7Z9MXXk3PskqhYa8CgNaEl6MX4TQ5lo35d6XhnCQ4fEkCkyZ_j08evxE9Y4oVCRTEdqsrkccCVTE8Di47lfmDR3i1NdoL3re9oLw6F_uNsnvRoQcq'
  ]
  firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccount)
  })     
  const payload = {
    notification: {
      title: 'Demo 2345',
      body: 'dfghj',
      sound: 'default',
      color: 'yellow',
      android_channel_id: 'default',
      channel_id: 'default'
    },
    data: { id: 'broadcast', channelId: 'default' }
  }
  const options = {
    priority: 'high',
    timeToLive: 60 * 60 * 24, // 1 day
  };
  console.log('------payload---',payload);
  console.log('-----TOKEN_Array----',firebaseToken);
  console.log('-------options-----',options);
  firebase.messaging().sendToDevice(firebaseToken, payload, options).then(function (response) {
console.log('--------response',response);
   
  }) .catch(function (error) {
      console.log('-------rejet',reject);      
    });
1 Answers

It looks like you did not change the code from this tutorial: https://medium.com/@jullainc/firebase-push-notifications-to-mobile-devices-using-nodejs-7d514e10dd4

you will need to change the 2nd line of code:

var serviceAccount = require("./julla-tutorial.json");

to actually point to your own firebase-push-admin.json file which holds your private keys registering your backend app with the firebase cloud messaging api. you can download this file from the firebase console as mentioned in the above article.

I recommend hiding this file from your git history by adding it to .gitignore so you dont accidentally push your private keys to a public repo.

I will link you another resource in addition to above link which helped me implement firebase push notifications in a nodeJS backend app.

https://izaanjahangir.medium.com/setting-schedule-push-notification-using-node-js-and-mongodb-95f73c00fc2e

https://github.com/izaanjahangir/schedule-push-notification-nodejs

Further I will also link you my own repo where I am currently working on a fully functional firebase push notification implementation. Maybe it helps to actually see some example code.

https://gitlab.com/fiehra/plants-backend

Related