![](/img/trans.png)
[英]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.