首页 > 解决方案 > 无法将 Firebase 云消息导入 Flutter 应用程序

问题描述

我创建了一个简单的界面来使用 firebase 云消息服务发送通知。当我使用 firebase - 控制台发送通知时,它会显示通知。但是当我使用我的界面将数据插入到 firebase 时,它​​没有显示任何内容。我怎么解决这个问题。

Future<void> insertItem() async {
    final form = formKey.currentState;
    form.save();

    Firestore.instance.runTransaction((transaction) async {
      await transaction.set(Firestore.instance.collection("Notification").document(), {
        'User': _user,
        'Title': _title,
        'Body': _msg,
      });
    });}

- 将数据插入 Firebase 代码-

exports.notificationTrigger = functions.firestore
  .document("Notification/{notificationId}")
  .onCreate((snapshot, context) => {
    msgData = snapshot.data();
    var userRef = admin.firestore().collection("Token").doc(msgData.User);
    return userRef.get().then((doc) => {
      if (!doc.exists) {
        console.log("No Devices");
      } else {
        token = doc.data().Token;
        var payload = {
          notification: {
            title: msgData.Title,
            body: msgData.Body,
          },
          data: {
            sendername: msgData.Title,
            message: msgData.Body,
          },
        };
    return admin
      .messaging()
      .sendToDevice(token, payload)
      .then(() => {
        console.log("pushed notification");
      })
      .catch((err) => {
        console.log(err);
      });
  }
});});

可以仪表板日志

标签: firebaseflutterdartgoogle-cloud-functionsfirebase-cloud-messaging

解决方案


推荐阅读