FCM push notification with ruby on rails

Neha Nakrani
2 min readOct 8, 2019

--

FCM push notification with ruby on rails

FCM is one of the best-known ways to notify the client iOS, Android, or web/JavaScript via push notification as it is free and backed by Google.
Push notification is a very popular communication channel to notify users about important events in the application.
This is about how to enable push notification to send to the app using Google’s Firebase Cloud Messaging.

Setup FCM :

  1. Go to the Firebase Console page and click on Add new project.
  2. If you don’t have an existing Firebase project, click Add project and enter either an existing Google Cloud Platform project name or a new project name.
  3. Get a server key from settings.

FCM with Ruby on Rails :

  1. Install fcm gem and just gem ‘fcm’ include in your Gemfile.
  2. Get the registration token or devices token from the devices where App installed. There can be multiple devices registered for an app.
  3. Call and setup your the fcm_call_notification function when you send the notification.
require 'fcm'
def fcm_push_notification
fcm_client = FCM.new(FCM_SEVER_KEY) # set your FCM_SERVER_KEY options = { priority: 'high',
data: { message: message, icon: image },
notification: { body: 'message',
title: 'title',
sound: 'default',
icon: 'image.png'
}
}
registration_ids = ["registration_id1", "registration_id2"]
#([Array of registration ids up to 1000])
# Registration ID looks something like: "dAlDYuaPXes:APA91bFEipxfcckxglzRo8N1SmQHqC6g8SWFATWBN9orkwgvTM57kmlFOUYZAmZKb4XGGOOL9wqeYsZHvG7GEgAopVfVupk_gQ2X5Q4Dmf0Cn77nAT6AEJ5jiAQJgJ_LTpC1s64wYBvC"
registration_ids.each_slice(20) do |registration_id|
response = fcm_client.send(registration_id, options)
puts response
end
end

Set options according to your requirement with the fcm_push_notification function will notify the client apps with the image also.

I hope you found it as easy as I did to make this work.

Thank you for reading!!!

--

--