简体   繁体   English

如何在flutter中使用javascript云函数发送fcm通知?

[英]How to send a fcm notification using javascript cloud functions in flutter?

I've created an app to test push notifications in a flutter app.我创建了一个应用程序来测试 Flutter 应用程序中的推送通知。

I am able to send a notification from the firebase messaging console and I can receive it as well in the foreground and background.我可以从 firebase 消息控制台发送通知,我也可以在前台和后台接收它。

Once I've done this, I moved to the next step which is to send it automatically using the firebase cloud messaging service and I've used javascript and I've deployed function and it gets executed without any problem.完成此操作后,我进入下一步,即使用 firebase 云消息传递服务自动发送它,我使用了 javascript 并部署了函数,并且它可以毫无问题地执行。

But the problem is that I can't receive a notification like this:-但问题是我无法收到这样的通知:-

在此处输入图片说明

but when I open my app since I've configured the firebase messaging inside initState();但是当我打开我的应用程序时,因为我已经在 initState() 中配置了 firebase 消息; I can see the notification and data get printed as well but I can't receive it like the photo above.我可以看到通知和数据也被打印出来,但我不能像上面的照片那样接收它。


What should I do?我该怎么办?

try to have a background handler or a top-level method as firebase messaging plugin's read me file says.尝试使用后台处理程序或顶级方法,如 firebase 消息传递插件的自述文件所述。


About my javascript index.js file:关于我的 javascript index.js 文件:

It sends a notification for all the tokens in the pushTokens collection when a new document added to the posts collection and it does this but the problem is that I've mentioned above.当一个新文档添加到帖子集合时,它会为 pushTokens 集合中的所有令牌发送一个通知,它会这样做,但问题是我在上面提到过。

Index.js:索引.js:

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

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


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

var notificationMessageData;

exports.fcmTester = functions.firestore.document('posts/{postID}').onCreate((snapshot, context) => {
    const notificationMessageData = snapshot.data();

    return admin.firestore().collection('pushTokens').get()
        .then(snapshot => {
            var tokens = [];

            if (snapshot.empty) {
                console.log('No Devices');
                throw new Error('No Devices');
            } else {
                for (var token of snapshot.docs) {
                    tokens.push(token.data().tokenID);
                }

                var payload = {
                    "notification": {
                        "title": "from" + notificationMessageData.writer,
                        "body": "from" + notificationMessageData.name,
                        "sound": "default"
                    },
                    "data": {
                        "sendername": notificationMessageData.writer,
                        "message": notificationMessageData.name
                    }
                }

                return admin.messaging().sendToDevice(tokens, payload)
            }

        })
        .catch((err) => {
            console.log(err);
            return null;
        })

});

Put your app in background and try sending notification again bcoz flutter firebase messaging plug-in will not create notification if you are using the app while the notification has been sent although you can see its data in but to generate notification you have to do it manually if user is currently using the app.将您的应用程序置于后台并再次尝试发送通知 bcoz flutter firebase 消息传递插件将不会创建通知,如果您在发送通知时使用该应用程序,尽管您可以看到其数据,但要生成通知,您必须手动执行如果用户当前正在使用该应用程序。

On the other cases plug-in will create notification like if app is terminated or its in background.在其他情况下,插件将创建通知,例如应用程序是否终止或其在后台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过云功能使用其UID向多个用户发送FCM通知 - Send FCM Notification to multiple users using their UID through cloud functions 如何使用 FCM 发送通知? - How to send notification using FCM? 如何使用Firebase云功能将通知发送到多个设备令牌 - How to send notification to multiple device token using firebase cloud functions 如何使用大于4kb的fcm发送通知 - How to send notification using fcm larger than 4kb 当用户 (A) 使用 firebase 云功能和 Flutter 关注用户 (B) 时如何接收通知 - How to receive a notification when a user (A) )follows user (B) using firebase cloud functions and Flutter 使用 Firebase 云函数从 firebase 发送推送通知 - Send push notification From firebase by using Firebase cloud Functions 使用Cloud Functions for Firebase向特定用户发送通知 - Send notification using Cloud Functions for Firebase to specific user 如何使用另一位使用Cloud Services for Firebase请求通知的用户插入的电子邮件发送通知? - How can I send a notification by using the email inserted by another user who requested the notification using Cloud Functions for Firebase? 具有 Firebase 实时数据库的云功能未触发 onCreate 且未发送 FCM 通知 - Cloud functions with Firebase Realtime Database not triggering onCreate and not sending FCM notification 如何动态发送 FCM 推送通知? - How to send FCM push notification dynamically?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM