Firebase Cloud Messaging不以正确的格式发送iOS通知内容和服务扩展的aps负载

问题描述 投票:6回答:2

我正在尝试使用Firebase实施通知。当应用程序位于后台或前台时,会正确接收通知。所以,基本机制正在发挥作用。

现在我已将Content Extensions和Service Extensions添加到应用程序中。当我使用本地通知时,内容扩展程序可以正常工作,但只要考虑可选字段,Firebase消息有效内容就会显示不正确。这是我的控制台图像的链接:

以下是Firebase远程通知有效负载(为了匿名而编辑了一些长Google数据:

{
    aps = 
    {
        alert = 
        {
        body = "Eureka! 11";
        title = "Patient is not doing well";
        };
    };
    category = provider-body-panel;
    gcm.message_id = 0:149073;
    gcm.n.e = 1;
    google.c.a.c_id = 2825604;
    google.c.a.e = 1;
    google.c.a.ts = 149073;
    google.c.a.udt = 0;
    mutable-content = 1;
}

似乎“类别”和“可变内容”不在正确的位置。它们应该在aps有效载荷中。

如何让这些选项在有效负载中,以便我的应用程序可以正确解析它并将其与内容和服务扩展连接?

ios firebase apple-push-notifications firebase-cloud-messaging
2个回答
7
投票

首先,我要提一下FCM有两种类型的消息有效负载。 notificationdata。在这里查看documentation

通过Firebase Notifications Console发送通知时,它将被视为notification有效负载。但是,如果添加自定义数据,它会将其作为自定义键值对添加到有效内容中。

例如,在您的帖子中,FCM有效负载应如下所示:

{
    "notification": {
        "body" : "Eureka!",
        "title": "Patient is not doing well"
    },

    "data": {
        "category": "provider-body-panel",
        "mutable-content" : true,
        "click_action" : "provider-body-panel"
    }
}

怎么了?

  • click_action应该在notification内。
  • mutable-content应该是mutable_content(注意下划线)并且应该与notification处于同一水平。
  • (这个我可能会误解,但是)FCM没有category参数,click_action已经对应了它。

请参阅参数here的文档。

在使用Firebase Notifications Console时,目前无法设置click_actionmutable_content的值。您必须自己构建有效负载,如下所示:

{
    "to": "<REGISTRATION_TOKEN_HERE>",
    "mutable_content" : true,
    "notification": {
        "body" : "Eureka!",
        "title": "Patient is not doing well",
        "click_action" : "provider-body-panel"
    }
}

然后从您自己的App Server发送它。你也可以通过using PostmancURL来做到这一点


4
投票

“mutable-content应该是”mutable_content“(firebase服务器的关键字,作为IOS的可变内容发送)正如你在帖子中提到的那样,我认为你在编辑时遗漏了。

下面是一个示例,其中还有发送到FCM服务器的json中数据部分的更正格式。所以更新将是:

{ 
  "to" : "YOUR firebase messaging registration id here",
  "mutable_content":true,
  "notification": {
    "title": "Its about time",
     "body": "To go online Amigo",
     "click_action": "NotificationCategoryIdentifier ForYourNotificationActions"
  },
  "data":{
    "customKey":"custom data you want to appear in the message payload"
    "media-attachment":"mycustom image url",
    "catalogID":"mycustom catalog for my custom app"
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.