首页 > 解决方案 > FCM API 错误请求

问题描述

我正在尝试通过 FCM 从我的 Ruby on Rails 项目中推送通知,这是我的代码:

require 'net/http'
require 'json'

def send_notifications
 log = Logger.new("log/notifications.log")
 begin
  uri = URI.parse("https://fcm.googleapis.com/fcm/send")
  http = Net::HTTP.new(uri.host, uri.port)

  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  key = '...'
  hash = {:notification => {:sound =>  "default", :title => "test", :text => "test", :badge => "0"}, :data => {:targetID => "1", :to => "..."}}

req = Net::HTTP::Post.new(uri.path, {'Content-Type' => 'application/json', 'Authorization' => "key=#{key}"})
req.body = hash.to_json
response = http.request(req)
log.debug("response #{response.body}")
rescue => e
  log.debug("failed #{e}")
 end
end

我收到 Bad Request 400 错误,调试响应正文只显示:“to”

请帮我调试这个问题。谢谢

标签: ruby-on-railsfirebasegoogle-apifirebase-cloud-messaginghttprequest

解决方案


推荐阅读