What is FCM?

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution to deliver messages at no cost.
Sending notification messages to drive user re-engagement and retention is possible.
An FCM implementation consists of two main components for sending and receiving:
  1. A trusted environment such as Cloud Functions for Firebase or an app server on which to build, target, and send messages.
  2. An iOS, Android, or web (JavaScript) client app that receives messages.

FCM on rails:

The FCM gem allows ruby backend send notifications to Android and iOS devices.
gem 'fcm'

Usage:

Get a server key from FCM.And register the devices (IOS/Android) for which the notifications to be sent.
A server key may be something like :
“AAAAyAPpUMw:APA91bG9gnGMaT36WW8LUbHy9wjhuuUzhrHDh67pkOqFeGynRWd31MI7k3tQmO1UPgvIZRF6b_TYDMFnzHa5Y4Qy_yG8Veg8SbbcMXsR

cjze-YJ210BKUB0r-ftw9gAgiG81l0S41Hyz”
Get the registration token from the devices where App installed.There can be multiple devices registered for an app.
A registration ID looks something like:
“dAlDYuaPXes:APA91bFEipxfcckxglzRo8N1SmQHqC6g8SWFATWBN9orkwgvTM57kmlFOUYZAmZKb4XGGOOL9wqeYsZHvG7GEgAopVfVupk_gQ2X5Q4Dmf0Cn77nAT6AEJ5jiAQJgJ_LTpC1s64wYBvC”
In order to send notification, FCM class needs to be initialized. Additionaly, the message that needs to be sent has to be declared within options hash.
Options can have a notification which are called data payloads. Data payload has custom key/value pairs, which will be accessible by the client app.
Notification payload enables fcm to automatically displays the message to end-user devices on behalf of the client app.
Sample sending notifications:
require 'fcm'

def fcm_call_notification

     fcm_client = FCM.new(server_key)

     options = {priority: 'high',data: {message: message,icon: image},notification: {body: message,sound: 'default',icon: image}}

user_device_ids=["dAlDYuaPXes:APA91bFEipxfcckxglzRo8N1SmQHqC6g8SWFATWBN9orkwgvTM57kmlFOUYZAmZKb4XGGOOL9wqeYsZHvG7GEgAopVfVupk_gQ2X5Q4Dmf0Cn77nAT6AEJ5jiAQJgJ_LTpC1s64wYBvC", "dAlDYuaPXes:APA91bFEipxfcckxglzRo8N1SmQHqC6g8SWFATWBN9orkwgvTM57kmlFOUYZAmZKb4XGGOOL9wqeYsZHvG7GEgAopVfVupk_gQ2X5Q4Dmf0Cn77nAT6AEJ5jiAQJgJ_LTpC1s64wYB23"] ([Array of registration ids up to 1000])

     user_device_ids.each_slice(20) do |device_ids|

          response =  fcm_client.send(device_ids, options)

          puts response  

     end

end
The response is just a hash containing the response body, headers and status_code.
Success message on calling above function:
{:body=>

"{\"multicast_id\":7599661814434358149,\"success\":2,\"failure\":0,\"canonical_ids\":0,\"results\":[]}",

:headers=>

 {"content-type"=>["application/json; charset=UTF-8"],

  "date"=>["Tue, 23 Jan 2018 09:23:13 GMT"],

  "expires"=>["Tue, 23 Jan 2018 09:23:13 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=>

 []}
Here is the response for non registered devices
{:body=>

"{\"multicast_id\":7599661814434358149,\"success\":0,\"failure\":2,\"canonical_ids\":0,\"results\":[{\"error\":\"NotRegistered\"},{\"error\":\"InvalidRegistration\"}]}",

:headers=>

 {"content-type"=>["application/json; charset=UTF-8"],

  "date"=>["Tue, 23 Jan 2018 09:23:13 GMT"],

  "expires"=>["Tue, 23 Jan 2018 09:23:13 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=>

 ["dAlDYuaPXes:APA91bFEipxfcckxglzRo8N1SmQHqC6g8SWFATWBN9orkwgvTM57kmlFOUYZAmZKb4XGGOOL9wqeYsZHvG7GEgAopVfVupk_gQ2X5Q4Dmf0Cn77nAT6AEJ5jiAQJgJ_LTpC1s64wYBvC"]}
The above fcm_call_notification function will notify the client apps with the image also.