Skip to content

Commit

Permalink
Merge pull request #91 from vadimeremeev/master
Browse files Browse the repository at this point in the history
add support of http v1 send request
  • Loading branch information
sabman authored Nov 25, 2021
2 parents ea640ad + 0f68496 commit 7691238
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ gem 'rake'
gem 'rspec'
gem 'webmock'
gem 'ci_reporter_rspec'
gem 'googleauth'
4 changes: 2 additions & 2 deletions fcm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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
60 changes: 58 additions & 2 deletions lib/fcm.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,13 +14,57 @@ 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
# {
# "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)
return if @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 #{jwt_token}"
req.body = post_body.to_json
end
build_response(response)
end
alias send_v1 send_notification_v1

# See https://developers.google.com/cloud-messaging/http for more details.
# { "notification": {
# "title": "Portugal vs. Denmark",
Expand Down Expand Up @@ -265,4 +311,14 @@ def validate_condition_topics?(condition)
topics = condition.scan(/(?:^|\S|\s)'([^']*?)'(?:$|\S|\s)/).flatten
topics.all? { |topic| topic.gsub(TOPIC_REGEX, "").length == 0 }
end

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
)
token = authorizer.fetch_access_token!
token['access_token']
end
end

0 comments on commit 7691238

Please sign in to comment.