使用 post 请求向 FCM 主题发送消息未送达 Flutter

Sent messages to FCM topics using post request don't get delivered Flutter

我使用 firebase_messaging 推送通知和发送消息我使用 POST 请求。

FCM token 发送消息时一切正常,但是向 topic 发送消息时(我基本上修改了用于 token 消息的请求)他们不会收到已交付。

我检查过主题是正确的,并且在 Android 物理设备上我正确订阅了该主题,因为从 FCM 控制台发送主题消息时它会立即传送。 你能看出我哪里做错了吗?

非常感谢。

令牌消息(有效):

void sendOrderCollected(Order order) async {
    var customerName = order.customerName;
    var customerFcmToken = order.customerFcmToken;

    await post('https://fcm.googleapis.com/fcm/send',
        headers: <String, String>{
          'Content-Type': 'application/json',
          'Authorization': 'key=$firebaseServerKey'
        },
        body: jsonEncode({
          'notification': <String, dynamic>{
            'title': sprintf(
                AppLocalizations.instance.text('ORDER_COLLECTED_PUSH_SUBTITLE'),
                [order.shopName]),
            'body': sprintf(
                AppLocalizations.instance.text('ORDER_COLLECTED_PUSH_BODY'),
                [customerName]),
            'sound': 'true'
          },
          'priority': 'high',
          'data': <String, dynamic>{
            'click_action': 'FLUTTER_NOTIFICATION_CLICK',
            'id': '1',
            'status': 'done'
          },
          'to': customerFcmToken
        })).whenComplete(() {
//      print('sendOrderCollected(): message sent');
    }).catchError((e) {
      print('sendOrderCollected() error: $e');
    });
  }

主题消息(未送达):

  void sendNewPromotion(Promotion promotion,String topic) async {
    print('sendNewPromotion() web started.\n topic: $topic, promotion : ${promotion.toMap().toString()}'); // correct topic

    await post('https://fcm.googleapis.com/fcm/send',
        headers: <String, String>{
          'Content-Type': 'application/json',
          'Authorization': 'key=$firebaseServerKey'
        },
        body: jsonEncode({
          'notification': <String, dynamic>{
            'title': sprintf(
                AppLocalizations.instance
                    .text('PROMOTION_PUSH_SUBTITLE'),
                [promotion.productName]),
            'body': sprintf(
                AppLocalizations.instance.text('PROMOTION_PUSH_BODY'),
                [promotion.productName, promotion.price, promotion.availableQuantity]),
            'sound': 'true'
          },
//          'priority': 'high',
          'android':{
            'priority' : 'high'
          },
          'apns':{
            'headers':{
              'apns-priority': '5'
            }
          },
          'webpush': {
            'headers': {
              'Urgency': 'high'
            }
          },
          'data': <String, dynamic>{

            'click_action': 'FLUTTER_NOTIFICATION_CLICK',
            'id': '1',
            'status': 'done',
            // parameter to pass with the message
//            'promotionId': promotion.promotionId,
//            'imageUrl': promotion.imageUrl,
//            'isPromotion' : promotion.isPromotion,
//            'productName': promotion.productName,
//            'productCategory': promotion.category,
//            'vendor': promotion.vendor,
//            'price': promotion.price,
//            'description': promotion.productDescription
          },
          'to': topic // correct
//          'topic': topic  // throws error 400
        })).whenComplete(() {
      print('sendNewPromotion(): message sent');
    }).catchError((e) {
      print('sendNewPromotion() error: $e');
    });
  }
}

您在主题消息中所做的一切都是正确的,但您必须将 "to" 密钥修改为:

"to":"/topics/yourtopicyousubscribed"