Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Firebase Cloud Messaging API wrapper for Ruby, suppot HTTP v1 API including access_token auto refresh feature.

License

NotificationsYou must be signed in to change notification settings

miyataka/fcmpush

Repository files navigation

Fcmpush is an Firebase Cloud Messaging(FCM) Client. It implementsFCM HTTP v1 API, includingAuto Refresh access_token feature, and batch request!!This gem supports HTTP v1 API only,NOT supportedlegacy HTTP protocol.

fcmpush is highly inspired byandpush gem.

Installation

Add this line to your application's Gemfile:

gem'fcmpush'

And then execute:

$ bundle

Or install it yourself as:

$ gem install fcmpush

Usage

on Rails, config/initializers/fcmpush.rb

Fcmpush.configuredo |config|## for message push# firebase web console => project settings => service account => firebase admin sdk => generate new private key# pass string of path to credential file to config.json_key_ioconfig.json_key_io="#{Rails.root}/path/to/service_account_credentials.json"# Or content of json key file wrapped with StringIO# config.json_key_io = StringIO.new('{ ... }')# Or set environment variables# ENV['GOOGLE_ACCOUNT_TYPE'] = 'service_account'# ENV['GOOGLE_CLIENT_ID'] = '000000000000000000000'# ENV['GOOGLE_CLIENT_EMAIL'] = 'xxxx@xxxx.iam.gserviceaccount.com'# ENV['GOOGLE_PRIVATE_KEY'] = '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n\'## for topic subscribe/unsubscribe because they use regacy auth# firebase web console => project settings => cloud messaging => Project credentials => Server key# @deprecated: This attribute will be removed next version.config.server_key='your firebase server key'# Or set environment variables# @deprecated: This attribute will be removed next version.# ENV['FCM_SERVER_KEY'] = 'your firebase server key'# Proxy ENV variables are considered by default if set by net/http, but you can explicitly define your proxy host here# user and password are optional# config.proxy = { uri: "http://proxy.host:3128", user: nil, password: nil }# explicitly disable using proxy, even ignore environment variables if set# config.proxy = false# HTTP connection open and read timeouts (in seconds) are set for all client requests.# If unset, the default values for Net::HTTP::Persistent are used (currently 60 seconds).# config.open_timeout = 30# config.read_timeout = 15end

for more detail. seehere.

push message

require'fcmpush'project_id="..."# Your project_iddevice_token="..."# The device token of the device you'd like to push a message toclient=Fcmpush.new(project_id)payload={# ref. https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messagesmessage:{token:device_token,notification:{title:"this is title",body:"this is message body"}}}response=client.push(payload)json=response.jsonjson[:name]# => "projects/[your_project_id]/messages/0:1571037134532751%31bd1c9631bd1c96"

push messages in batch [DEPRECATED]

require'fcmpush'project_id="..."# Your project_iddevice_tokens=["...A","...B","...C"]# The device token of the device you'd like to push a message toclient=Fcmpush.new(project_id)payloads=device_tokens.mapdo |token|{# ref. https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messagesmessage:{token:token,notification:{title:"this is title",body:"this is message body"}}}endresponse=client.batch_push(payloads)response_array=response.jsonresponse_array.first[:name]# => "projects/[your_project_id]/messages/0:1571037134532751%31bd1c9631bd1c96"

topic subscribe/unsubscribe

require'fcmpush'project_id="..."# Your project_idtopic="your_topic_name"device_tokens=["device_tokenA","device_tokenB", ...]# The device tokens of the device you'd like to subscribeclient=Fcmpush.new(project_id)response=client.subscribe(topic,device_tokens)# response = client.unsubscribe(topic, device_tokens)json=response.jsonjson[:results]# => [{}, {"error":"NOT_FOUND"}, ...]  ref. https://developers.google.com/instance-id/reference/server#example_result_3

Performance

  • fcmpush's performance is good. (about the same as fastest one!)
  • And fcmpush supports batch request feature! batch request not use in benchmarking. Because, it not supported by other gems.
  • andpush is the fastest, but it uses legacy HTTP API.
  • fcmpush is fastest in gems using V1 HTTP API(fcmpush,google-api-fcm,firebase_cloud_messenger).
  • I excludedgoogle-api-fcm gem because it can't run in ruby 3.
  • benchmark detail ishere.
Warming up --------------------------------------             andpush     1.000  i/100ms                 fcm     1.000  i/100ms             fcmpush     1.000  i/100msfirebase_cloud_messenger                         1.000  i/100msCalculating -------------------------------------             andpush     19.236  (±10.4%) i/s -     95.000  in   5.048723s                 fcm      6.536  (±15.3%) i/s -     33.000  in   5.083179s             fcmpush     18.871  (±10.6%) i/s -     93.000  in   5.031072sfirebase_cloud_messenger                          3.238  (± 0.0%) i/s -     17.000  in   5.265755sComparison:             andpush:       19.2 i/s             fcmpush:       18.9 i/s - same-ish: difference falls within error                 fcm:        6.5 i/s - 2.94x  (± 0.00) slowerfirebase_cloud_messenger:        3.2 i/s - 5.94x  (± 0.00) slower

Experimental Features

  • proxy
    • LIMITATION: supporthttp_proxy only. NOT supportsHTTPS_PROXY.

Contributing

Bug reports and pull requests are welcome on GitHub athttps://github.com/miyataka/fcmpush.

License

The gem is available as open source under the terms of theMIT License.

About

Firebase Cloud Messaging API wrapper for Ruby, suppot HTTP v1 API including access_token auto refresh feature.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp