I have a javascript function like this:
function sendNotification(){
var url = 'https://fcm.googleapis.com/fcm/send';
var contentType = 'application/json';
var data = {"notification":
{
"title": 'Hello'
},
"to": '/topics/breaking_news'
};
var xhr = new XMLHttpRequest();
xhr.onload = function(){
alert (xhr.responseXML);
}
xhr.open("POST", url, false);
xhr.setRequestHeader('Content-Type', contentType);
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Access-Control-Allow-Methods', 'POST');
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.log(xhr.responseXML);
}
};
xhr.setRequestHeader('Authorization', 'key=XXXXXXXX');
xhr.send();
}
But the call to send returns 400. I am a bit rusty not having programmed for a while, so if it something trivial, I apologize. Been breaking my head on this for two days now.
I did the same using chrome extension. Here is my js code:-
function send_notification(){
// Set up an asynchronous AJAX POST request
var hr = new XMLHttpRequest();
var url="https://fcm.googleapis.com/fcm/send";
hr.open("POST", url, true);
hr.setRequestHeader('Content-Type','application/json');
hr.setRequestHeader('Authorization','key=your-server-key-here');
var data='{"to" : "your-device-token-here","notification" : {"body" : "great
match!","title" : "Portugal vs. Denmark","content_available" :
true,"priority" : "high"},"data" : {"body" : "great match!","title" :
"Portugal vs. Denmark","content_available" : true,"priority" : "high"}}';
// Handle request state change events
hr.onreadystatechange = function() {
// If the request completed
if (hr.readyState == 4) {
if (hr.status == 200) {
// success
resp=JSON.parse(hr.responseText);
console.log('Response Sent with params '+data );
} else {
// Show what went wrong
console.log('Something went wrong '+ hr.responseText);
}}}; hr.send(data);}
And manifest.json
{
"manifest_version": 2,
"name": "Firebase Auth in Chrome Extension Sample",
"description": "This sample shows how to authorize Firebase in a Chrome
extension using a Google account.",
"version": "2.1",
"icons": {
"128": "firebase.png"
},
"browser_action": {
"default_icon": "firebase.png",
"default_popup": "credentials.html"
},
"background": {
"page": "background.html"
},
"permissions": [
"identity"
],
"content_security_policy":"script-src 'self' https://www.gstatic.com/
https://*.firebaseio.com https://www.googleapis.com; object-src 'self'",
"oauth2": {
"client_id": "oauth-client-id",
"scopes": [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile"
]
},
"key": "token-here"
}
This should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.