【问题标题】:How can i send different type of Notificaton with FirebaseCloud sendNotification Function如何使用 Firebase Cloud 发送通知功能发送不同类型的通知
【发布时间】:2020-08-29 09:52:08
【问题描述】:

我有这个功能,一旦我的 Db 节点发生某些变化,它就会发送通知。但我想为不同的操作发送多种类型的通知。但是当我部署我的功能时,它会覆盖之前的功能,然后我在单个脚本中尝试了所有功能,但它无法正常工作

    const functions = require('firebase-functions');

const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/All_Users/{reciever_user_id}/CouponsUsedNotifications/{notification_id}')
.onWrite((data, context ) => {
    const reciever_user_id = context.params.reciever_user_id;
    const notification_id = context.params.notification_id;
    console.log('Notification sent to : ', reciever_user_id)
    if(!data.after.val()){
            console.log('Notification Deleted : ', notification_id)

            return null;
    }
    const deviceToken = admin.database().ref(`/All_Users/${reciever_user_id}/Credentials/device_token`).once('value');
    return deviceToken.then(result =>
        {
            const token_id = result.val()
            const payload = {
                notification:
                {
                    title: "Coupon Used",
                    body: `Your Coupon has been used..!!`,
                    icon: "default"
                }
            }

            return admin.messaging().sendToDevice(token_id, payload).then(
                Response => {
                    console.log('Notification Sent')
                }
            )
        })})
exports.sendNotification = functions.database.ref('/All_Users/{reciever_user_id}/UserApprovalNotifications/{notification_id}')
.onWrite((data, context ) => {
    const reciever_user_id = context.params.reciever_user_id;
    const notification_id = context.params.notification_id;

    console.log('Notification sent to : ', reciever_user_id)

    if(!data.after.val()){
            console.log('Notification Deleted : ', notification_id)

            return null;
    }
    const deviceToken = admin.database().ref(`/All_Users/${reciever_user_id}/Credentials/device_token`).once('value');
    return deviceToken.then(result =>
        {
            const token_id = result.val()
            const payload = {
                notification:
                {
                    title: "Profile Approved",
                    body: `Your Profile has been Approved..!!`,
                    icon: "default"
                }
            }
            return admin.messaging().sendToDevice(token_id, payload).then(
                Response => {
                    console.log('Notification Sent')
                }
            )
        })})

【问题讨论】:

    标签: javascript java firebase android-studio google-cloud-functions


    【解决方案1】:

    找到解决方案...因为我不是 javascript 开发人员,这就是我苦苦挣扎的原因,但我找到的解决方案非常简单。只需将文件安装到新文件夹即可。

    exports.sendNotification = function. 
    

    sendNotification 是函数的名称,只需更改它并部署它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-23
      • 2018-06-01
      • 2021-03-18
      • 1970-01-01
      • 2021-03-08
      • 2021-04-12
      • 2017-11-16
      • 2020-12-12
      相关资源
      最近更新 更多