fcm_pusher 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/fcm_pusher.rb +68 -19
  3. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4d4907b8aac2d5918043cdcb407bdb07ad1cb3f2
4
- data.tar.gz: 7db69837928a543f85dbd29f415bee88af6e5b87
3
+ metadata.gz: 5327ea046b784b0325bbccaee00aa607020bbe81
4
+ data.tar.gz: 5c737f21ae3edb37139ab18c44d3ba819d51336b
5
5
  SHA512:
6
- metadata.gz: 7f9ffdb0e52df59f05450adc7b36290527e2e607d64038ad4c3464893f02e05a7472431b38cd1ae533d5f193d6945d7da087713d69218b518e9cbd0579a778ef
7
- data.tar.gz: ac48bac74c84af3bbe536287d23cfdc64129785a8c68d77d264fc628b966bb79fececde8f363230fafba2191272861c023f92029a50662b599a9c8f291995464
6
+ metadata.gz: '08c066ac68726ce362a7734fcdb2dbb010c1104709e55a6db9adfe7503143a20505e31336af7808d1fcc6b5cc235198b2c582e712b011178bfc25ee9e32c5632'
7
+ data.tar.gz: 26768c0888a6565274fd10f2158640437ee5db97f603e7cfb56b21dfddc39878d15ac037b36f8166571da812f46b137218353df29537599b50d625d9aa77d5b6
data/lib/fcm_pusher.rb CHANGED
@@ -1,4 +1,4 @@
1
- # This class initializes a push notification object
1
+ # This class initializes a FcmPusher object
2
2
  # to be sended as a JSON POST request to Google FCM API Server
3
3
  # Use the FCM APY_KEY and the FCM_TOKEN from an user or device
4
4
 
@@ -7,38 +7,87 @@ require 'json'
7
7
  require 'net/https'
8
8
  require 'uri'
9
9
 
10
-
11
10
  class FcmPusher
12
- FCM_BASE_URL = "https://fcm.googleapis.com/fcm/send"
13
- FCM_API_KEY = ENV['FCM_API_KEY']
14
- attr_accessor :fcm_token
11
+ module Priority
12
+ HIGH = "high"
13
+ NORMAL = "normal"
14
+ end
15
15
 
16
- def initialize(fcm_token)
17
- @fcm_token = fcm_token
18
- end
16
+ module Configuration
17
+ FCM_BASE_URL = "https://fcm.googleapis.com/fcm/send"
18
+ FCM_API_KEY = "AAAABxYhiPE:APA91bECmJXkhoPKHFqlVA4-y79YABxVMSBUJGMLC35dQwQG-03zCqHmt72r6Pv4eiO42tHb0QyYPW6giAoo8E7KTtZU6IgwA1SH6oK02GNZUgVBI3GwGOrUH1Da8xEqA1d0LjbJrXek"
19
+ end
20
+
21
+ attr_accessor :fcm_token, :registration_ids, :priority
22
+
23
+ def initialize
24
+ @priority = Priority::HIGH
25
+ end
26
+
27
+ # send notification to a unique registered token
28
+ # optional parameters are icon, badge, sound and priority
29
+ def send_once(to, title, body, options = {})
30
+ @fcm_token = to
31
+ set_priority(priority) if !options[:priority].nil?
32
+ body = parse_once(to, title, body, options)
33
+ request(Configuration::FCM_BASE_URL, Configuration::FCM_API_KEY, body)
34
+ end
35
+
36
+ # send notifications to more than one registered token
37
+ # optional parameters are icon, badge, sound and priority
38
+ def send_all(registration_ids, title, body, options = {})
39
+ @registration_ids = registration_ids
40
+ set_priority(priority) if !options[:priority].nil?
41
+ body = parse_all(registration_ids, title, body, options)
42
+ request(Configuration::FCM_BASE_URL, Configuration::FCM_API_KEY, body)
43
+ end
19
44
 
20
- def send(title, body, icon, sound, badge, priority)
21
- uri = URI(FCM_BASE_URL)
45
+ # set the priority of the notification to be sended, the default value is Priority::HIGH
46
+ def set_priority(priority)
47
+ @priority = priority
48
+ end
49
+
50
+ def request(url, api_key, body)
51
+ uri = URI(url)
22
52
  https = Net::HTTP.new(uri.host, uri.port)
23
53
  https.use_ssl = true
24
- request = Net::HTTP::Post.new(uri.path, {'Content-Type': 'application/json', 'Authorization': "key=#{FCM_API_KEY}"})
25
- request.body = json_format(@fcm_token, title, body, icon, sound, badge, priority)
26
- response = https.request(request)
54
+ request = Net::HTTP::Post.new(uri.path, {'Content-Type': 'application/json', 'Authorization': "key=#{api_key}"})
55
+ request.body = body
56
+ response = https.request(request)
27
57
  puts "Finished successfully with response #{response}"
28
58
  rescue => exception
29
59
  puts "Failed with #{exception}"
30
60
  end
31
61
 
32
- def json_format(to, title, body, icon, sound, badge, priority)
33
- { to: to,
62
+ # parse json to be used in send_once
63
+ # optional parameters are icon, badge, sound and priority
64
+ def parse_once(to, title, body, options = {})
65
+ {
66
+ to: to,
34
67
  notification: {
35
68
  title: title,
36
69
  body: body,
37
- icon: icon,
38
- badge: badge,
39
- sound: sound
70
+ icon: options[:icon],
71
+ badge: options[:badge],
72
+ sound: options[:sound]
40
73
  },
41
- priority: priority
74
+ priority: options[:priority]
42
75
  }.to_json
43
76
  end
77
+
78
+ # parse json to be used in send_all
79
+ # optional parameters are icon, badge, sound and priority
80
+ def parse_all(registration_ids, title, body, options = {})
81
+ {
82
+ registration_ids: registration_ids,
83
+ notification: {
84
+ title: title,
85
+ body: body,
86
+ icon: options[:icon],
87
+ badge: options[:badge],
88
+ sound: options[:sound]
89
+ },
90
+ priority: options[:priority]
91
+ }.to_json
92
+ end
44
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fcm_pusher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amarildo Lucas
@@ -14,17 +14,18 @@ dependencies:
14
14
  name: dotenv
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 2.2.1
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
27
- description: Send push notifications using the Firebase Cloud Messaging Server
26
+ version: 2.2.1
27
+ description: A Ruby gem to send push notifications from Firebase Cloud Messaging to
28
+ Android and iOS devices.
28
29
  email: vmarildo@gmail.com
29
30
  executables: []
30
31
  extensions: []