【问题标题】:sending notification from firebase cloud functions getting null as notification从 Firebase 云函数发送通知,通知为空
【发布时间】:2019-11-29 02:30:30
【问题描述】:

当我从 firebase 云函数发送通知时,当孩子被添加到 firebase 数据库时,它成功发送了一个通知,但是当我从 firebase 数据库中删除孩子时,我收到通知,因为下面是我得到的屏幕截图链接当我从 firebase 数据库中删除孩子时的通知

//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 debate created
exports.pushNotifications = functions.database.ref('app_title').onWrite(( change,context) => {
    console.log('Push notification event triggered');

       const app_title = change.after.val();

        const payload = {notification: {
            title: 'New Notification',
            body: `${app_title}`
            }
        };

   return admin.messaging().sendToTopic("appGlobal",payload)
       .then(function(response){
            console.log('Notification sent successfully:',response);
       })
       .catch(function(error){
            console.log('Notification sent failed:',error);
       });
});

【问题讨论】:

  • 在日志中打印您从 FCM 获得的通知

标签: android firebase firebase-realtime-database firebase-cloud-messaging google-cloud-functions


【解决方案1】:

change.after.val()为null,因为它被删除了,你应该返回change.before.val()进行删除操作(参考answer)

检查类似的更改以检测删除

//Comment created

if (change.after.exists() && !change.before.exists()) {
    return //TODO
    });

    //Comment deleted
} else if (!change.after.exists() && change.before.exists()) {
    return //TODO              
    });

    //Comment updated
}  else if (change.after.exists() && event.data.previous.exists()) {
        return //TODO
    }

你需要实现onCreate、onDelete而不是onWrite以获得更好的结果

onWrite(),在数据被创建、更新或删除时触发 实时数据库。

onCreate(),在实时创建新数据时触发 数据库。

onUpdate(),当实时更新数据时触发 数据库。

onDelete(),当从 Realtime 中删除数据时触发 数据库。 (ref)

【讨论】:

  • 如果它有效,请接受它作为答案。它对其他人也有帮助。随时欢迎您
  • 谢谢@majuran 这真的帮了我
猜你喜欢
  • 2019-09-25
  • 2019-02-23
  • 2018-09-29
  • 2019-01-30
  • 2018-06-01
  • 1970-01-01
  • 2018-01-18
  • 2018-04-14
  • 2021-04-04
相关资源
最近更新 更多