首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >推送通知不工作(使用firebase云函数+ expo)

推送通知不工作(使用firebase云函数+ expo)
EN

Stack Overflow用户
提问于 2021-01-14 10:34:00
回答 1查看 120关注 0票数 0

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

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

代码语言:javascript
复制
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);
            });

云函数如下:

代码语言:javascript
复制
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
    )
});

我的问题是fetch,我在云函数日志中得到了这个错误:

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

EN

回答 1

Stack Overflow用户

发布于 2021-01-14 11:02:42

我忘了将这个添加到云函数文件中:

代码语言:javascript
复制
const fetch = require('node-fetch');

代码现在可以工作了,获取推送通知:)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65712600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档