DEV Community

Neha Nakrani
Neha Nakrani

Posted on

20 2

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 📝

Image of Bright Data

Feed Your Models Real-Time Data – Enhance your AI with up-to-the-minute data.

Utilize our live data feeds for dynamic model training, ensuring your AI systems are always ahead.

Access Real-Time Data

Top comments (5)

Collapse
 
couponapproved profile image
couponapprove

One problem:> When the android app is killed, I dont receive notifications.

Collapse
 
kevin_rufus_80aa55bb89dbd profile image
Kevin Rufus

I am getting this error
body=>"{\"multicast_id\":3188400671596904807,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"InvalidApnsCredential\"}]}"

Collapse
 
sukumar profile image
pavan

can you please update with latest version code format

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.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay