From 3b864ea45fd61e36c4e70878edae853bd79bfe8a Mon Sep 17 00:00:00 2001 From: Vadim Eremeev Date: Sun, 26 Sep 2021 01:55:18 +0700 Subject: [PATCH 1/3] add support of http v1 send request --- Gemfile | 1 + lib/fcm.rb | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 8a64880..f0e87c4 100644 --- a/Gemfile +++ b/Gemfile @@ -5,3 +5,4 @@ gem 'rake' gem 'rspec' gem 'webmock' gem 'ci_reporter_rspec' +gem 'googleauth' diff --git a/lib/fcm.rb b/lib/fcm.rb index c5d0a55..f0b07d2 100644 --- a/lib/fcm.rb +++ b/lib/fcm.rb @@ -1,9 +1,11 @@ require 'faraday' require 'cgi' require 'json' +require 'googleauth' class FCM BASE_URI = 'https://fcm.googleapis.com' + BASE_URI_V1 = 'https://fcm.googleapis.com/v1/projects/' DEFAULT_TIMEOUT = 30 FORMAT = :json @@ -12,13 +14,58 @@ class FCM INSTANCE_ID_API = 'https://iid.googleapis.com' TOPIC_REGEX = /[a-zA-Z0-9\-_.~%]+/ - attr_accessor :timeout, :api_key + attr_accessor :timeout, :api_key, :json_key_path, :project_base_uri - def initialize(api_key, client_options = {}) + def initialize(api_key, json_key_path = '', project_name = '', client_options = {}) @api_key = api_key @client_options = client_options + @json_key_path = json_key_path + @project_base_uri = BASE_URI_V1+project_name.to_s end + + # See https://firebase.google.com/docs/cloud-messaging/send-message for more details. + # { + # "token": "4sdsx", + # "notification": { + # "title": "Breaking News", + # "body": "New news story available." + # }, + # "data": { + # "story_id": "story_12345" + # }, + # "android": { + # "notification": { + # "click_action": "TOP_STORY_ACTIVITY", + # "body": "Check out the Top Story" + # } + # }, + # "apns": { + # "payload": { + # "aps": { + # "category" : "NEW_MESSAGE_CATEGORY" + # } + # } + # } + # } + # fcm = FCM.new(api_key, json_key_path, project_name) + # fcm.send( + # { "token": "4sdsx",, "to" : "notification": {}.. } + # ) + def send_notification_v1(message) + unless @project_base_uri.empty? + post_body = { 'message': message } + + response = Faraday.post("#{@project_base_uri}/messages:send") do |req| + req.headers['Content-Type'] = 'application/json' + req.headers['Authorization'] = "Bearer #{get_jwt_token}" + req.body = post_body.to_json + end + build_response(response) + end + end + alias send_v1 send_notification_v1 + # See https://developers.google.com/cloud-messaging/http for more details. # { "notification": { # "title": "Portugal vs. Denmark", @@ -265,4 +312,13 @@ def validate_condition_topics?(condition) topics = condition.scan(/(?:^|\S|\s)'([^']*?)'(?:$|\S|\s)/).flatten topics.all? { |topic| topic.gsub(TOPIC_REGEX, "").length == 0 } end + + def get_jwt_token + scope = 'https://www.googleapis.com/auth/firebase.messaging' + authorizer = Google::Auth::ServiceAccountCredentials.make_creds( + json_key_io: File.open(@json_key_path), + scope: scope) + token = authorizer.fetch_access_token! + token['access_token'] + end end From e7f8c3c699dd673da34d80ff743ced6af17470a8 Mon Sep 17 00:00:00 2001 From: Vadim Eremeev Date: Sun, 26 Sep 2021 02:23:10 +0700 Subject: [PATCH 2/3] code cleanup --- lib/fcm.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/fcm.rb b/lib/fcm.rb index f0b07d2..3a3a33e 100644 --- a/lib/fcm.rb +++ b/lib/fcm.rb @@ -20,11 +20,10 @@ def initialize(api_key, json_key_path = '', project_name = '', client_options = @api_key = api_key @client_options = client_options @json_key_path = json_key_path - @project_base_uri = BASE_URI_V1+project_name.to_s + @project_base_uri = BASE_URI_V1 + project_name.to_s end - - # See https://firebase.google.com/docs/cloud-messaging/send-message for more details. + # See https://firebase.google.com/docs/cloud-messaging/send-message # { # "token": "4sdsx", # "notification": { @@ -53,16 +52,16 @@ def initialize(api_key, json_key_path = '', project_name = '', client_options = # { "token": "4sdsx",, "to" : "notification": {}.. } # ) def send_notification_v1(message) - unless @project_base_uri.empty? - post_body = { 'message': message } + return if @project_base_uri.empty? - response = Faraday.post("#{@project_base_uri}/messages:send") do |req| - req.headers['Content-Type'] = 'application/json' - req.headers['Authorization'] = "Bearer #{get_jwt_token}" - req.body = post_body.to_json - end - build_response(response) + post_body = { 'message': message } + + response = Faraday.post("#{@project_base_uri}/messages:send") do |req| + req.headers['Content-Type'] = 'application/json' + req.headers['Authorization'] = "Bearer #{jwt_token}" + req.body = post_body.to_json end + build_response(response) end alias send_v1 send_notification_v1 @@ -313,11 +312,12 @@ def validate_condition_topics?(condition) topics.all? { |topic| topic.gsub(TOPIC_REGEX, "").length == 0 } end - def get_jwt_token + def jwt_token scope = 'https://www.googleapis.com/auth/firebase.messaging' authorizer = Google::Auth::ServiceAccountCredentials.make_creds( json_key_io: File.open(@json_key_path), - scope: scope) + scope: scope + ) token = authorizer.fetch_access_token! token['access_token'] end From 0f68496a0aeb12af55d8d4939d1e4719078ef500 Mon Sep 17 00:00:00 2001 From: Vadim Eremeev Date: Sun, 26 Sep 2021 02:42:54 +0700 Subject: [PATCH 3/3] update version, disable dependence --- fcm.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fcm.gemspec b/fcm.gemspec index 403dfa1..7a47cf0 100644 --- a/fcm.gemspec +++ b/fcm.gemspec @@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__) Gem::Specification.new do |s| s.name = "fcm" - s.version = "1.0.3" + s.version = "1.0.4" s.platform = Gem::Platform::RUBY s.authors = ["Kashif Rasul", "Shoaib Burq"] s.email = ["kashif@decision-labs.com", "shoaib@decision-labs.com"] @@ -19,5 +19,5 @@ Gem::Specification.new do |s| s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] - s.add_runtime_dependency('faraday', '~> 1') + # s.add_runtime_dependency('faraday', '~> 1') end