代码之家  ›  专栏  ›  技术社区  ›  Ankit Jayaswal

如何使用FCM为富文本通知创建有效负载

  •  6
  • Ankit Jayaswal  · 技术社区  · 6 年前

    我正在我的一个应用程序中实现富文本通知。我知道我需要以下格式的有效载荷:

    {
      "aps": {
          "alert": {
              "title": "", 
              "body": “”
          },
          "badge": 1,
          "sound": "default",
          "mutable-content": true,
          "content-available": true,
          "category": "defaultCategory"
      },
      "image_url": ""
    }
    

    虽然当我尝试使用默认的APN流程时,我成功地做到了这一点,但我在使用FCM实现它时遇到了一个问题,问题是我没有收到密钥:

    mutable-content: 1
    category: defaultCategory
    

    我查了一下,找到了一个网址 FCM rich push notification payload for iOS 我也试过用上面提到的钥匙。

    "mutable_content": true,
    "click_action": defaultCategory,
    

    但即使使用这些,我也得不到正确的结果。最终更改后我收到的当前有效载荷为:

    {
        gcm.notification.category: defaultCategory, 
        image: /r/u/rustyredsmall.jpg, 
        type_id: XMH677878912-L-Blue, 
        type: Product, 
        aps: {
            alert =     {
               body = "new product notification message2018-05-24 00:00:00";
               title = "Product Notification";
            };
           badge = 1;
           sound = default;
        }, 
        0: {"mutable_content":true}, 
        gcm.message_id: 0:1527142412430945%98b85c5198b85c51
    }
    

    有什么建议,我怎样才能得到正确的有效载荷?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Ankit Jayaswal    6 年前

    我调试了问题并成功解决了问题,有一些 服务器端的密钥放置问题 . 我们在服务器端创建了负载,如下所示:

    {
        "to”: “xyz”,
        "mutable_content": true,
        "notification":
            {
                "body": “this is the message body.“,
                "title": “tiltle text”,
                "sound": "default",
                "badge": 1,
                "click_action": "defaultCategory"
            },
        "data":
            {
                "type": "Category",
                "typeId": "74",
                "redirect_title": "",
                "image_url": "\/d\/r\/dress_16.jpg",
                "notification_id": "1"
            }
    }
    

    FCM格式化了此负载,并将其以以下格式发送到移动端:

    {
        gcm.message_id: “0:1527233474081223%98b85c5198b85c51”, 
        aps: {
            alert: {
                body: "new product notification message2018-05-24 00:00:00";
                title: "Product Notification";
            };
            badge: 1;
            category: “defaultCategory”;
            mutable-content: 1;
            sound: “default”;
        }, 
        notification_id: 11, 
        typeId: “XMH677878912-L-Blue”, 
        image_url: “/r/u/rustyredsmall.jpg”, 
        type: “Product”, 
        redirect_title: “Midi Dress-L-Blue”
    }