hey-you-fcm-push 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.byebug_history +50 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +12 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +89 -0
- data/Makefile +3 -0
- data/README.md +81 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/hey-you-fcm-push.gemspec +37 -0
- data/lib/hey-you-fcm-push.rb +11 -0
- data/lib/hey_you/builder/fcm_push.rb +34 -0
- data/lib/hey_you/channels/fcm_push.rb +49 -0
- data/lib/hey_you/config/fcm_push.rb +21 -0
- data/lib/hey_you_fcm_push/connection.rb +61 -0
- data/lib/hey_you_fcm_push/message_object.rb +54 -0
- data/lib/hey_you_fcm_push/message_object/android_config.rb +57 -0
- data/lib/hey_you_fcm_push/message_object/apns_config.rb +21 -0
- data/lib/hey_you_fcm_push/message_object/fcm_options.rb +15 -0
- data/lib/hey_you_fcm_push/message_object/notification.rb +16 -0
- data/lib/hey_you_fcm_push/message_object/web_push_config.rb +26 -0
- data/lib/hey_you_fcm_push/version.rb +3 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7bffeca01ef07a68e7e23b6a8bd60b685223c00e33db1f6e3b153e509841fb8c
|
4
|
+
data.tar.gz: 562002a2673a27b9711f531b8ea3af2211b8f45fafaff5d23a9c1099e474d168
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 831603025086b5b21fe8ee5d527394b59b8c9acc78ed132b718eb631ff501c61e9d086d815fd7914da17233eb6cdd187bda687350a031ffa13dafcf2a9675d48
|
7
|
+
data.tar.gz: 87adaa77047e75ff3ddee26c5e08abf9a681c9322ccaf49055d293323c12b823e166fe5395a13ec89aa02c09e6798050d9c9a79fec0bcb52c685caa26608b796
|
data/.byebug_history
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
continue
|
2
|
+
described_class.instance
|
3
|
+
continue
|
4
|
+
described_class.instance
|
5
|
+
stub_object.counter
|
6
|
+
continue
|
7
|
+
reauth_token
|
8
|
+
token
|
9
|
+
continue
|
10
|
+
reauth_token
|
11
|
+
token
|
12
|
+
continue
|
13
|
+
@reauth_token
|
14
|
+
@token
|
15
|
+
continue
|
16
|
+
@token
|
17
|
+
continue
|
18
|
+
@reauth_token
|
19
|
+
@token
|
20
|
+
continue
|
21
|
+
reauth_token
|
22
|
+
token
|
23
|
+
continue
|
24
|
+
reauth_token
|
25
|
+
token
|
26
|
+
HeyYouFcmPush::Connection.instance
|
27
|
+
HeyYouFcmPush::Connection
|
28
|
+
next
|
29
|
+
reauth_token
|
30
|
+
token
|
31
|
+
reauth_token
|
32
|
+
token
|
33
|
+
continue
|
34
|
+
builder.fcm_push.webpush
|
35
|
+
builder.fcm_push.android
|
36
|
+
builder.fcm_push.notification
|
37
|
+
continue
|
38
|
+
builder.fcm_push.notification
|
39
|
+
builder.fcm_push
|
40
|
+
builder
|
41
|
+
continue
|
42
|
+
ch_data.fetch(:topic, nil)
|
43
|
+
ch_data
|
44
|
+
continue
|
45
|
+
builder.fcm_push.topic
|
46
|
+
builder.fcm_push
|
47
|
+
builder
|
48
|
+
continue
|
49
|
+
builder.fcm_push
|
50
|
+
builder
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
hey-you-fcm-push (0.1.0)
|
5
|
+
googleauth (~> 0.10.0)
|
6
|
+
hey-you (~> 1.4)
|
7
|
+
httparty (~> 0.18.0)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: https://rubygems.org/
|
11
|
+
specs:
|
12
|
+
addressable (2.7.0)
|
13
|
+
public_suffix (>= 2.0.2, < 5.0)
|
14
|
+
byebug (11.1.3)
|
15
|
+
concurrent-ruby (1.1.7)
|
16
|
+
crack (0.4.4)
|
17
|
+
diff-lcs (1.4.4)
|
18
|
+
faraday (0.17.3)
|
19
|
+
multipart-post (>= 1.2, < 3)
|
20
|
+
fcm (0.0.6)
|
21
|
+
httparty (~> 0.10, >= 0.10.0)
|
22
|
+
ffaker (2.17.0)
|
23
|
+
googleauth (0.10.0)
|
24
|
+
faraday (~> 0.12)
|
25
|
+
jwt (>= 1.4, < 3.0)
|
26
|
+
memoist (~> 0.16)
|
27
|
+
multi_json (~> 1.11)
|
28
|
+
os (>= 0.9, < 2.0)
|
29
|
+
signet (~> 0.12)
|
30
|
+
hashdiff (1.0.1)
|
31
|
+
hey-you (1.4.0)
|
32
|
+
fcm (~> 0.0.2)
|
33
|
+
i18n (~> 1.0)
|
34
|
+
mail (~> 2.7)
|
35
|
+
httparty (0.18.1)
|
36
|
+
mime-types (~> 3.0)
|
37
|
+
multi_xml (>= 0.5.2)
|
38
|
+
i18n (1.8.5)
|
39
|
+
concurrent-ruby (~> 1.0)
|
40
|
+
jwt (2.2.2)
|
41
|
+
mail (2.7.1)
|
42
|
+
mini_mime (>= 0.1.1)
|
43
|
+
memoist (0.16.2)
|
44
|
+
mime-types (3.3.1)
|
45
|
+
mime-types-data (~> 3.2015)
|
46
|
+
mime-types-data (3.2020.1104)
|
47
|
+
mini_mime (1.0.2)
|
48
|
+
multi_json (1.15.0)
|
49
|
+
multi_xml (0.6.0)
|
50
|
+
multipart-post (2.1.1)
|
51
|
+
os (1.1.1)
|
52
|
+
public_suffix (4.0.6)
|
53
|
+
rake (13.0.3)
|
54
|
+
rspec (3.10.0)
|
55
|
+
rspec-core (~> 3.10.0)
|
56
|
+
rspec-expectations (~> 3.10.0)
|
57
|
+
rspec-mocks (~> 3.10.0)
|
58
|
+
rspec-core (3.10.0)
|
59
|
+
rspec-support (~> 3.10.0)
|
60
|
+
rspec-expectations (3.10.0)
|
61
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
62
|
+
rspec-support (~> 3.10.0)
|
63
|
+
rspec-mocks (3.10.0)
|
64
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
65
|
+
rspec-support (~> 3.10.0)
|
66
|
+
rspec-support (3.10.0)
|
67
|
+
signet (0.14.0)
|
68
|
+
addressable (~> 2.3)
|
69
|
+
faraday (>= 0.17.3, < 2.0)
|
70
|
+
jwt (>= 1.5, < 3.0)
|
71
|
+
multi_json (~> 1.10)
|
72
|
+
webmock (3.11.0)
|
73
|
+
addressable (>= 2.3.6)
|
74
|
+
crack (>= 0.3.2)
|
75
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
76
|
+
|
77
|
+
PLATFORMS
|
78
|
+
ruby
|
79
|
+
|
80
|
+
DEPENDENCIES
|
81
|
+
byebug
|
82
|
+
ffaker (~> 2.15)
|
83
|
+
hey-you-fcm-push!
|
84
|
+
rake (~> 13.0)
|
85
|
+
rspec (~> 3.0)
|
86
|
+
webmock (~> 3.4)
|
87
|
+
|
88
|
+
BUNDLED WITH
|
89
|
+
2.1.4
|
data/Makefile
ADDED
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Hey, You, FCM!
|
2
|
+
[![Build Status](https://travis-ci.com/QNester/hey-you-fcm-push.svg?branch=master)](https://travis-ci.com/QNester/hey-you-fcm-push#)[![Gem Version](https://badge.fury.io/rb/hey-you-fcm-push.svg)](https://badge.fury.io/rb/hey-you-fcm-push)
|
3
|
+
|
4
|
+
Send fcm pushes via [hey-you gem](https://github.com/QNester/hey-you) using [google fcm protocol](https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages/send)
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'hey-you-fcm-push'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install hey-you-fcm-push
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
After load gem you can send FCM pushes via [hey-you](https://github.com/QNester/hey-you).
|
25
|
+
|
26
|
+
For example:
|
27
|
+
```yaml
|
28
|
+
# config/notifications.yml
|
29
|
+
events:
|
30
|
+
say_hello:
|
31
|
+
# ...
|
32
|
+
fcm_push:
|
33
|
+
topic: 'users_topic'
|
34
|
+
notification:
|
35
|
+
title: 'Hey!'
|
36
|
+
body: 'Hey, %{name}!'
|
37
|
+
android:
|
38
|
+
notification:
|
39
|
+
title: 'Hey, Android'
|
40
|
+
body: 'Hey, %{name}!'
|
41
|
+
webpush:
|
42
|
+
notification:
|
43
|
+
title: 'Hey, Webpush'
|
44
|
+
body: 'Hey, %{name}!'
|
45
|
+
apns:
|
46
|
+
payload:
|
47
|
+
hello: 'world'
|
48
|
+
fcm_options:
|
49
|
+
analytics_label: label
|
50
|
+
push_data:
|
51
|
+
hello: 'world'
|
52
|
+
```
|
53
|
+
*More about format: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages*
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
# config/initalizers/hey-you.rb
|
57
|
+
HeyYou::Config.configure do
|
58
|
+
# ... base hey-you configuration ...
|
59
|
+
|
60
|
+
# FCM project_id
|
61
|
+
config.fcm_push.project_id = 'my-project-12345'
|
62
|
+
|
63
|
+
# Google FCM server credentials file
|
64
|
+
config.fcm_push.credentials_file = '/path/to/credentials.json'
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
# // somewhere in your app
|
70
|
+
builder = HeyYou::Builder.new('events.say_hello', name: "Jonny")
|
71
|
+
HeyYou::Channels::FcmPush.send!(builder, token: 'token') #=> { message_id: 'message_id' }
|
72
|
+
```
|
73
|
+
|
74
|
+
## Development
|
75
|
+
|
76
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
|
77
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
78
|
+
|
79
|
+
To run tests execute `make test`.
|
80
|
+
|
81
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "hey_you_fcm_push"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require_relative 'lib/hey_you_fcm_push/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "hey-you-fcm-push"
|
5
|
+
spec.version = HeyYouFcmPush::VERSION
|
6
|
+
spec.authors = ["Sergey Nesterov"]
|
7
|
+
spec.email = ["qnesterr@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Send fcm pushes via hey-you}
|
10
|
+
spec.description = %q{Send fcm pushes via hey-you using google fcm protocol}
|
11
|
+
spec.homepage = "https://github.com/QNester/hey-you-fcm-push"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
|
13
|
+
|
14
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/QNester/hey-you-fcm-push.git"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/QNester/hey-you-fcm-push/CHANGELOG.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_runtime_dependency "hey-you", '~> 1.4'
|
30
|
+
spec.add_runtime_dependency "googleauth", '~> 0.10.0'
|
31
|
+
spec.add_runtime_dependency "httparty", '~> 0.18.0'
|
32
|
+
|
33
|
+
spec.add_development_dependency "rake", "~> 13.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
spec.add_development_dependency "webmock", '~> 3.4'
|
36
|
+
spec.add_development_dependency "ffaker", '~> 2.15'
|
37
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'hey-you'
|
2
|
+
require_relative 'hey_you_fcm_push/version'
|
3
|
+
require_relative 'hey_you/config/fcm_push'
|
4
|
+
require_relative 'hey_you/builder/fcm_push'
|
5
|
+
require_relative 'hey_you/channels/fcm_push'
|
6
|
+
|
7
|
+
module HeyYouFcmPush
|
8
|
+
CHANNEL_NAME = 'fcm_push'.freeze
|
9
|
+
|
10
|
+
HeyYou::Config.instance.registrate_channel(CHANNEL_NAME)
|
11
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'hey_you_fcm_push/message_object'
|
2
|
+
|
3
|
+
module HeyYou
|
4
|
+
class Builder
|
5
|
+
class FcmPush < Base
|
6
|
+
attr_reader :topic, :condition, :name, :notification, :android, :webpush, :apns, :fcm_options, :push_data
|
7
|
+
|
8
|
+
NOTIFICATION_KEYS = HeyYouFcmPush::MessageObject::Notification::TRANSLATED_KEYS.map(&:to_s).freeze
|
9
|
+
|
10
|
+
def build
|
11
|
+
@topic = ch_data.fetch('topic', nil)
|
12
|
+
@condition = ch_data.fetch('condition', nil)
|
13
|
+
@name = ch_data.fetch('name', nil)
|
14
|
+
|
15
|
+
@notification = interpolate_notification(ch_data.fetch('notification', nil))
|
16
|
+
|
17
|
+
@android = interpolate_notification(ch_data.fetch('android', nil))
|
18
|
+
@webpush = interpolate_notification(ch_data.fetch('webpush', nil))
|
19
|
+
@apns = interpolate_notification(ch_data.fetch('apns', nil))
|
20
|
+
@fcm_options = interpolate_notification(ch_data.fetch('fcm_options', nil))
|
21
|
+
@push_data = ch_data.fetch('push_data', nil)
|
22
|
+
end
|
23
|
+
|
24
|
+
def interpolate_notification(data)
|
25
|
+
return unless data
|
26
|
+
|
27
|
+
interpolate_notification(data['notification']) if data['notification']
|
28
|
+
NOTIFICATION_KEYS.each { |k| data[k] = interpolate(data[k], options) if data[k] }
|
29
|
+
|
30
|
+
data
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'hey_you_fcm_push/connection'
|
2
|
+
require 'hey_you_fcm_push/message_object'
|
3
|
+
|
4
|
+
module HeyYou
|
5
|
+
module Channels
|
6
|
+
class FcmPush < Base
|
7
|
+
class << self
|
8
|
+
# Sending fcm message
|
9
|
+
#
|
10
|
+
# @param [HeyYou::Builder] builder - builder with notifications texts and settings
|
11
|
+
# @option [String] name - Output Only. The identifier of the message sent
|
12
|
+
# @option [String] token - Registration token to send a message to.
|
13
|
+
# @option [String] topic - Topic name to send a message to, e.g. "weather".
|
14
|
+
# @option [String] condition - Condition to send a message to, e.g. "'foo' in topics && 'bar' in topics".
|
15
|
+
# @option [Hash] notification - Basic notification template to use across all platforms.
|
16
|
+
# @option [Hash] android - Android specific options for messages sent through FCM connection server.
|
17
|
+
# @option [Hash] webpush - Webpush protocol options.
|
18
|
+
# @option [Hash] apns - Apple Push Notification Service specific options.
|
19
|
+
# @option [Hash] fcm_options - Platform independent options for features provided by the FCM SDKs.
|
20
|
+
# @option [Hash] push_data - Input only. Arbitrary key/value payload. The key should not be a reserved word ("from", "message_type", or any word starting with "google" or "gcm").
|
21
|
+
#
|
22
|
+
# @return [Hash] - FCM response
|
23
|
+
#
|
24
|
+
# @see: https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message
|
25
|
+
def send!(builder, **options)
|
26
|
+
message = build_message(builder, **options)
|
27
|
+
HeyYouFcmPush::Connection.instance.send_notification(message, validate_only: options[:validate_only])
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def build_message(builder, **options)
|
33
|
+
HeyYouFcmPush::MessageObject.new(
|
34
|
+
token: options[:token],
|
35
|
+
topic: options[:topic] || builder.fcm_push.topic,
|
36
|
+
condition: options[:condition] || builder.fcm_push.condition,
|
37
|
+
name: options[:name] || builder.fcm_push.name,
|
38
|
+
notification: options[:notification] || builder.fcm_push.notification,
|
39
|
+
android: options[:android] || builder.fcm_push.android,
|
40
|
+
webpush: options[:webpush] || builder.fcm_push.webpush,
|
41
|
+
apns: options[:apns] || builder.fcm_push.apns,
|
42
|
+
fcm_options: options[:fcm_options]|| builder.fcm_push.fcm_options,
|
43
|
+
data: options[:push_data] || builder.fcm_push.push_data
|
44
|
+
).to_h
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class HeyYou::Config::FcmPush
|
2
|
+
extend HeyYou::Config::Configurable
|
3
|
+
|
4
|
+
attr_accessor :credentials_file, :project_id
|
5
|
+
|
6
|
+
REQUIRED_CONFIGS = %i[credentials_file project_id]
|
7
|
+
|
8
|
+
def credentials_file=(filepath)
|
9
|
+
@credentials_file = filepath
|
10
|
+
ENV[Google::Auth::CredentialsLoader::ENV_VAR] = credentials_file
|
11
|
+
end
|
12
|
+
|
13
|
+
def validate_config
|
14
|
+
return if REQUIRED_CONFIGS.all? { |conf| send(conf) }
|
15
|
+
|
16
|
+
raise(
|
17
|
+
HeyYou::Config::Configurable::RequiredConfigsNotPassed,
|
18
|
+
"You must pass all required config for channel `fcm_push`: #{REQUIRED_CONFIGS}"
|
19
|
+
)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'googleauth'
|
2
|
+
require 'hey_you/config/fcm_push'
|
3
|
+
|
4
|
+
module HeyYouFcmPush
|
5
|
+
class Connection
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
URL_TMPL = "https://fcm.googleapis.com/v1/projects/%{project_id}/messages:send".freeze
|
9
|
+
AUTH_SCOPES = ['https://www.googleapis.com/auth/firebase.messaging'].freeze
|
10
|
+
RETRY_COUNT = 1
|
11
|
+
HTTP_CODES = {
|
12
|
+
not_auth: 401,
|
13
|
+
success: 200
|
14
|
+
}
|
15
|
+
|
16
|
+
attr_reader :authorization, :options, :project_id, :uri
|
17
|
+
|
18
|
+
def initialize
|
19
|
+
@authorization = Google::Auth.get_application_default(AUTH_SCOPES)
|
20
|
+
@options = { headers: { 'Content-Type': 'application/json' } }
|
21
|
+
@project_id = HeyYou::Config::FcmPush.instance.project_id
|
22
|
+
@uri = URL_TMPL % { project_id: project_id }
|
23
|
+
|
24
|
+
reauth
|
25
|
+
end
|
26
|
+
|
27
|
+
def send_notification(message, validate_only: false, retry_count: 0)
|
28
|
+
request_options = options.clone
|
29
|
+
request_options[:body] = { message: message, validate_only: validate_only }.to_json
|
30
|
+
|
31
|
+
make_request(request_options)
|
32
|
+
rescue AuthError => e
|
33
|
+
retry_count += 1
|
34
|
+
raise e if retry_count > RETRY_COUNT
|
35
|
+
|
36
|
+
reauth
|
37
|
+
return send_notification(message, validate_only: validate_only, retry_count: retry_count + 1)
|
38
|
+
end
|
39
|
+
|
40
|
+
def reauth
|
41
|
+
authorization.apply!(options[:headers])
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def make_request(request_options)
|
47
|
+
response = HTTParty.post(uri, request_options)
|
48
|
+
process_response(response)
|
49
|
+
end
|
50
|
+
|
51
|
+
def process_response(response)
|
52
|
+
raise AuthError if response.code == HTTP_CODES[:not_auth]
|
53
|
+
return response.to_hash if response.code == HTTP_CODES[:success]
|
54
|
+
|
55
|
+
raise ResponseError, "response code: #{response.code}"
|
56
|
+
end
|
57
|
+
|
58
|
+
class AuthError < StandardError; end
|
59
|
+
class ResponseError < StandardError; end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative 'message_object/android_config'
|
2
|
+
require_relative 'message_object/apns_config'
|
3
|
+
require_relative 'message_object/fcm_options'
|
4
|
+
require_relative 'message_object/notification'
|
5
|
+
require_relative 'message_object/web_push_config'
|
6
|
+
|
7
|
+
module HeyYouFcmPush
|
8
|
+
class MessageObject
|
9
|
+
attr_reader :name, :receiver_hash, :android_config, :apns_config, :fcm_options,
|
10
|
+
:notification, :push_data, :web_push_config
|
11
|
+
|
12
|
+
# @option [String] name - Output Only. The identifier of the message sent
|
13
|
+
# @option [String] token - Registration token to send a message to.
|
14
|
+
# @option [String] topic - Topic name to send a message to, e.g. "weather".
|
15
|
+
# @option [String] condition - Condition to send a message to, e.g. "'foo' in topics && 'bar' in topics".
|
16
|
+
# @option [Hash] notification - Basic notification template to use across all platforms.
|
17
|
+
# @option [Hash] android - Android specific options for messages sent through FCM connection server.
|
18
|
+
# @option [Hash] webpush - Webpush protocol options.
|
19
|
+
# @option [Hash] apns - Apple Push Notification Service specific options.
|
20
|
+
# @option [Hash] fcm_options - Platform independent options for features provided by the FCM SDKs.
|
21
|
+
# @option [Hash] data - Input only. Arbitrary key/value payload. The key should not be a reserved word ("from", "message_type", or any word starting with "google" or "gcm").
|
22
|
+
#
|
23
|
+
# @see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#resource:-message
|
24
|
+
def initialize(name: nil, token: nil, topic: nil, condition: nil, **options)
|
25
|
+
@receiver_hash = { token: token } if token
|
26
|
+
@receiver_hash = { topic: topic } if topic
|
27
|
+
@receiver_hash = { condition: condition } if condition
|
28
|
+
|
29
|
+
raise PushReceiverError, "You should pass `token` or `topic` or `condition`" unless receiver_hash
|
30
|
+
|
31
|
+
@name = name
|
32
|
+
@notification = Notification.new(**options[:notification].transform_keys(&:to_sym)) if options[:notification]
|
33
|
+
@android_config = AndroidConfig.new(options[:android].transform_keys(&:to_sym)) if options[:android_config]
|
34
|
+
@web_push_config = WebPushConfig.new(options[:webpush].transform_keys(&:to_sym)) if options[:web_push_config]
|
35
|
+
@apns_config = ApnsConfig.new(options[:apns].transform_keys(&:to_sym)) if options[:apns_config]
|
36
|
+
@fcm_options = FcmOptions.new(**options[:fcm_options].transform_keys(&:to_sym)) if options[:fcm_options]
|
37
|
+
@push_data = options[:data]
|
38
|
+
end
|
39
|
+
|
40
|
+
def to_h
|
41
|
+
{
|
42
|
+
name: name,
|
43
|
+
data: push_data,
|
44
|
+
notification: notification&.to_h,
|
45
|
+
android_config: android_config&.to_h,
|
46
|
+
web_push_config: web_push_config&.to_h,
|
47
|
+
apns_config: apns_config&.to_h,
|
48
|
+
fcm_options: fcm_options&.to_h
|
49
|
+
}.merge(receiver_hash).compact
|
50
|
+
end
|
51
|
+
|
52
|
+
class PushReceiverError < StandardError; end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module HeyYouFcmPush
|
2
|
+
class MessageObject
|
3
|
+
class AndroidConfig
|
4
|
+
attr_reader :to_h
|
5
|
+
|
6
|
+
def initialize(**options)
|
7
|
+
options[:fcm_options] = options[:fcm_options]&.transform_keys(&:to_sym)
|
8
|
+
options[:notification] = options[:notification]&.transform_keys(&:to_sym)
|
9
|
+
|
10
|
+
@to_h = {
|
11
|
+
collapse_key: options[:collapse_key],
|
12
|
+
priority: options[:priority],
|
13
|
+
ttl: options[:ttl],
|
14
|
+
restricted_package_name: options[:restricted_package_name],
|
15
|
+
data: options[:data],
|
16
|
+
notification: build_notification(options[:notification]),
|
17
|
+
fcm_options: (
|
18
|
+
FcmOptions.new(analytics_label: options[:fcm_options][:analytics_label]).to_h if options[:fcm_options]
|
19
|
+
),
|
20
|
+
direct_boot_ok: options[:direct_boot_ok]
|
21
|
+
}.compact
|
22
|
+
end
|
23
|
+
|
24
|
+
def build_notification(notification)
|
25
|
+
return if !notification || notification == {}
|
26
|
+
|
27
|
+
{
|
28
|
+
title: notification[:title],
|
29
|
+
body: notification[:body],
|
30
|
+
icon: notification[:icon],
|
31
|
+
color: notification[:color],
|
32
|
+
sound: notification[:sound],
|
33
|
+
tag: notification[:tag],
|
34
|
+
click_action: notification[:click_action],
|
35
|
+
body_loc_key: notification[:body_loc_key],
|
36
|
+
body_loc_args: notification[:body_loc_args],
|
37
|
+
title_loc_key: notification[:title_loc_key],
|
38
|
+
title_loc_args: notification[:title_loc_args],
|
39
|
+
channel_id: notification[:channel_id],
|
40
|
+
ticker: notification[:ticker],
|
41
|
+
sticky: notification[:sticky],
|
42
|
+
event_time: notification[:event_time],
|
43
|
+
local_only: notification[:local_only],
|
44
|
+
notification_priority: notification[:notification_priority],
|
45
|
+
default_sound: notification[:default_sound],
|
46
|
+
default_vibrate_timings: notification[:default_vibrate_timings],
|
47
|
+
default_light_settings: notification[:default_light_settings],
|
48
|
+
vibrate_timings: notification[:vibrate_timings],
|
49
|
+
visibility: notification[:visibility],
|
50
|
+
notification_count: notification[:notification_count],
|
51
|
+
light_settings: notification[:light_settings],
|
52
|
+
image: notification[:image]
|
53
|
+
}.compact
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module HeyYouFcmPush
|
2
|
+
class MessageObject
|
3
|
+
class ApnsConfig
|
4
|
+
attr_reader :to_h
|
5
|
+
|
6
|
+
def initialize(headers: nil, payload: nil, fcm_options: nil)
|
7
|
+
fcm_options = fcm_options&.transform_keys(&:to_sym)
|
8
|
+
|
9
|
+
hash = { headers: headers, payload: payload }
|
10
|
+
|
11
|
+
if fcm_options
|
12
|
+
hash.merge!(
|
13
|
+
fcm_options: FcmOptions.new(analytics_label: fcm_options[:analytics_label], image: fcm_options[:image]).to_h
|
14
|
+
)
|
15
|
+
end
|
16
|
+
|
17
|
+
@to_h = hash
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module HeyYouFcmPush
|
2
|
+
class MessageObject
|
3
|
+
class FcmOptions
|
4
|
+
attr_reader :to_h
|
5
|
+
|
6
|
+
def initialize(analytics_label:, link: nil, image: nil)
|
7
|
+
@to_h = {
|
8
|
+
analytics_label: analytics_label,
|
9
|
+
link: link,
|
10
|
+
image: image
|
11
|
+
}.compact
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module HeyYouFcmPush
|
2
|
+
class MessageObject
|
3
|
+
class Notification
|
4
|
+
TRANSLATED_KEYS = %w[title body]
|
5
|
+
|
6
|
+
attr_reader :to_h
|
7
|
+
def initialize(title:, body:, image: nil)
|
8
|
+
@to_h = {
|
9
|
+
title: title,
|
10
|
+
body: body,
|
11
|
+
image: image
|
12
|
+
}.compact
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module HeyYouFcmPush
|
2
|
+
class MessageObject
|
3
|
+
class WebPushConfig
|
4
|
+
attr_reader :to_h
|
5
|
+
|
6
|
+
def initialize(headers: nil, data: nil, notification: nil, fcm_options: nil)
|
7
|
+
notification = notification&.transform_keys(&:to_sym)
|
8
|
+
fcm_options = fcm_options&.transform_keys(&:to_sym)
|
9
|
+
|
10
|
+
hash = {
|
11
|
+
headers: headers,
|
12
|
+
data: data,
|
13
|
+
notification: (Notification.new(**notification).to_h if notification),
|
14
|
+
}
|
15
|
+
|
16
|
+
if fcm_options
|
17
|
+
hash.merge(
|
18
|
+
fcm_options: FcmOptions.new(analytics_label: fcm_options[:analytics_label], link: fcm_options[:link]).to_h
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
@to_h = hash.compact
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hey-you-fcm-push
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Nesterov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-12-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: hey-you
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.4'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: googleauth
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.10.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.10.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: httparty
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.18.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.18.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.4'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.4'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: ffaker
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '2.15'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '2.15'
|
111
|
+
description: Send fcm pushes via hey-you using google fcm protocol
|
112
|
+
email:
|
113
|
+
- qnesterr@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".byebug_history"
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- CHANGELOG.md
|
123
|
+
- Gemfile
|
124
|
+
- Gemfile.lock
|
125
|
+
- Makefile
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- bin/console
|
129
|
+
- bin/setup
|
130
|
+
- hey-you-fcm-push.gemspec
|
131
|
+
- lib/hey-you-fcm-push.rb
|
132
|
+
- lib/hey_you/builder/fcm_push.rb
|
133
|
+
- lib/hey_you/channels/fcm_push.rb
|
134
|
+
- lib/hey_you/config/fcm_push.rb
|
135
|
+
- lib/hey_you_fcm_push/connection.rb
|
136
|
+
- lib/hey_you_fcm_push/message_object.rb
|
137
|
+
- lib/hey_you_fcm_push/message_object/android_config.rb
|
138
|
+
- lib/hey_you_fcm_push/message_object/apns_config.rb
|
139
|
+
- lib/hey_you_fcm_push/message_object/fcm_options.rb
|
140
|
+
- lib/hey_you_fcm_push/message_object/notification.rb
|
141
|
+
- lib/hey_you_fcm_push/message_object/web_push_config.rb
|
142
|
+
- lib/hey_you_fcm_push/version.rb
|
143
|
+
homepage: https://github.com/QNester/hey-you-fcm-push
|
144
|
+
licenses: []
|
145
|
+
metadata:
|
146
|
+
allowed_push_host: https://rubygems.org/
|
147
|
+
homepage_uri: https://github.com/QNester/hey-you-fcm-push
|
148
|
+
source_code_uri: https://github.com/QNester/hey-you-fcm-push.git
|
149
|
+
changelog_uri: https://github.com/QNester/hey-you-fcm-push/CHANGELOG.md
|
150
|
+
post_install_message:
|
151
|
+
rdoc_options: []
|
152
|
+
require_paths:
|
153
|
+
- lib
|
154
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: 2.5.0
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
requirements: []
|
165
|
+
rubygems_version: 3.1.2
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: Send fcm pushes via hey-you
|
169
|
+
test_files: []
|