zu senden Ich verwende Ruby Version 1.8.7.Mit Ruby, um eine Benachrichtigung an Firebase
Ich verwende dieses FCM Juwel https://github.com/spacialdb/fcm und möchte eine Benachrichtigung an Android Client-App senden, aber es funktioniert nicht.
In Controller:
fcm = FCM.new(FIREBASE_API_KEY, :timeout => 30)
options = {:data => {:message => "This is a FCM Topic Message!"}}
response = fcm.send_to_topic('global', options)
Klasse 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
- Der Server-Schlüssel korrekt ist, weil ich Benachrichtigung erfolgreich von PHP-Code senden kann. { "message_id" => 8885803884270587181}
Könnte jemand bitte, was falsch mit dem Code hinweisen: wie unten
tun 'params.to_json' –
response = self.class.post ('/ send', params.merge (@client_options) .to_json) erzeugt Fehler Typeerror (nicht konvertieren String in Hash). – Jimy25
Ihr Code funktioniert. Es gibt keine Fehlermeldung. Ich weiß nicht, was du fragst. –