snm-fcm 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +64 -16
  3. data/lib/snm/fcm/version.rb +1 -1
  4. data/lib/snm/fcm.rb +16 -9
  5. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 991a04d8b3f660e9f74fbdf29a31b7478e0ed39b04a2a510d1353550d6fb16b2
4
- data.tar.gz: c370801fce9fd3413877015b9484a1cfbd1ece69eaeb2d852639dc188c87219b
3
+ metadata.gz: d59ac9bf55d1e9380c20cf2ff2c94f170bd160fc1fd32d2437bbaa4f559b715a
4
+ data.tar.gz: 988218b976e0e35a148aba9cb17aadbf6bd19d5a8e0e45092b3d3c090256fce0
5
5
  SHA512:
6
- metadata.gz: 52832fe027409c109c83d99dc32eac6bddd0d95f14a8e0cac644026bd9b52a616f5dad840a845456a8d0aeda52e443c6239daaa119e106871870d6d4d2cf6282
7
- data.tar.gz: e7d828acb36fe5b75b88428141714c1c1540a2746bdbb976aa5222d2e2ddcfd43c337dc9aaad133b4ed678ace3c9aac867f3e48fab5a46d958dd072032a38368
6
+ metadata.gz: 94f041e6ab5cf3ea1a0a88a0ed53e828576e3b5ee7b349074df60bbe8fbafb9db9feaaf02feff0a4b5d76d42de1db5560fc6d929fdd7c293404b3f56de8c01d7
7
+ data.tar.gz: 85bcbe3e2aeed13eafc76fc09c9efbd653d0ca8d5fb16e005a2c5f931b87ebdd8173d2d0ec058b94c439dbeeba7b855c7babf141fbc6151d03381f084a14e094
data/README.md CHANGED
@@ -1,34 +1,82 @@
1
1
  # Snm::Fcm
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
3
+ Ruby Library to send notification on android via Firebase Cloud Messaging API (V1)
4
4
 
5
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/snm/fcm`. To experiment with that code, run `bin/console` for an interactive prompt.
5
+ This library is based on Firebase Cloud Messaging API (V1) to send notification on android devices.
6
6
 
7
- ## Installation
7
+ ## Requirements
8
+ Version 0.1.6 requires at least Ruby 3.0.0, This library dependents on googleauth, redis, json and http gem.
8
9
 
9
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+ ## Installation
10
11
 
11
12
  Install the gem and add to the application's Gemfile by executing:
12
13
 
13
- $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+ $ gem 'snm-fcm', '~> 0.1.5'
14
15
 
15
16
  If bundler is not being used to manage dependencies, install the gem by executing:
16
17
 
17
- $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+ $ gem install snm-fcm
18
19
 
19
20
  ## Usage
20
-
21
- TODO: Write usage instructions here
22
-
23
- ## Development
24
-
25
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
-
27
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
21
+ ### Setup
22
+ Create an initializer file for ex rails-project/config/initializers/snm_fcm.rb
23
+ ```
24
+ Snm::Fcm::Notification.configure do |config|
25
+ config.credentails_file_path = 'path/to/my-fcm-e65a9915e042.json'
26
+ config.redis_endpoint = 'redis://localhost:6379/1'
27
+ end
28
+ Snm::Fcm::Notification.setup
29
+ ```
30
+ ### Send notification
31
+ ```
32
+ msg_data = {
33
+ 'message'=> {
34
+ 'token'=> 'android-device-fcm-token',
35
+ 'notification'=>
36
+ {
37
+ 'title'=> 'Lorem ipsum',
38
+ 'body'=> 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
39
+ 'image'=> 'https://www.snmmaurya.com/images/me.jpg'
40
+ },
41
+ 'data'=> {
42
+ 'key1'=> 'value1',
43
+ 'key2'=> 'value2'
44
+ }
45
+ }
46
+ }
47
+ respoonse = Snm::Fcm::Notification.deliver(msg_data)
48
+
49
+ # Success response:
50
+
51
+ {"name"=>"projects/my-fcm/messages/0:1715935676674045%f570bc3bf570bc3b"}
52
+
53
+ ```
54
+ ## How to get my-fcm-e65a9915e042.json
55
+
56
+ 1. Got go https://console.firebase.google.com/
57
+
58
+ ### For Android (FE)
59
+
60
+ 2. Create your project for ex 'my-fcm' and download google-services.json to set up your android project.
61
+
62
+ ### For Ruby
63
+ 3. Go to the project click on settings icon next to Project Overview top left cornor.
64
+
65
+ 3.1 in the general tab you will find 'project id' copy this id and put somewhere in your system.
66
+
67
+ 3.2 Click on Cloud Messging tab -
68
+
69
+ 3.3 In side Firebase Cloud Messaging API (V1) panel.
70
+
71
+ 3.4 Click on Manage Service Accounts On the next page a service already be there click on the name of that serive name for ex 'firebase-adminsdk-4apop@my-fcm.iam.gserviceaccount.com'
72
+
73
+ 3.5 Click on advanced settings - choose 'keys' tab.
74
+
75
+ 3.6 Now create a new key type json download it (automatically downloaded) put this file somewhere in your system. this file is your my-fcm-e65a9915e042.json
28
76
 
29
77
  ## Contributing
30
78
 
31
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/snm-fcm. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/snm-fcm/blob/main/CODE_OF_CONDUCT.md).
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/snmmaurya/snm-fcm. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/snmmaurya/snm-fcm/blob/main/CODE_OF_CONDUCT.md).
32
80
 
33
81
  ## License
34
82
 
@@ -36,4 +84,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
36
84
 
37
85
  ## Code of Conduct
38
86
 
39
- Everyone interacting in the Snm::Fcm project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/snm-fcm/blob/main/CODE_OF_CONDUCT.md).
87
+ Everyone interacting in the Snm::Fcm project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/snmmaurya/snm-fcm/blob/main/CODE_OF_CONDUCT.md).
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Snm
4
4
  module Fcm
5
- VERSION = "0.1.5"
5
+ VERSION = "0.1.6"
6
6
  end
7
7
  end
data/lib/snm/fcm.rb CHANGED
@@ -4,13 +4,14 @@ require_relative "fcm/version"
4
4
  require 'googleauth'
5
5
  require 'http'
6
6
  require 'json'
7
+ require 'redis'
7
8
 
8
9
  module Snm
9
10
  module Fcm
10
11
  class Error < StandardError; end
11
12
 
12
13
  class Configuration
13
- attr_accessor :credentails_file_path, :project_id
14
+ attr_accessor :credentails_file_path, :project_id, :redis_endpoint, :snm_fcm_redis
14
15
  end
15
16
 
16
17
  class Notification
@@ -18,7 +19,7 @@ module Snm
18
19
  @configuration ||= Configuration.new
19
20
  end
20
21
 
21
- def self.setup(&block)
22
+ def self.configure(&block)
22
23
  yield(configuration)
23
24
  end
24
25
 
@@ -30,18 +31,24 @@ module Snm
30
31
  end
31
32
 
32
33
  def self.get_access_token
33
- scope = 'https://www.googleapis.com/auth/cloud-platform'
34
- authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
35
- json_key_io: File.open(Notification.configuration.credentails_file_path),
36
- scope: scope
37
- )
38
- authorizer.fetch_access_token!['access_token']
34
+ access_token = Notification.configuration.snm_fcm_redis.get('access_token')
35
+ if access_token.nil?
36
+ scope = 'https://www.googleapis.com/auth/cloud-platform'
37
+ authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
38
+ json_key_io: File.open(Notification.configuration.credentails_file_path),
39
+ scope: scope
40
+ )
41
+ access_token = authorizer.fetch_access_token!['access_token']
42
+ Notification.configuration.snm_fcm_redis.setex('access_token', 3599, access_token)
43
+ end
44
+ access_token
39
45
  end
40
46
 
41
- def self.set_project
47
+ def self.setup
42
48
  file = File.read(Notification.configuration.credentails_file_path)
43
49
  data_hash = JSON.parse(file)
44
50
  Notification.configuration.project_id = data_hash["project_id"]
51
+ Notification.configuration.snm_fcm_redis = Redis.new(url: Notification.configuration.redis_endpoint)
45
52
  end
46
53
  end
47
54
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snm-fcm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - snmmaurya
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-16 00:00:00.000000000 Z
11
+ date: 2024-05-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http
@@ -58,6 +58,20 @@ dependencies:
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
60
  version: 2.7.2
61
+ - !ruby/object:Gem::Dependency
62
+ name: redis
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '5.2'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '5.2'
61
75
  description: Ruby Library to send notification on android via Firebase Cloud Messaging
62
76
  API (V1)
63
77
  email: