我正在使用 Ruby 版本 1.8.7。
我使用这个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}
请问有人能指出代码出了什么问题吗? 任何帮助都将不胜感激。
FCM
类是FCM gem的一部分。很少有可能出现问题。 - Daniel Rikowski