fcm_pusher 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/fcm_pusher.rb +56 -59
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f04c44335331f22b66e1877da968738a0ebe41a
|
4
|
+
data.tar.gz: d99ffe7ca4e3d7e4738ddb1af5f5345aff9d6bf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e5fd0d02f453d116b1d5a91a931b6aabd63ea0fe1ce40e2374bca1f284e32a8da7167255c4a1c31914eec9bf103a43f82782ac91edd8d4923a8bfa60aa6881d
|
7
|
+
data.tar.gz: c769301e985d2c8fd7df7f25acca8d4e1d00453ad4507213788d6eb87ee32be2bb117c76f4e737f7c23fbea79250900a4ca040ae6bf38bee7938b48efa69f878
|
data/lib/fcm_pusher.rb
CHANGED
@@ -1,93 +1,90 @@
|
|
1
|
-
# This class initializes a FcmPusher 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
|
|
5
|
-
require 'dotenv/load'
|
6
5
|
require 'json'
|
7
6
|
require 'net/https'
|
8
7
|
require 'uri'
|
9
8
|
|
10
9
|
class FcmPusher
|
11
|
-
module Priority
|
10
|
+
module Priority
|
12
11
|
HIGH = "high"
|
13
12
|
NORMAL = "normal"
|
14
|
-
end
|
13
|
+
end
|
15
14
|
|
16
15
|
module Configuration
|
17
16
|
FCM_BASE_URL = "https://fcm.googleapis.com/fcm/send"
|
18
|
-
FCM_API_KEY = "AAAABxYhiPE:APA91bECmJXkhoPKHFqlVA4-y79YABxVMSBUJGMLC35dQwQG-03zCqHmt72r6Pv4eiO42tHb0QyYPW6giAoo8E7KTtZU6IgwA1SH6oK02GNZUgVBI3GwGOrUH1Da8xEqA1d0LjbJrXek"
|
19
17
|
end
|
20
18
|
|
21
|
-
attr_accessor :fcm_token, :registration_ids, :priority
|
19
|
+
attr_accessor :fcm_api_key, :fcm_token, :registration_ids, :priority
|
22
20
|
|
23
|
-
def initialize
|
24
|
-
@
|
21
|
+
def initialize(fcm_api_key)
|
22
|
+
@fcm_api_key = fcm_api_key
|
23
|
+
@priority = FcmPusher::Priority::HIGH
|
25
24
|
end
|
26
25
|
|
27
26
|
# send notification to a unique registered token
|
28
27
|
# optional parameters are icon, badge, sound and priority
|
29
28
|
def send_once(to, title, body, options = {})
|
30
29
|
@fcm_token = to
|
31
|
-
set_priority(priority) if !options[:priority].nil?
|
32
30
|
body = parse_once(to, title, body, options)
|
33
|
-
request(Configuration::FCM_BASE_URL,
|
31
|
+
request(Configuration::FCM_BASE_URL, @fcm_api_key, body)
|
34
32
|
end
|
35
33
|
|
36
|
-
# send notifications to more than one registered token
|
34
|
+
# send notifications to more than one registered token
|
37
35
|
# optional parameters are icon, badge, sound and priority
|
38
36
|
def send_all(registration_ids, title, body, options = {})
|
39
37
|
@registration_ids = registration_ids
|
40
|
-
set_priority(priority) if !options[:priority].nil?
|
41
38
|
body = parse_all(registration_ids, title, body, options)
|
42
|
-
request(Configuration::FCM_BASE_URL,
|
39
|
+
request(Configuration::FCM_BASE_URL, @fcm_api_key, body)
|
43
40
|
end
|
44
41
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
42
|
+
private
|
43
|
+
def request(url, api_key, body)
|
44
|
+
uri = URI(url)
|
45
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
46
|
+
https.use_ssl = true
|
47
|
+
request = Net::HTTP::Post.new(uri.path, {'Content-Type': 'application/json', 'Authorization': "key=#{api_key}"})
|
48
|
+
request.body = body
|
49
|
+
response = https.request(request)
|
50
|
+
puts "Finished successfully with response #{response}"
|
51
|
+
rescue => exception
|
52
|
+
puts "Failed with #{exception}"
|
53
|
+
end
|
49
54
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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)
|
57
|
-
puts "Finished successfully with response #{response}"
|
58
|
-
rescue => exception
|
59
|
-
puts "Failed with #{exception}"
|
60
|
-
end
|
55
|
+
# parse json to be used in send_once
|
56
|
+
# optional parameters are icon, badge, sound and priority
|
57
|
+
def parse_once(to, title, body, options = {})
|
58
|
+
options[:priority] = @priority if options[:priority].nil?
|
61
59
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
priority: options[:priority]
|
75
|
-
}.to_json
|
76
|
-
end
|
60
|
+
{
|
61
|
+
to: to,
|
62
|
+
notification: {
|
63
|
+
title: title,
|
64
|
+
body: body,
|
65
|
+
icon: options[:icon],
|
66
|
+
badge: options[:badge],
|
67
|
+
sound: options[:sound]
|
68
|
+
},
|
69
|
+
priority: options[:priority]
|
70
|
+
}.to_json
|
71
|
+
end
|
77
72
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
73
|
+
# parse json to be used in send_all
|
74
|
+
# optional parameters are icon, badge, sound and priority
|
75
|
+
def parse_all(registration_ids, title, body, options = {})
|
76
|
+
options[:priority] = @priority if options[:priority].nil?
|
77
|
+
|
78
|
+
{
|
79
|
+
registration_ids: registration_ids,
|
80
|
+
notification: {
|
81
|
+
title: title,
|
82
|
+
body: body,
|
83
|
+
icon: options[:icon],
|
84
|
+
badge: options[:badge],
|
85
|
+
sound: options[:sound]
|
86
|
+
},
|
87
|
+
priority: options[:priority]
|
88
|
+
}.to_json
|
89
|
+
end
|
90
|
+
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.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Amarildo Lucas
|
@@ -9,21 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
date: 2017-09-23 00:00:00.000000000 Z
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: dotenv
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - '='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: 2.2.1
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - '='
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: 2.2.1
|
12
|
+
dependencies: []
|
27
13
|
description: A Ruby gem to send push notifications from Firebase Cloud Messaging to
|
28
14
|
Android and iOS devices.
|
29
15
|
email: vmarildo@gmail.com
|