decision-labs / fcm Goto Github PK
View Code? Open in Web Editor NEWRuby bindings to Firebase Cloud Messaging (FCM) for Android, iOS or Web
License: MIT License
Ruby bindings to Firebase Cloud Messaging (FCM) for Android, iOS or Web
License: MIT License
When I use send_to_topic
it returns nil, instead of send_with_notification_key
which returns response from fcm.
Could you please help me out..
Req:
require 'fcm'
class FcmController < ApplicationController
def sendPushNote
fcm = FCM.new("1:776--------e6ec29")
registration_ids= ["fuOeAA-fPwI:APA91bFiUr7nNKOkXZEQYswuXq7AuQ4Hzjhw_OO-V3TGC3BnHNueZnvt8noE-35--------ByurLtBaNwVg6Zd_cIiv_0IFgSpn77g"] # an array of one or more client registration tokens
options = {data: {score: "123"}, collapse_key: "updated_score"}
response = fcm.send(registration_ids, options)
case response[:status]
when 200
render json: {response: response[:body], status_code: response[:status]}, statu: :ok
else
render json: {response: response[:body], status_code: response[:status], headers: response[:headers]}, statu: :un_processible_entity
end # when
end # sendPush
end # class
Res :
{
response: "<HTML> <HEAD> <TITLE>Internal Server Error</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Internal Server Error</H1> <H2>Error 500</H2> </BODY> </HTML> ",
status_code: null}
Is the FCM class thread safe or should I create a new one for each request? Is there a way to set global options?
Assume that I send $fcm.send(tokens, {data: some_data, notifications: some_notifications})
would that send two push notif, one contains data key and the other contains notification key?
my project seems to have this problem.
Does this gem support sending notification to multiple topics?
I am receiving this error, when I am using legacy server key.
Can someone please tell me. Thanks
When sending the field click_action
to a Flutter app, nothing happens (the field is not received in the push notification).
This is the body I'm sending:
{
notification: {
title: 'Title',
body: 'Message',
tag: 'Update',
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
Doesn't this gem support this or am I doing something wrong?
I am getting the response as
{:body=>"{"multicast_id":6960682389725131834,"success":2,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1497533189234401%0aef4f2ef9fd7ecd"},{"message_id":"0:1497533189249924%0aef4f2ef9fd7ecd"}]}", :headers=>{"content-type"=>["application/json; charset=UTF-8"], "date"=>["Thu, 15 Jun 2017 13:26:29 GMT"], "expires"=>["Thu, 15 Jun 2017 13:26:29 GMT"], "cache-control"=>["private, max-age=0"], "x-content-type-options"=>["nosniff"], "x-frame-options"=>["SAMEORIGIN"], "x-xss-protection"=>["1; mode=block"], "server"=>["GSE"], "alt-svc"=>["quic=":443"; ma=2592000; v="38,37,36,35""], "accept-ranges"=>["none"], "vary"=>["Accept-Encoding"], "connection"=>["close"]}, :status_code=>200, :response=>"success", :canonical_ids=>[], :not_registered_ids=>[]}
But the message is not there on firebase console or not posted to device
response = fcm.send(registration_ids, options) function status : "success":1, "failure":0
but did not getting on the app background.
Is it really necessary to build exponential backoff to our server logic?
How frequent will we get this error? At what amount of rps will we hit this error?
How should we manage sync of exponential backoff time in huge distributed workers(100-1000) environments which pull notification jobs from queue.
hai im sule
i tested in ruby 2.5.3 and rails 5.2 .3
notification title and body no show
here example code
fcm = FCM.new("keyfcm")
options = {
"notification": {
"body": "book aproving",
"title": "your booking for serive has been approve",
"click_action": "MainActivity"
}
}
response =fcm.send_to_topic_condition(
"'USER_ID_23' in topics && 'testing' in topics",
data: {
"body": "This is a FCM Topic Message!",
"title": "Teknopolitan bsd",
})
Hi!
It would be cool it the gem took care of splitting more than 1000 registration tokens into batches.
SSL_connect returned=1 errno=0 state=error: certificate verify failed
registration_ids= [token2] # an array of one or more client registration tokens
options = {data: {message: "123"}, collapse_key: "updated_score"}
response = fcm.send(registration_ids, options)
I just implement the simplest code to send notification to Android. Double check the server key and tokens are correct. Any guide? (Environment is Window 10)
Thanks.
This is how my options look. I am receiving notification without image. How do I send image?
options = {
data: {
title: "MagoGenie",
message: "Have a Great Day!!",
image_url: "https://www.magogenie.com/uploads/magogenie_related/web_banner-min.png"
}
}
Hi guys!
I'm facing a problem when i try to send a notification to IOS devices:
"results":[{"error":"InvalidRegistration"}]}",
But it happens only to IOS devices, to Android it's working fine.
Do you know what can i do to fix it?
To have sure about my APNS certificate, i generate a new certificate and added to firebase, but nothing changes..
I'm using the last FCM gem vesion too 1.0.2
Do you have any suggestion?
thanks!
Hi,
The correct endpoint for Device Group Messaging should be https://android.googleapis.com/gcm/notification. The current endpoint returns 404 error.
Link to FCM Docs on Device Group Messaging:
https://firebase.google.com/docs/cloud-messaging/notifications
All of the examples use the keyword argument format
fcm.add(notification_key: "foo", project_id: "123" ..)
But these are not actually supported by the library, which requires parameters by position.
Just a heads up everyone. We are renaming our organization to decision-labs
. This will also be reflected in the name of github organization. We will be doing this next week Friday 25th of September 11:00h GMT
After that everything should continue to work as before. BUT when you have the chance you should change the references to spacialdb
to decision-labs
If you have any questions please feel free to post them here.
Thanks
I would like to use this with ruby 1.9.3 but your gem does not allow it. I'll try to run it anyway but for speeding up debug time, do you know already what part of you code might have issues with that version ?
Thank you.
Firebase Response={"multicast_id":6346611008984444477,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1527570229728811%746f20adf9fd7ecd"}]}
But My firebase console didnt receive any message May i know the correction
Thanks in Advance
Hi
Is there a reason why you are using faraday
version 0.15.4
specifically (https://github.com/spacialdb/fcm/blob/master/fcm.gemspec#L22) ?
At this point faraday
is on version 1.0.0
. Would it be possible to be less strict on faraday version?
Thanks
Marcos
While on receive data is null on notification.
This code tries to stop you from sending to topics that don't match the valid regex provided on FCM.
def send_to_topic(topic, options = {})
if topic =~ /[a-zA-Z0-9\-_.~%]+/
send_with_notification_key('/topics/' + topic, options)
end
end
But the =~
method will still match a string that has invalid characters in it, as long as there are valid characters in it. [see here].(https://ruby-doc.org/core-2.2.0/Regexp.html#method-i-3D~)
So the following can happen:
$ topic = "TopicA$"
$ topic =~ /[a-zA-Z0-9\-_.~%]+/
$ => 0
Can we change this to return false when invalid characters are present in the topic? Something like this maybe?
$ topic = "TopicA$"
$ topic.gsub(/[a-zA-Z0-9\-_.~%]+/, "").length == 0
$ => false
We are using this library with JRuby-9.1.15.0 and Sidekiq.
Notifications work for sometime when sidekiq is restarted and stop working after sometime.
We are creating a new FCM instance everytime while sending notifications.
Could this be related to thread safety?
Faraday 1.0.1 has been released.
https://github.com/lostisland/faraday/releases/tag/v1.0.1
Is there a way to use the firebase emulator locally for use with testing?
I will like to request for Google Instance ID API to be bundled in this gem. This can allow use cases such as add, remove and subscribing of topics on server side, as well as retrieving all the topics that the registration_id is subscribed to.
I am using Ruby version 1.8.7.
I use this FCM gem https://github.com/spacialdb/fcm and want to send notification message to Android client app but it does not work.
In Controller:
fcm = FCM.new(FIREBASE_API_KEY, :timeout => 30) options = {:data => {:message => "This is a FCM Topic Message!"}} response = fcm.send_to_topic('global', options)
Class FCM:
`require 'httparty'
require 'cgi'
require 'json'
class FCM
include HTTParty
base_uri 'https://fcm.googleapis.com/fcm'
default_timeout 30
format :json
attr_accessor :timeout, :api_key
def initialize(api_key, client_options = {})
@api_key = api_key
@client_options = client_options
end
def send_with_notification_key(notification_key, options = {})
body = { :to => notification_key }.merge(options)
params = {
:body => body.to_json,
:headers => {
'Authorization' => "key=#{@api_key}",
'Content-Type' => 'application/json'
}
}
response = self.class.post('/send', params.merge(@client_options))
response.parsed_response
end
def send_to_topic(topic, options = {})
if topic =~ /[a-zA-Z0-9-_.~%]+/
send_with_notification_key('/topics/' + topic, options)
end
end
end`
The server key is correct, because I can send notification successfully by PHP code.
The response output as below: {"message_id"=>8885803884270587181}
Could anyone please to point out what wrong with the code. Any help would be greatly appreciated.
Am getting this error: LoadError (cannot load such file -- fcm), while trying to use fcm gem. But if i use gcm it has no error. But i upgraded to using firebase.
I have it in my gemfile and i have tried out everything i can. Can any one help me out please?
This is my class that i use to send the notification with
class Device < ActiveRecord::Base
require 'fcm'
def self.notify_android(recipient)
fcm = FCM.new(".....")
registration_ids= recipient # an array of one or more client registration tokens
options = {data: {hi: "invoice"}}
response = fcm.send(registration_ids, options)
end
end
Can we set expiry to FCM message i.e. if it doesn't get delivered after 10 minutes in case mobile is offline expire the message
Thanks
I'm currently using your GCM gem on a project and I'm upgrading my Android app to use FCM. Is this gem ready for use? Only asking because the initial push was 12 hours ago.
Thanks!
Hello
Currently, at work we are processing lots of FCM notifications. We want to be able to properly handle failures of few notifications in batches of 1000 notifications.
In the doc it is mentioned:
Array of objects representing the status of the messages processed. The objects are listed in the same order as the request (i.e., for each registration ID in the request, its result is listed in the same index in the response).
Don't you think it could be a good idea to have the ability to process result and get better error handling?
For example:
response = fcm.send(registration_ids, options, return_errors: true)
pp response.errors
{
"kjlhgeqiworguhqeporigqhe98y324t513": "MismatchSenderId"
}
Cheers
๐ฎ
HTTParty uses class variables which are modified by this gem everytime we make HTTP request.
This is obviously not threadsafe.
We could use Faraday instead which uses instance variables to create HTTP connections and supports JRuby as well.
I have made these changes and it seems to be working pretty well so far.
Any comments?
Is there some debug systems to show what FCM gets and what send, any errors?
Hi anyone, why base uri declare like:
base_uri 'https://fcm.googleapis.com/fcm'
not:
base_uri 'https://fcm.googleapis.com/fcm/send'
I cant work properly. Please tell me
When I try to implement the example I get this error thrown at the console.
SocketError (Failed to open TCP connection to fcm.googleapis.com:443 (getaddrinfo: Temporary failure in name resolution))
I think it has to do that FCM API URL in the Gem is outdated. The documentation suggests the following API URL for this purpose
https://fcm.googleapis.com/fcm/send
Regular curl command to test push notifications work for me.
Using the Gem i get a notification that says
"This site has been updated in the background" instead of the notification
Response is 200, success
I have made a fork for implementing the retry specified on google fcm references
pull request #58
Getting this response:
{:body=>"{\"multicast_id\":5212874389930311708,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1490785863976498%81297788962bca0e\"}]}", :headers=>{"content-type"=>["application/json; charset=UTF-8"], "date"=>["Wed, 29 Mar 2017 11:11:03 GMT"], "expires"=>["Wed, 29 Mar 2017 11:11:03 GMT"], "cache-control"=>["private, max-age=0"], "x-content-type-options"=>["nosniff"], "x-frame-options"=>["SAMEORIGIN"], "x-xss-protection"=>["1; mode=block"], "server"=>["GSE"], "alt-svc"=>["quic=\":443\"; ma=2592000; v=\"37,36,35\""], "accept-ranges"=>["none"], "vary"=>["Accept-Encoding"], "connection"=>["close"]}, :status_code=>200, :response=>"success", :canonical_ids=>[], :not_registered_ids=>[]}
Since GCM will be deprecated in April, Is it okey to use gcm for this constant?
GROUP_NOTIFICATION_BASE_URI = 'https://android.googleapis.com/gcm'
I'm able to send push notifications to $RECIPIENT when I'm using console.firebase.com.
If I send with spacialdb/fcm, then no push is sent. Even though the reply from the server looks ok. => (:status_code=>200, :response=>"success")
Any idea what might be wrong?
code
require 'fcm'
fcm = FCM.new('$TOKEN')
# An array of one or more client registration tokens
registration_ids = ['$RECIPIENT']
options = { data: { score: '123' }, collapse_key: 'updated_score' }
response = fcm.send(registration_ids, options)
puts "Sendt: #{response}"
running code
$ ruby fcm.rb
Sendt: {:body=>"{\"multicast_id\":60356242858746826,\"success\":1,\"failure\":0,
\"canonical_ids\":0,
\"results\":[{\"message_id\":\"0:1528896392320437%e06a962bca0e\"}]}",
:headers=>{"content-type"=>["application/json; charset=UTF-8"],
"date"=>["Wed, 13 Jun 2018 13:26:32 GMT"], "expires"=>["Wed, 13 Jun 2018 13:26:32 GMT"], "cache-control"=>["private, max-age=0"],
"x-content-type-options"=>["nosniff"], "x-frame-options"=>["SAMEORIGIN"],
"x-xss-protection"=>["1; mode=block"],
"server"=>["GSE"], "alt-svc"=>["quic=\":443\"; ma=2592000; v=\"43,42,41,39,35\""],
"accept-ranges"=>["none"], "vary"=>["Accept-Encoding"],
"connection"=>["close"]}, :status_code=>200, :response=>"success",
:canonical_ids=>[], :not_registered_ids=>[]}
Hi Kashif,
I have a need to show the notification history send from the specific FCM key with their state : delivered successfully or missed. Thats-why i am looking for a dashboard . Because we have only got the status by server that the particular notification have reached (to particular server ie: fcm and apn,s) but cant have such information that is that particular notification is delivered to device or not?
So please help if you can do any needful.
Quick response is awaited.
Thanks
@sabman trying to use tag or collapase_key but it's not working as supposed to. Any idea? that's my cordova client code arnesson/cordova-plugin-firebase#385
Hi. I have just integrated fcm in my rails applications. i have tried to test the notifications but this is giving me invalid registration error although registration key is correct.
Following is the response
{:body=>"{\"multicast_id\":7702172266196992064,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"InvalidRegistration\"}]}", :headers=> {"content-type"=>["application/json; charset=UTF-8"], "date"=>["Thu, 16 Nov 2017 13:54:00 GMT"], "expires"=>["Thu, 16 Nov 2017 13:54:00 GMT"], "cache-control"=>["private, max-age=0"], "x-content-type-options"=>["nosniff"], "x-frame-options"=>["SAMEORIGIN"], "x-xss-protection"=>["1; mode=block"], "server"=>["GSE"], "alt-svc"=>["hq=\":443\"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=\":443\"; ma=2592000; v=\"41,39,38,37,35\""], "accept-ranges"=>["none"], "vary"=>["Accept-Encoding"], "connection"=>["close"]}, :status_code=>200, :response=>"success", :canonical_ids=>[], :not_registered_ids=>[]}
Facing an issue with Ios Notification which is not receiving at device. Same setup is working fine with Android.
Found few discussion but didn't help
a. #15
b. #1 (comment)
Here is my
Request:
fcm = FCM.new(ENV["FCM_TOKEN"])
options = {data: {message: message}, priority:'high'}
response = fcm.send(device_tokens, options)
Response:
{:body=>
"{"multicast_id":8122297942819980145,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1505802064679190%9310a6bdf9fd7ecd"}]}",
:headers=>
{"content-type"=>["application/json; charset=UTF-8"],
"date"=>["Tue, 19 Sep 2017 06:21:04 GMT"],
"expires"=>["Tue, 19 Sep 2017 06:21:04 GMT"],
"cache-control"=>["private, max-age=0"],
"x-content-type-options"=>["nosniff"],
"x-frame-options"=>["SAMEORIGIN"],
"x-xss-protection"=>["1; mode=block"],
"server"=>["GSE"],
"alt-svc"=>["quic=":443"; ma=2592000; v="39,38,37,35""],
"accept-ranges"=>["none"],
"vary"=>["Accept-Encoding"],
"connection"=>["close"]},
:status_code=>200,
:response=>"success",
:canonical_ids=>[],
:not_registered_ids=>[]}
Please let me know if i'm doing anything wrong here.
@kmckinley @kashif @fluxsaas
Thank you
Apps using the FCM legacy HTTP API should consider migrating to the HTTP v1 API
For details:
https://firebase.google.com/docs/cloud-messaging/migrate-v1
Hi, I was recently using your gem to send messages to Firebase. But I don't like to configure an instance always when I want to send.
So about that, I was thinking about creating an initializer way to configure the gem before sending notifications. Instead of doing this:
require 'fcm'
fcm = FCM.new('API_KEY')
registration_ids = ['11', '12']
options = {
title: 'Portugal vs Denmark',
body: '5x1'
}
fcm.send(registration_ids, options)
And start doing this:
# config/initializers/fcm.rb
require 'fcm'
FCM.configure do |config|
config.api_key = 'MY_API_KEY'
end
And the file I want to send notifications, just:
FCM.send(registration_ids, options)
I was working this morning on a version of that, but I stopped to try to view if this is useful right now. What do you think about that?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.