简体   繁体   English

如何将我的 Flutter 应用程序连接到 firebase 云 function 发送的通知?

[英]How do I connect my Flutter App to a notification sent by a firebase cloud function?

I'm trying to automatically send a notification to a Flutter App whenever a specific value in my firestore changes.每当我的 Firestore 中的特定值发生变化时,我都会尝试自动向 Flutter 应用程序发送通知。 The cloud function is triggered but my App simply doesn't receive the message.云 function 被触发,但我的应用程序根本没有收到消息。 I managed to get a message using the messaging in the firebase console, so I recon the cloud function doesn't send the message properly?我设法使用 firebase 控制台中的消息获取消息,所以我侦察云 function 没有正确发送消息? I should also mention that I don't receive any errors.我还应该提到我没有收到任何错误。

A quick rundown of what I'm doing:快速概述我正在做的事情:

  1. Added a cloud function to my firebase project that listens to firestore changes在我的 firebase 项目中添加了一个云 function 来监听 Firestore 的变化
  2. Subscribed to the notification topic in my Flutter App订阅了我的 Flutter App 中的通知主题
  3. Listened to the onMessage Event in my App在我的应用程序中收听了 onMessage 事件

This is my cloud 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);
}
});

And this is my setup in my Flutter App这是我在 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}');
  }
});

Turns out it was just a dumb mistake on my part.原来这只是我的一个愚蠢的错误。 The way I'm reading the value from the firestore is wrong and since I'm testing the value in the if statement, I never actually sennt a message.我从 firestore 读取值的方式是错误的,因为我在 if 语句中测试值,所以我从未真正发送过消息。

This works now:这现在有效:

functions.firestore.document("/phValues/values").onUpdate(async (change) => {
const phValue = change.after.ph; // This line

暂无
暂无

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

相关问题 每当用户注册到我的应用程序时,如何使用Cloud Functions for Firebase发出通知 - How do i give notification every time user get registered to my app, using Cloud Functions for Firebase 将 Firebase 应用与 Google Sign In 结合使用时,如何使用我的 Google Cloud Function 进行身份验证? - How do I authenticate with my Google Cloud Function when using my Firebase App with Google Sign In? 我无法将我的颤振应用程序与 Firebase 连接? - I cannot connect my flutter app with firebase? 如何将我的应用程序连接到Firebase功能,并让Nodemailer通过联系表单向我发送电子邮件? - How do i connect my app to firebase function and have nodemailer email me my contact form? 如何通过firebase云消息发送定时通知到flutter app - how to send scheduled notification to flutter app through firebase cloud messaging Firebase:如何在应用检查中注册我的 Flutter 应用? - Firebase: How do I register my Flutter app in app check? 如何修复未使用 Cloud Firestore 构建的颤振应用程序? - how do I fix my flutter app not building with cloud firestore? 如何在我的 Flutter 应用程序中正确地转换来自 Firebase Function 调用的响应 - How do I properly cast a response from a Firebase Function call in my Flutter app 我如何将 Firebase Firestore 连接到我的 swift 应用程序 - How do i connect the firebase Firestore to my swift app 如何将 Firestore 连接到我的 Firebase 应用 - How do I connect Firestore to my firebase app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM