DEV Community

Neha Nakrani
Neha Nakrani

Posted on

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 send to app using Google's Firebase Cloud Messaging.

Alt Text

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,
                sound: 'default',
                icon: image
                }
              }
    registration_ids = ["registration_id1", "registration_id2"] ([Array of registration ids up to 1000])
    # A 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
Enter fullscreen mode Exit fullscreen mode

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

Now, your notification should be ready for appearing.

Thank you for reading!🤗

This blog was originally published on Medium 📝

Oldest comments (3)

Collapse
 
gidpoiiohika profile image
Yaroslav

hello, what is registration_ids and how and where to get it?

Collapse
 
priyanka0808 profile image
priyanka-0808

registartion_id is a device_token of the user who will get notification.

Collapse
 
sukumar profile image
pavan

can you please update with latest version code format