首页 > 解决方案 > 通过在 android/java 中由 JavaScript 编写的函数在 FCM 中的同一应用程序中制作多种类型的通知

问题描述

我构建了一个应用程序,我需要在其中添加多种类型的通知,但我不能自己做,因为我对 JS 知之甚少

我尝试通过不同的主体、标题 ..etc 部署许多功能,但似乎无法在 Firebase Functions 中部署许多功能

'use strict'


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


exports.sendNotification = functions.database.ref('/Noti/{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 DeviceToken = admin.database().ref(`/user/${receiver_user_id}/token`).once('value');

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

        const payload = 
        {
            notification:
            {
                title: "Open this notification now",
                body: `I have a problem in my car `,
                 icon: "default" ,
                sound: "default"




            }
        };

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



这就是我要描述的问题

标签: javascriptandroidfirebase-realtime-databasefirebase-cloud-messaging

解决方案


是的,您可以部署各种功能,但您需要为每个功能设置不同的名称。函数名称在 after exports.,因此您可以拥有:

exports.sendNotification
exports.newMessageNotification
exports.newFollowerNotification
...

他们每个人都会做你希望他们做的事情。


推荐阅读