簡體   English   中英

節點JS FCM令牌未向用戶發送通知

[英]Node JS FCM token not sending notification to user

我正在嘗試基於用戶FCM令牌發送通知,如果該特定用戶的Firebase數據庫有任何更改,我已經使用firebase函數成功發送了通知。 但是目前,node.JS函數不提供任何發送給用戶的日志消息/通知。 請幫助我解決這些問題。

//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);
            });
    });
});

它不顯示任何日志消息或任何通知。

您使用的是不正確的承諾。 觸發函數完成后,sendToDevice可能會中止,因為它沒有在等待那個諾言。

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);
    });                
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM