使用 FCM 从 Rails API 推送通知到 Android 没有发送通知?



我正在编写 rails 5 中的 API Rest,使用 gem 'fcm' 发送通知。我已经在我的安卓应用程序中配置了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

我从邮递员执行,这是执行控制器的路由:

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

并且,这是响应:

{

"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":["星期四, 17 八月 2017 17:43:39 GMT"],"expires":["星期四, 17 Aug 2017 17:43:39 GMT"],"缓存控制":["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\"],"接受范围":["无"],"vary":["接受编码"],"连接":["关闭"]},"status_code":200,"响应":"成功","canonical_ids":[],"not_registered_ids":[]}

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

我错过了什么吗?

请帮忙?

还是有其他方法或 Ruby Gem 可以通过清晰的示例发送通知?

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

除了使用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

其余客户端 gem

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

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

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

相关内容

  • 没有找到相关文章

最新更新