[英]How do i give notification every time user get registered to my app, using Cloud Functions for Firebase
[英]How do I connect my Flutter App to a notification sent by a firebase cloud function?
每当我的 Firestore 中的特定值发生变化时,我都会尝试自动向 Flutter 应用程序发送通知。 云 function 被触发,但我的应用程序根本没有收到消息。 我设法使用 firebase 控制台中的消息获取消息,所以我侦察云 function 没有正确发送消息? 我还应该提到我没有收到任何错误。
快速概述我正在做的事情:
这是我的云 function
import * as functions from "firebase-functions";
import admin = require("firebase-admin");
admin.initializeApp();
exports.sendPhValueNotification = functions.firestore.document("/phValues/values").onUpdate(async (_,
context) => {
const phValue = context.params.ph;
if (phValue < 7.2 || phValue > 7.8) {
// Notification details.
const payload = {
topic: "phValues",
notification: {
title: `PH-Wert ${phValue < 7.2 ? "unter" : "über"} dem Richtwert!`,
body: `Der PH-Wert im Whirlpool beträgt ${phValue}`
}
};
// Send notifications to everyone
await admin.messaging().send(payload);
}
});
这是我在 Flutter 应用程序中的设置
await FirebaseMessaging.instance.subscribeToTopic('phValues');
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print('Got a message whilst in the foreground!');
print('Message data: ${message.data}');
if (message.notification != null) {
print('Message also contained a notification: ${message.notification}');
}
});
原来这只是我的一个愚蠢的错误。 我从 firestore 读取值的方式是错误的,因为我在 if 语句中测试值,所以我从未真正发送过消息。
这现在有效:
functions.firestore.document("/phValues/values").onUpdate(async (change) => {
const phValue = change.after.ph; // This line
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.