2016-10-06 44 views
0

我試圖通過Firebase向我的設備發送消息。但是我得到了錯誤。我在先進的REST客戶端上成功測試了它。這是來自其他客戶端的消息通過ajax向firebase雲消息發送請求?

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