0

我正在使用 Rails 和https://github.com/zaru/webpush gem,并根据本指南 https://rossta.net/blog/web-push-notifications-from-rails.html进行了配置

我设法注册 ServiceWorker 并获得订阅。我把它这样保存在控制器中

endpoint = params[:subscription][:endpoint]
p256dh = params[:subscription][:keys][:p256dh]
auth = params[:subscription][:keys][:auth]

current_user.gcm_endpoint = endpoint
current_user.gcm_p256dh = p256dh
current_user.gcm_auth = auth

后来我发送这样的消息:

message = {
    title: "Title",
    body: "This is a test notification",
    icon: "images/my-128.png"
}

Webpush.payload_send(
    endpoint: current_user.gcm_endpoint,
    message: JSON.generate(message),
    p256dh: current_user.gcm_p256dh,
    auth: current_user.gcm_auth,
    ttl: 600,
    api_key: "<MY API KEY>"
)

我已经在 Google 控制台上配置并激活了帐户。我已打开 GCM API 并添加了正确的密钥。但无论我做什么,我都会收到这个 400 错误代码。而且我在 Google 控制台中没有统计信息 - 好像没有对我的 API 调用进行调用。

4

1 回答 1

0

问题出在钥匙本身。需要使用单独的 Firebase 控制面板生成它。在这里,有了那个键,我的代码运行良好。

于 2016-09-26T20:57:50.473 回答