使用FCM从Rails api向Android推送通知未发送通知?

时间:2017-08-18 19:30:31

标签: android ruby-on-rails rubygems firebase-cloud-messaging

我在rails 5中编写Api Rest,使用gem'fcm'发送通知。我已经在我的Android应用程序中配置了firebase,我可以从Firebase控制台成功发送通知,但是从我的rails api我无法收到设备中的通知,这是我的代码:

这是我的rails控制器:

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

我从Postman执行这是执行控制器的路线:

http://localhost:3000/acciones/enviar?here将设备令牌作为参数

而且,这是回复:

  

{ “本体”: “{\” multicast_id \ “:5276983113254623155,\” 成功\ “:1,\” 失败\ “:0,\” canonical_ids \ “:0,\” 结果\“:[{ \ “MESSAGE_ID \”:\ “0:1502991819420287%2293308c2293308c \”}]} “ ”标题“:{ ”内容类型“:[” 应用/ JSON;   charset = UTF-8“],”date“:[”Thu,2017年8月17日17:43:39   GMT“],”到期“:[”星期四,2017年8月17日17:43:39   GMT“],”缓存控制“:”私有,   最大年龄= 0 “],” X-内容类型的选项 “:[” nosniff “],” X框选项 “:[” SAMEORIGIN “]中,” x-XSS-保护 “:[” 1;   模式=块 “],” 服务器 “:[” GSE “],” ALT-SVC “:[” QUIC = \ “:443 \”; MA = 2592000;   V = \ “39,38,37,35 \” “],” 接受范围, “:[” 无 “],” 变化 “:[” 接受编码 “],” 连接 “:[” 关闭“]} “STATUS_CODE”:200, “响应”: “成功”, “canonical_ids”:[], “not_registered_ids”:[]}

响应显示成功:1和状态码:200但通知永远不会到达设备,并且firebase控制台不显示该消息。

我错过了什么吗?

请帮帮忙?

或者是否有其他方式或ruby gem以明确的例子发送通知?

欢迎任何建议......提前致谢

3 个答案:

答案 0 :(得分:0)

    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

答案 1 :(得分:0)

尝试此邮件选项,因为错误应该是您的通知语法。

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

答案 2 :(得分:0)

除了使用fcm gem之外,还可以使用RestClient gem。 fcm通知的用法如下。要注意的一件事是,如果有效载荷使用“ .to_json”传递,则标头内容类型也必须指定为json。希望有帮助。

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