【问题标题】:Firebase Cloud Messaging in flutter function works but didn't receive notificationFlutter 功能中的 Firebase 云消息传递工作但未收到通知
【发布时间】:2020-10-21 13:17:28
【问题描述】:

我想向设备发送 fcm 通知,但问题是它完全命中 api 成功,但推送通知没有发送到应用程序。

 Future<bool> callOnFcmApiSendPushNotifications(
      Future<String> userToken, Message message) async {

    print(message.message);
    print(userToken);
    final postUrl = 'https://fcm.googleapis.com/fcm/send';
    final data = {
      "notification": {"body": message.message, "title": "New Notification"},
      "priority": "high",
      "data": {
        "click_action": "FLUTTER_NOTIFICATION_CLICK",
        "id": "1",
        "status": "done"
      },
      "to": "$userToken"
    };

    final headers = {
      'content-type': 'application/json',
      'Authorization':
          'key=Server key'
    };

    print('hit till response');

    final response = await Dio()
        .post(postUrl, data: data, options: Options(headers: headers));
    print(response.statusCode);
    if (response.statusCode == 200) {
      // on success do sth
      print('test ok push CFM');
      return true;
    } else {
      print(' CFM error');
      // on failure do sth
      return false;
    }
  }

  static Future<String> getToken(userId) async {
    final Firestore _db = Firestore.instance;

    var token;
    await _db
        .collection('users')
        .document(userId)
        .collection('tokens')
        .getDocuments()
        .then((snapshot) {
      snapshot.documents.forEach((doc) {
        token = doc.documentID;
      });
    });

    return token;
  }

我的消息发送代码

Future<void> addMessageToDb(
      Message message, User sender, User receiver) async {
    var map = message.toMap();

    await firestore
        .collection("messages")
        .document(message.senderId)
        .collection(message.receiverId)
        .add(map);

    var token = getToken(message.receiverId);
    print('token setted for fcm');
    callOnFcmApiSendPushNotifications(token, message);
    print('fcm worked');

    return await firestore
        .collection("messages")
        .document(message.receiverId)
        .collection(message.senderId)
        .add(map);
  }

消息发送后我的控制台:

I/flutter (14894): hari
I/flutter (14894): token setted for fcm
I/flutter (14894): let's see
I/flutter (14894): Instance of 'Future<String>'
I/flutter (14894): hit till response
I/flutter (14894): fcm worked
I/flutter (14894): 200
I/flutter (14894): test ok push CFM

在我的控制台中,我可以看到所有打印语句部分 fcm 函数,但我的应用程序中没有收到任何通知。帮助表示赞赏

【问题讨论】:

    标签: firebase flutter push-notification firebase-cloud-messaging


    【解决方案1】:

    看来我的令牌在 fcm 函数中变为 null 已纠正它。

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 2019-10-08
      • 2020-07-16
      • 2020-06-16
      • 2019-02-06
      • 2020-07-28
      • 2017-05-05
      • 2020-01-02
      • 2021-07-22
      相关资源
      最近更新 更多