收到的JSON有效负载无效.未知名称click_action

Fra*_*nch 5 json push-notification service-worker progressive-web-apps firebase-cloud-messaging

所以我目前正在开发一个PWA.

我现在正在使用推送通知,我已经能够通过以下非常简单的JSON结构接收背景和前景通知.

{
  "message":{
    "token":"aValidToken",
    "notification": {
      "title": "New Content!",
      "body": "A new video has been uploaded."
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我还能够在其中添加包含其他信息的数据成员,并且仍然可以获得通知而不会带来任何不便.

现在的问题是,如果我想将另一个成员添加到JSON,例如click_action,我发布以下内容:

{
  "message":{
    "token":"aValidToken",
    "notification": {
      "title": "New Content!",
      "body": "A new video has been uploaded.",
      "click_action":"https://www.google.com.ar/"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

{
    "error": {
        "code": 400,
        "message": "Invalid JSON payload received. Unknown name \"click_action\" at 'message.notification': Cannot find field.",
        "status": "INVALID_ARGUMENT",
        "details": [
            {
                "@type": "type.googleapis.com/google.rpc.BadRequest",
                "fieldViolations": [
                    {
                        "field": "message.notification",
                        "description": "Invalid JSON payload received. Unknown name \"click_action\" at 'message.notification': Cannot find field."
                    }
                ]
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

几乎每一个其他成员都会发生这种情况:优先级,图标,声音,徽章等.

最后,我尝试在setBackgroundMessageHandler(它被调用)中使用硬编码图标click_action无效.没有图标,单击通知时没有任何反应.

messaging.setBackgroundMessageHandler( (notif) => {

  const notificationTitle = notif.notification.title;
  const notificationOptions = {
    body : notif.notification.body,
    icon : '/assets/icon/icon72x72.png',
    click_action : 'https://www.google.com.ar/'
  };

  return self.registration.showNotification(notificationTitle, notificationOptions);
});
Run Code Online (Sandbox Code Playgroud)

这纯粹是一个Ionic PWA项目,旨在在移动浏览器和桌面上运行.我会感谢你能给我的每一个提示!谢谢!

Mer*_*aya 10

您似乎正在使用新API:https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages

但是尝试使用旧版API中的字段:https: //firebase.google.com/docs/cloud-messaging/http-server-ref

您可以使用您正在使用的API定义图标,但您的有效负载需要为:

{
  "message": {
    "token": "aValidToken",
    "webpush": {
      "notification": {
        "title": "New Content!",
        "body": "A new video has been uploaded.",
        "icon": "your_icon"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到有关webpush通知字段的更多信息.

仅供参考,您提及的大多数其他字段(优先级,声音,徽章)在网络上都不支持任何API.

编辑(2018年5月10日):新API现在支持所有通知属性(优先级,图标,声音,徽章等).有关详细信息,请参阅本指