2017-01-16 10 views
1

Typhoeus :: Requestを使用してFCMに簡単なリクエストを送信します。Ruby on RailsでTyphoeus :: Requestを使用して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 

私は次のメッセージを取得しておいてください。これは私がFCMに要求を送信する方法である

...私はひどく間違って何かをやっているが、数時間から任意の手がかりを考え出すことができないようですresponse.bodyに:

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

エラーメッセージが明確に述べていJSONでそのSTHは間違っているかもしれないと私はすでに、まだ私のJSONとなしの成功を供給する様々な組み合わせを試してみました。私はどんなヒントにも本当に感謝しています!

+0

あなたは 'Fcm_server_api_key'前に' '「キー=」を追加してみてくださいことはできますか?通常、 'Authorization'の値の場合、渡される値は' key = 'の形式になります。 –

+0

入力いただきありがとうございます。残念ながら、私はすでに両方のケースを試していました: "key = server_key"と "key ="なし。それがなければ、サーバは誤った認証鍵を返すので、私は "key = server_key"を使用してen11します。 – Savail

答えて

1

TyphoeusでJsonをFCMに送信するのがなぜ機能しないのか全くわかりませんが、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 

EDIT:

Ekhm、よく私はそれを聞かせてことができませんでしたコードをもう一度見てみましょう。ポストTyphoeusリクエストでJsonを送信するために、私は "params"ではなく "body"パラメータでハッシュを提供しなければなりませんでした。ここでは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 

は今すみません、私は壁に頭を強打する必要があります...

+0

これは奇妙です。それでも、共有してくれてありがとう。 :) –

関連する問題