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.
Setup FCM:
Go to the Firebase Console page and click on Add new project.
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.
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
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 📝
Top comments (5)
One problem:> When the android app is killed, I dont receive notifications.
I am getting this error
body=>"{\"multicast_id\":3188400671596904807,\"success\":0,\"failure\":1,\"canonical_ids\":0,\"results\":[{\"error\":\"InvalidApnsCredential\"}]}"
can you please update with latest version code format
hello, what is registration_ids and how and where to get it?
registartion_id is a device_token of the user who will get notification.