2016-10-06 14 views
0

firebase経由でデバイスにメッセージを送信しようとしています。しかし、私は誤りがある。私はそれを事前のRESTクライアントでうまくテストしました。これは残りのクライアントからのメッセージですajaxでfirebase cloud messagingへの投稿要求を送信しますか?

Content-Type: application/json 
Authorization: key=MY-KEY 
Content-Length: 106 

POST /fcm/send HTTP/1.1 
HOST: fcm.googleapis.com 
content-type: application/json 
authorization: key=MY-KEY 
content-length: 106 

{ 
    "to":"/topics/Self_Taught" 
    "notification": 
    { 
    "body":"Hello" 
    } 
}  

これに基づいて、私はJavaScriptコードを作成しました。それほど心配しないでください、それは他の図書館であり、正常に動作します。

$.ajax({ 
      url: "https://fcm.googleapis.com/fcm/send", 
      type: "POST", 
      contentType: "application/json", 
      authorization: "key=MY-KEY", 
      data: { 
       "to": "/topics/Self_Taught", 
       "notification": { 
        "body": message 
       } 

      }, 
      success: function (result) { 
       $.gritter.add({ 
        title: "", 
        text: result.message_id, 
        class_name: 'gritter-success' 
       }); 
      }, 
      error: function (result) { 
       $.gritter.add({ 
        title: "", 
        text: result.error, 
        class_name: 'gritter-error' 
       }); 
      } 
     }); 

そして、これは私が戻ってresult.error

function() { 
if (l) { 
    var t = l.length; 
    (function i(t) { 
     x.each(t, function (t, n) { 
     var r = x.type(n); 
     "function" === r ? e.unique && p.has(n) || l.push(n) : n && n.length && "string" !== r && i(n) 
     }) 
    }) 
    (arguments), n ? o = l.length : r && (s = t, c(r)) 
    } 
    return this 
} 

から得るものです私は、「データ」を「通知」の変化により、このリンクをたどって、「メッセージ」を「ボディ」。しかし、私は同じエラーを持っています。 https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#http_post_request

私の間違いはどこですか? :(ありがとう

+0

あなたはFCMレポートのエラーを表示することができます? –

+0

@KanishkDudejaそれはなんですか? –

答えて

1

認可「ヘッダ」&通知データが文字列として渡される必要があるの一部である必要があります下にしてみてください!。それは動作します:)

 $.ajax({   
      type : 'POST', 
      url : "https://fcm.googleapis.com/fcm/send", 
      headers : { 
       Authorization : 'key=' + '<key>' 
      }, 
      contentType : 'application/json', 
      dataType: 'json', 
      data: JSON.stringify({"to": "<instance ID>", "notification": {"title":"Test","body":"Test"}}), 
      success : function(response) { 
       console.log(response); 
      }, 
      error : function(xhr, status, error) { 
       console.log(xhr.error);     
      } 
     }); 
+2

これは、書式設定を修正し、それを動作させるために何をしているのかを説明した場合には、良い答えとなります。 – Shadow

+0

何が違うのですか?回答:承認は「ヘッダ」に含める必要があり、通知データは文字列として渡す必要があります。完全に動作します –

+0

喜んで聞いてください - その情報を含む回答を編集してください:) – Shadow

関連する問題