简体   繁体   中英

Node JS FCM token not sending notification to user

I'm trying to send a notification based on the user FCM token, I have used the firebase function to successfully send a notification if there are any changes to the firebase database for that particular user. But currently, the node.JS function does not provide any logs messages/notification sent to the user. Please do help me in resolving these issues.

//import firebase functions modules
const functions = require('firebase-functions');
//import admin module
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);


// Listens for new messages added to messages/:pushId
exports.pushNotification = functions.database.ref('/Notification/{receiver_id}/push_id/{job_id}').onWrite((data, context) => {

  const receiver_id = context.params.receiver_id;
  const job_id = context.params.job_id;
  console.log('Start');
  console.log('receiverID : ' + receiver_id);
  console.log('jobID : ' + job_id);

  const DeviceToken = admin.database().ref(`/User/${receiver_id}/fcmtoken`).once('value');

    return DeviceToken.then(result => 
    {
        const token_id = result.val();
        console.log(token_id);
        const payload = 
        {
            notification:
            {
                title: "New Job Request",
                body: `JobID ` + job_id,
                tag: collapseKey,
                icon: "default",
                color: '#18d821',
                sound: 'default',
            }
        };

        return admin.messaging().sendToDevice(token_id, payload)
        .then(response => 
            {
                console.log('This was a notification feature.');
                return null;

            })
            .catch(function(error) {
                console.log('Error sending message:', error);
            });
    });
});

It's not displaying any log messages or any notification.

You are using the incorrect promise. The sendToDevice may be abort when the triggger function finish because it isn't waiting for that promise.

exports.pushNotification = functions.database.ref('/Notification/{receiver_id}/push_id/{job_id}').onWrite((data, context) => {

  const receiver_id = context.params.receiver_id;
  const job_id = context.params.job_id;
  console.log('Start');
  console.log('receiverID : ' + receiver_id);
  console.log('jobID : ' + job_id);

  const DeviceToken = admin.database().ref(`/User/${receiver_id}/fcmtoken`).once('value');

  return DeviceToken.then(result => 
    {
        const token_id = result.val();
        console.log(token_id);
        const payload = 
        {
            notification:
            {
                title: "New Job Request",
                body: `JobID ` + job_id,
                tag: collapseKey,
                icon: "default",
                color: '#18d821',
                sound: 'default',
            }
        }; 
        return admin.messaging().sendToDevice(token_id, payload) 
    })
    .then(response => {
        console.log('This was a notification feature.');
        return null; 
    })
    .catch(function(error) {
        console.log('Error sending message:', error);
    });                
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM