【问题标题】:push notifications not working (using firebase cloud functions + expo)推送通知不起作用(使用 Firebase 云功能 + 博览会)
【发布时间】:2021-04-19 02:17:48
【问题描述】:

我正在尝试使用 expo 令牌、firebase 云功能和 react 原生前端为用户实现推送通知。

这就是我在前端做事的方式,它正确地调用了函数:

const writeNotification = Firebase.functions().httpsCallable('writeNotification');
            writeNotification({ 
                type: 0,
                senderUID: this.state.likerUID,
                recieverUID: this.state.posterUID,
                postID: this.state.postID,
                likerUsername: this.state.likerUsername
            })
            .then((result) => {
                console.log(result)
            })
            .catch((error) => {
                console.log(error);
            });

这里是云功能:

exports.writeNotification = functions.https.onCall((data, context) => {

    //Get the information of the user who is going to recieve the notification
    admin.firestore()
    .collection('users')
    .doc(data.recieverUID)
    .get()
    .then(function(doc) {
        if (doc.exists) {
            //Find out if the user will accept push notifications
            if (doc.data().pushStatus) {

                var messages = []

                //Write the notification and add it to messages
                messages.push({
                    "to": doc.data().token,
                    "sound": "default",
                    "title":"you got a like!",
                    "body": data.likerUsername + " liked your post!"
                });

                //Post it to expo
                fetch('https://exp.host/--/api/v2/push/send', {
                    method: 'POST',
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(messages)
                });

                return (
                    "notification written"
                )
            }
            else {
                console.log("doesnt accept push notifications: " + doc.data().username)
                return
            }
        } else {
            // doc.data() will be undefined in this case, so this wont even come up honestly
            console.log("No such document!");
            return
        }
    })
    .catch(function(error) {
        console.error("Error finding user: ", error);
    });

    return (
        true
    )
});

我的问题在于获取,并且在云功能日志中收到此错误:

所以看起来问题是在写到 expo 时发生的。我做错了那部分吗?感谢任何帮助,这是我第一次实现推送通知

【问题讨论】:

    标签: javascript react-native google-cloud-functions expo


    【解决方案1】:

    我忘记将它添加到云函数文件中:

    const fetch = require('node-fetch');
    

    代码现在可以工作,收到推送通知:)

    【讨论】:

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