在Ruby on Rails中使用Typhoeus :: Request向FCM发送请求

时间:2017-01-16 03:24:49

标签: ruby-on-rails json ruby firebase-cloud-messaging

我想使用Typhoeus :: Request向FCM发送一个简单的请求。我似乎做了一件非常糟糕的事情,但几个小时后就无法提出任何线索......

这是我向FCM发送请求的方式:

req = Typhoeus::Request.new(
        Fcm_server_uri,
        method: :post,
        params: {:to => fcm_registration_id},
        headers: {'Authorization' => Fcm_server_api_key,'Content-Type' => "application/json",charset: "UTF-8"})

    req.run
    response = req.response

    body = response.body

我在response.body中继续收到以下消息:

"JSON_PARSING_ERROR: Unexpected token END OF FILE at position 0."

错误信息清楚地表明,与Json的关系可能是错误的,我已经尝试过提供我的Json的各种组合,但还没有成功。我真的很感激任何提示!

1 个答案:

答案 0 :(得分:1)

我完全感到困惑的是,为什么将Jsons发送到FCM在Typhoeus中不起作用但我终于设法通过将Content-Type从application / json更改为纯文本并以纯文本格式发送我的消息来发送成功请求当然。

这是我为方便起见编写的完整助手模块:

module FcmModule
  require 'typhoeus'
  require 'typhoeus/request'

  Fcm_server_api_key = 'key=<YOUR_SERVER_KEY>'
  Fcm_server_uri = 'https://fcm.googleapis.com/fcm/send'

  Status_message_sent = 0
  Status_failed = 1
  Status_not_registered = 2
  Status_update_registration_id = 3

  def send_notification_to_fcm(title, description, from_teacher,
                               notification_type_id, fcm_registration_id)

    req = Typhoeus::Request.new(
        Fcm_server_uri,
        method: :post,
        body: "registration_id=#{fcm_registration_id}&" +
            "data.myFromTeacher=#{from_teacher}&" +
            "data.myTitle=#{title}&" +
            "data.myDescription=#{description}&" +
            "data.myNotificationTypeId=#{notification_type_id}",

        headers: {'Authorization' => Fcm_server_api_key,'Content-Type' => "application/x-www-form-urlencoded",charset: "UTF-8"})

    req.run
    response = req.response

    body = response.body
    bodyResults = Hash[body.each_line.map { |l| l.chomp.split('=', 2) }]

    if !bodyResults['id'].nil? && !bodyResults['registration_id'].nil?
      return FcmResponse.new(bodyResults['id'], bodyResults['registration_id'], Status_update_registration_id)
    end

    if !bodyResults['Error'].nil?
      if bodyResults['Error'] == 'NotRegistered'
        return FcmResponse.new(nil, nil, Status_not_registered)
      else
        return FcmResponse.new(nil, nil, Status_failed)
      end
    else
      return FcmResponse.new(bodyResults['id'], nil, Status_message_sent)
    end

  end

  class FcmResponse

    def initialize(message_id, registration_id, status)
      @message_id = message_id
      @registration_id = registration_id
      @status = status
    end

    def message_id
      @message_id
    end

    def registration_id
      @registration_id
    end

    def status
      @status
    end

  end
end

以下是使用该模块的示例:

fcm_response = send_notification_to_fcm('title','description', 'from_teacher', 1, fcm_registration_id)
        if fcm_response.status == Status_message_sent
          # todo save to our users notifications in database
        elsif fcm_response.status == Status_update_registration_id
          # todo update fcm_registration_id for given device with fcm_response.registration_id
        elsif fcm_response.status == Status_not_registered
          # todo delete given device from our database
        elsif fcm_response.status == Status_failed
          # todo return some error message to client to retry sending the notification
        end

编辑:

Ekhm,我不能放过它再一次看着代码。为了在Typhoeus请求后发送Json,我必须在“body”参数中提供哈希,而不是“params”。这是发送Json的工作请求:

req = Typhoeus::Request.new(
        Fcm_server_uri,
        method: :post,
        body: {'to' => fcm_registration_id}, # body instead of params!
        headers: {'Authorization' => Fcm_server_api_key,'Content-Type' => "application/json",charset: "UTF-8"})

    req.run
    response = req.response

    body = response.body

现在请原谅,我需要把头靠在墙上......