使用Web Push发送消息,如何发送订阅

问题描述

我正在尝试遵循Google的Web推送教程:https://developers.google.com/web/fundamentals/push-notifications/sending-messages-with-web-push-libraries

我已将节点服务器设置为可以接收订阅。我不确定如何从客户端发送订阅请求?

本教程仅向您显示如何发出发布请求,但是您实际发送的有效负载到底是什么呢?服务器正在检查的端点是什么?这不在教程中。

function sendSubscriptionToBackEnd(subscription) {
  return fetch('/api/save-subscription/',{
    method: 'POST',headers: {
      'Content-Type': 'application/json'
    },body: JSON.stringify(subscription)
  })
  .then(function(response) {
    if (!response.ok) {
      throw new Error('Bad status code from server.');
    }

    return response.json();
  })
  .then(function(responseData) {
    if (!(responseData.data && responseData.data.success)) {
      throw new Error('Bad response from server.');
    }
  });
}

所以当我这样做

sendSubscriptionToBackEnd(subscription)

subscription应该是什么样子?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)