1

I'm trying to send a message to my device via firebase. But I got error. I tested it successfully on advance REST client. This is message from rest client

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"
  }
}    

Based on that, I made my javascript code. Don't worry about gritter, it's other library, and it works normally.

$.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'
                });
            }
        });

And this is what I get back from 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 
}

I followed this link by change "notification" to "data", and "body" to "message". But I got the same error. https://firebase.google.com/docs/cloud-messaging/android/topic-messaging#http_post_request

Where is my mistake? :( Thank you!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ngo Kim Huynh
  • 69
  • 3
  • 16

1 Answers1

5

Authorization needs to be part of 'headers' & notification data needs to be passed as a string. Try Below: It works :)

     $.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);                   
            }
        });
Tim
  • 1,606
  • 2
  • 21
  • 42
Yasser Parkar
  • 66
  • 1
  • 4
  • 2
    This could be a good answer if you fixed your formatting and explained what you are doing differently to make it work. – Shadow Jun 27 '17 at 04:55
  • Whats done differently? Answer: Authorization needs to be part of 'headers' & notification data needs to be passed as string. Works perfectly – Yasser Parkar Jul 04 '17 at 18:58
  • Glad to hear it - please edit your answer to contain that information :) – Shadow Jul 04 '17 at 23:29
  • 1
    When dataType: 'json' is included, I'm getting error status parsererror. But when this is not included, I am getting success Error=MissingRegistration. Has anybody encounter this problem? What could cause this? – Uroš Podkrižnik Oct 16 '17 at 09:22
  • When I try I get `Cannot find field` and it says `Unknown Name \"to"\ ` and also for `\"notification"\ ` – Daniel Jackson Jan 17 '19 at 17:43