Sunday 30 July 2017

Rails5 application with Push Notification using FCM for Android

Part One for Android

Gem used
a.fcm

Support

a. Android
b. IOS


Here we are going to see how push notification can be sent to android using fcm. Same fcm gem can be used for ios as well.

a. Add it to gem file and run bundle


#for push notification for ios and android 
 gem 'fcm'


b. Next add project to firebase 
(mobile developers will understand the process)

  1. visit https://console.firebase.google.com
  2. create a new project with required info(ex: project name)   
  3. Asks for  
      a.Add Firebase to your iOS app
      b.Add Firebase to your Android app
   here we go with android and fill the fields 
      a. Android package name
      b. Download config file
      c. Add Firebase SDK   
   Please do all required steps to get
      a. Server key   
      b. Legacy server key
      c. registration_ids/device token of every user who receives 
         the push notification  
 
c. Here i'm writing push notification as a separate service.Basically
 an api service. So my code goes here 

 /controller/api/PushNotifiactionService/push_notification.rb


  
require 'fcm'
fcm = FCM.new("Legacy server key") Ex: AIzaSydsfds324asfdsfdsf
# get it from project settings - cloud messaging 
registration_ids= ["fgf4yaJ64E:APA91bfsfds...", "EaIbj_2oGkR7JXi..."]  
#mobile end will provide you the ref_ids to whom shoulbe received
options = {data: {student_count: "50"}, collapse_key: "updated_count"}
#message content to sent in push notifiaction
response = fcm.send(registration_ids, options)
#sending push notifaiction

d. Add it to config/application.rb
config.autoload_paths += %W(#{config.root}/app/controllers
/PushNotificationService/PushNotification)
That's it



Reference Link :

a. https://github.com/spacialdb/fcm