我正在使用Ruby版本1.8.7。使用Ruby将通知发送到Firebase
我使用此FCM宝石https://github.com/spacialdb/fcm并希望发送通知消息到Android客户端应用程序,但它不起作用。
在控制器:
fcm = FCM.new(FIREBASE_API_KEY, :timeout => 30)
options = {:data => {:message => "This is a FCM Topic Message!"}}
response = fcm.send_to_topic('global', options)
类FCM:
require 'httparty'
require 'cgi'
require 'json'
class FCM
include HTTParty
base_uri 'https://fcm.googleapis.com/fcm'
default_timeout 30
format :json
attr_accessor :timeout, :api_key
def initialize(api_key, client_options = {})
@api_key = api_key
@client_options = client_options
end
def send_with_notification_key(notification_key, options = {})
body = { :to => notification_key }.merge(options)
params = {
:body => body.to_json,
:headers => {
'Authorization' => "key=#{@api_key}",
'Content-Type' => 'application/json'
}
}
response = self.class.post('/send', params.merge(@client_options))
response.parsed_response
end
def send_to_topic(topic, options = {})
if topic =~ /[a-zA-Z0-9\-_.~%]+/
send_with_notification_key('/topics/' + topic, options)
end
end
end
- 服务器密钥是正确的,因为我可以通过PHP代码成功发送通知。
- 响应输出如下: { “MESSAGE_ID”=> 8885803884270587181}
任何人都可以请指出有什么不好的代码。 任何帮助将不胜感激。
do'params.to_json' –
response = self.class.post('/ send',params.merge(@client_options).to_json)产生错误TypeError(无法将String转换为散列)。 – Jimy25
您的代码有效。没有错误消息。我不知道你在问什么。 –