首页 > 解决方案 > 是否可以将我的 Firebase 功能用于 iOS 通知?

问题描述

我已经为我的 android 应用程序上传了这个云功能到 Firebase Functions。

exports.sendNotification = functions.database.ref('/Notifications/{user_id}/{notification_id}').onWrite((change, context) => {

  const user_id = context.params.user_id;
  const notification_id = context.params.notification_id;

  //Get name
  const fromUser = admin.database().ref(`/Notifications/${user_id}/${notification_id}`).once('value');
    return fromUser.then(fromUserResult => {
                var from_user_id = fromUserResult.val().from;

                if(from_user_id === null){
                     from_user_id = "BeBetter"
                }

                console.log('You have a new notification from :' + from_user_id);

    const userQuery = admin.database().ref(`/Users/${from_user_id}/name`).once('value');

            return userQuery.then(userResult => {

                const userName = userResult.val();
                const afterData = change.after.val();

            //New Type of Notification.
            if (afterData.type === "friend request") {
                const userToken = admin.database().ref('Users/' + user_id + '/user_token').once('value');
                return userToken.then(result => {

                    const token_id = result.val();

                    const payload = {
                        notification: {
                            title: "Friend Request",
                            body: `${userName} sent you a Friend Request! <3`,
                            icon: "default",
                            click_action : "BeBetter_TARGET_NOTIFICATION"
                        },
            data : {
            from_user_id : from_user_id 
            }
                    };

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

                });

            //New Type of Notification.
            } else if(afterData.type === "friend request accepted") {
            const userToken = admin.database().ref('/Users/' + user_id + '/user_token').once('value');
            return userToken.then(result => {

                const token_id = result.val();

                const payload = {
                    notification: {
                        title: "Friend Request Accepted",
                        body: `You've got a new friend! ${userName} <3`,
                        icon: "default",
                        click_action : "BeBetter_TARGET_NOTIFICATION"
                    },
                };

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

            });

        //New Type of Notification.
        } else if(afterData.type === "experience invite") {
            const userToken = admin.database().ref('/Users/' + user_id + '/user_token').once('value');
            return userToken.then(result => {

                const token_id = result.val();

                const payload = {
                    notification: {
                        title: "Experience Invite!",
                        body: `${userName} invited you to an experience!`,
                        icon: "default",
                        click_action : "BeBetter_TARGET_NOTIFICATION"
                    },
                };

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

            });


        }else if(afterData.type === "experience completed") {
            const userToken = admin.database().ref('/Users/' + user_id + '/user_token').once('value');
            return userToken.then(result => {

                const token_id = result.val();

                const payload = {
                    notification: {
                        title: "Experience Completed!",
                        body: `${userName} joined! <3`,
                        icon: "default",
                        click_action : "BeBetter_TARGET_NOTIFICATION"
                    },
                };

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

            });
        } else {
            console.log('Not a friend request');
            return null;
        }

        });

    });


});

是否可以为我的 iOS 应用程序使用相同的功能?如何?我还没有找到一个很棒的指南。我的 AppDelegate,是超标准的。我已将其设置为能够发送云消息。但是希望它在我写入数据库的通知部分时自动发送通知。此致

标签: iosswiftfirebasegoogle-cloud-functions

解决方案


Cloud Functions 适用于添加到项目中的所有应用程序。在您的情况下,您有一个实时数据库 onWrite 函数,无论什么代码或应用程序以您指定的模式写入数据库,该函数都会触发。

我强烈建议您尝试一下,看看它是否有效。您无需发布到 Stack Overflow 即可获得尝试新事物的授权。当事情没有按您期望的方式工作时,堆栈溢出很有用。然后您的问题可以分享详细信息。


推荐阅读