接收到无效的JSON有效负载.未知名称click_action [英] Invalid JSON payload received. Unknown name click_action

查看:465
本文介绍了接收到无效的JSON有效负载.未知名称click_action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我目前正在开发PWA.

So I'm currently developing a PWA.

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

I'm working right now with Push notifications and I've already been able to recieve both background and foreground notifications with the following very simple JSON structure.

{
  "message":{
    "token":"aValidToken",
    "notification": {
      "title": "New Content!",
      "body": "A new video has been uploaded."
    }
  }
}

我还能够添加一个包含其他信息的数据成员,并且仍然能够收到通知,而不会带来任何不便.

I've also been able to add a data member with other information in it and still get the notification without inconveniences.

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

Now the problem is that if I want to add another member to the JSON, like for instance click_action, I post the following:

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

然后出现以下错误:

{
    "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."
                    }
                ]
            }
        ]
    }
}

几乎所有其他成员都在这方面发生这种情况,例如:优先级,图标,声音,徽章等

This is happening to me with almost every other member like: priority, icon, sound, badge, etc.

最后,我尝试在 setBackgroundMessageHandler (确实被调用)中对图标 click_action 进行硬编码无济于事.没有图标出现,单击通知时什么也没有发生.

Lastly, I've tried hardcoding an icon and click_action in the setBackgroundMessageHandler (which does get called) to no avail. No icon appears, nothing happens when you click the notification.

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);
});

这纯粹是一个 Ionic PWA 项目,旨在在移动浏览器和台式机上运行.我会很感激你能给我的每一个小费!谢谢!

This is purely an Ionic PWA project, meant to run on mobile browser and Desktop. I'll appreciate every tip you could give me! Thanks!

推荐答案

好像您正在使用新的API:

Looks like you're using the new API: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages

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

But trying to use fields from the legacy API: https://firebase.google.com/docs/cloud-messaging/http-server-ref

您可以使用正在使用的API定义图标,但是有效载荷必须为:

You can define an icon with the API you're using, but your payload needs to be:

{
  "message": {
    "token": "aValidToken",
    "webpush": {
      "notification": {
        "title": "New Content!",
        "body": "A new video has been uploaded.",
        "icon": "your_icon"
      }
    }
  }
}

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

仅供参考,您提到的大多数其他字段(优先级,声音,徽标)尚不在具有两种API的网络上均受支持.

FYI, most of the other fields you mentioned (priority, sound, badge) are not yet supported on web with either API.

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

Edit (10th May 2018): All notification properties (priority, icon, sound, badge, etc.) are now supported with the new API. See this guide for details.

这篇关于接收到无效的JSON有效负载.未知名称click_action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆