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

标签 flutter firebase-cloud-messaging

我使用 firebase_messaging 进行推送通知并使用 POST 请求发送消息。

当向FCM token发送消息时,一切正常,但是当将它们发送到topic时(我基本上修改了用于token的请求> 消息)它们不会被发送。

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

非常感谢。

token 消息(工作):

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"

关于flutter - 使用 post 请求向 FCM 主题发送的消息未送达 Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63574994/

相关文章:

javascript - Web Push 和 FCM 的区别

Flutter:如何将文件引用到 main.dart?

firebase - 如何为firebase firestore配置获取googleappid?

android - 将来自不同 Firebase 项目的两项 Firebase 服务用于一个移动应用程序

java - 如何在 Android 中保存 FCM token ?

android - Notification Messaging Style 消息存储位置

android - 无法保存 flutter 中 TabBar 选项卡位置的状态

android - 如何在 Flutter 中使用本地资源播放音频?

flutter - 什么?在 Dart 中做什么?

Android/Firebase - 解析 GCM 事件中的时间戳时出错 - 空时间戳