Узел Js: почему этот код отправляет уведомление 3 раза?Firebase - PullRequest
0 голосов
/ 11 июля 2019

Я настраиваю эту функцию на другое приложение, которое отправляет уведомление только один раз. Но когда я внедряю в свое приложение, оно отправляет 3 раза push-уведомление.

Я уже пробовал с другим проектом firebase и его работой, но в этом я уже изменяю функцию и все еще нет результата.

'use strict'


const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref
('/pushnotifications/{receiver_user_id}/{notification_id}')
.onWrite((data, context) =>
{
const receiver_user_id = context.params.receiver_user_id;
const notification_id = context.params.notification_id;

console.log('We have a notification to send to :' , receiver_user_id);
if (!data.after.val())
{
    console.log('A notification has been deleted :' , notification_id);
    return null;
}

const DbPushNotif = admin.database().ref
(`pushnotifications/${receiver_user_id}/${notification_id}`)
.once('value');
return DbPushNotif.then(notifResult => {
const title = notifResult.val().title;
const body = notifResult.val().desc;
const from_user_id = notifResult.val().from;

const DeviceToken = 
admin.database().ref
(`/user/${receiver_user_id}/device_token`).once('value');

return DeviceToken.then(result =>
{
    const token_id = result.val();

    const payload =
    {
        notification:
        {
            title: `${title}`,
            body: `${body}`,
            icon: "default"
        }
    };

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

 });

});

Я ожидаю, что выходные данные будут отправлять push-уведомления 1 раз, но фактические отправляются 3 раза для push-уведомлений.

...