Flutter:Firebase消息未收到通知

时间:2020-10-27 17:44:28

标签: firebase flutter google-cloud-functions firebase-cloud-messaging

我在flutter应用程序中使用了Firebase消息传递,并且通知是由Firebase云功能触发的。整个执行不会引发任何错误,但是通知永远不会到达订阅该主题的我的应用程序。我两天前已经尝试过了,一切都正常,但是当我现在再次尝试时,它没有通过。

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


admin.initializeApp();
exports.notifFunc = functions.database.ref("/collegeData/{collegeId}/notices/{noticeId}").onCreate((snapshot, context) => {
    const notice = snapshot.val();
    const colId = context.params.collegeId;
    console.log(colId);
    return admin.messaging().sendToTopic(
        "collegeId"+colId, 
        {
            notification: {
                title: notice.companyName, 
                body: notice.notice, 
                clickAction: "FLUTTER_NOTIFICATION_CLICK",
                badge: "!",
            }
        }
    ).then(function (response) {
        console.log("Push response : " + response);
        return response
    })
    .catch(function (error) {
        console.error("Error in sending push");
    });
});

每次触发函数并且函数执行成功而没有错误时,日志显示“推送响应:对象”消息。

void initState() {
    final fbm = FirebaseMessaging();
    fbm.configure(
      onMessage: (msg) {
        print(msg);
        return;
      },
      onLaunch: (msg) {
        print(msg);
        return;
      },
      onResume: (msg) {
        print(msg);
        return;
      },
    );
    Provider.of<User>(context, listen: false)
        .loadCurrentUserProfile()
        .then((profile) {
      setState(() {
        String collegeId = Provider.of<User>(context, listen: false).collegeId;
        String userId = Provider.of<Auth>(context, listen: false).userId;
        fbm.subscribeToTopic("userId" + userId).then((value) {
          print("Success");
        }).catchError((e) {
          print("Error");
        });
        if (collegeId != null) {
          Provider.of<Auth>(context, listen: false).setCollegeId(collegeId);
          fbm.subscribeToTopic("collegeId" + collegeId).then((value) {
            print("Success");
          }).catchError((e) {
            print("Error");
          });
        }

        _loading = false;
      });
    });
    super.initState();
  }

这是我应用程序第一个屏幕上有状态小部件中initState函数的代码。订阅函数将触发,并且都将打印“成功”。 但是该应用程序从不应答任何通知,因为处理程序从不打印任何消息。

我检查了主题名称的可变部分的值是否完全相同。 我在这里想念什么?

0 个答案:

没有答案