matQ
matQ

Reputation: 617

Push notification to Android from Rails api using FCM is not sending the notification?

i am coding an Api Rest in rails 5, using gem 'fcm' to send notifications. I have already configure firebase in my android app and I can send notifications successfully from the Firebase console, but from my rails api i cannot receive the notificacion in my device, this is my code:

this is my rails controller:

class AccionesController < ApplicationController
 def enviar

    require 'fcm'

      fcm = FCM.new("AAAAlBfTsV4:AheregoesmySERVEKEYsXXm-vQGfMjVuo8TpYrApHsnGU4ZasdfajsdfñalUtf26LeND4U4lXFZZplpzJjTWoiisWP-Esl5afCSTmiDI9y5gP6OObqY76NVcOn9ceaIUGMZ")

      #  fcm = FCM.new("my_server_key", timeout: 3)

      registration_ids= [params[:devicetoken]] # an array of one or more client registration tokens
      options = {data: {score: "mynewscore"}, 
                 notification: {
                                    title: "Message Title", 
                                    body: "Hi, Worked perfectly",
                                    icon: "myicon"}
      ,collapse_key: "testeando desde rails", priority: "high"}
      response = fcm.send(registration_ids, options)
      render json: response
  end

  def noti_params
      params.permit(:devicetoken)
  end
end

I execute from Postman this is the route that execute the controller:

http://localhost:3000/acciones/enviar?here goes the device token as parameter

And, here is the response:

{"body":"{\"multicast_id\":5276983113254623155,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1502991819420287%2293308c2293308c\"}]}","headers":{"content-type":["application/json; charset=UTF-8"],"date":["Thu, 17 Aug 2017 17:43:39 GMT"],"expires":["Thu, 17 Aug 2017 17:43:39 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":["quic=\":443\"; ma=2592000; v=\"39,38,37,35\""],"accept-ranges":["none"],"vary":["Accept-Encoding"],"connection":["close"]},"status_code":200,"response":"success","canonical_ids":[],"not_registered_ids":[]}

the response shows success: 1 and status code: 200 but the notification never reaches the device,and the firebase console does not show the message.

Am I missing something?

please help?

or is there another way or ruby gem to send notification with a clear example?

any suggestions are welcome... thanks in advance

Upvotes: 1

Views: 3912

Answers (3)

zawhtut
zawhtut

Reputation: 8561

Instead of using fcm gem, you can also use RestClient gem. The usage for fcm notifications is as follow.One thing to note is if the payload is passing using ".to_json", the header content type also must be specified as json. Hope this help.

def self.send_noti(device_token)
    options = data().merge ({"to": "#{device_token}"})
    RestClient.post("https://fcm.googleapis.com/fcm/send", options.to_json, headers={'Content-Type' => 'application/json','Authorization' => "key=#{ENV['fcm_token']}"})        
end 

def self.data()
    options = {
                                 "notification": {
                                       "body": "Your noti body",
                                       "title": "Your noti title"
                                    },
                                    "data": {
                                       "d1": "Your data" #can be any, d1, status or whatever
                                    }                                        
            }

end

rest-client gem

Upvotes: 1

Raj
Raj

Reputation: 66

try this message options because the error should be your notification syntax.

options = {
    priority: 'high',
    data: {
        message: "Hai",
        location: location
    },
    notification: {
        body: "Hai",
        location: "location",
        sound: 'default'
    }
}

Upvotes: 0

Raj
Raj

Reputation: 66

    fcm_client = FCM.new(your_firebase_key)
    registration_ids= [user_device_token]
    options = {
        priority: 'high',
        data: {
            message: "Hai",
            location: location
        },
        notification: {
            body: "Hai",
            location: "location",
            sound: 'default'
        }
    }
    fcm_client.send(registration_ids, options)
  end
end

Upvotes: 0

Related Questions