dhruv
dhruv

Reputation: 151

Angular2 HTTP Post Request to FCM Server not working

Using Ionic 2 and angular http i am attempting to make http post request to https://fcm.googleapis.com/fcm/send, the request tested with postman works perfectly fine

this question is a follow-up question to my this question :

HTTP.post to FCM Server not working

console tells error to be The request was missing an Authentification Key (FCM Token). Please, refer to section "Authentification" of the FCM documentation, at firebase.google.com/docs/cloud-messaging/server. Error 401

the code for http request is

import { Http, Headers } from '@angular/http';

......

constructor(public http: Http) { }

sendPushNotification(deviceId: string) {
  let url = 'https://fcm.googleapis.com/fcm/send';
  let body = 
   {
     "notification": {
         "title": "Notification title",
         "body": "Notification body",
         "sound": "default",
         "click_action": "FCM_PLUGIN_ACTIVITY",
         "icon": "fcm_push_icon"
     },
     "data": {
         "hello": "This is a Firebase Cloud Messagin  hbhj g Device Gr new v Message!",
     },
     "to": "device token"
   };

  let headers: Headers = new Headers();
  headers.append('content-type', 'application/json');
  headers.append('Authorization', 'key='+someKey);

  this.http.post(url, body, headers).map(response => {
    return response;
  }).subscribe(data => {
     //post doesn't fire if it doesn't get subscribed to
     console.log(data);
  });
}

headers from chrome console are as follows :

General Headers

Request URL:https://fcm.googleapis.com/fcm/send Request Method:POST Status Code:401 Remote Address:[2404:6800:4009:807::200a]:443

Response headers

access-control-allow-origin:http://localhost:8100 access-control-expose-headers:Content-Encoding,Content-Length,Content-Type,Date,Server alt-svc:quic=":443"; ma=2592000; v="35,34" cache-control:private, max-age=0 content-encoding:gzip content-length:260 content-type:text/html; charset=UTF-8 date:Thu, 08 Dec 2016 12:36:14 GMT expires:Thu, 08 Dec 2016 12:36:14 GMT server:GSE status:401 x-content-type-options:nosniff x-frame-options:SAMEORIGIN x-xss-protection:1; mode=block

Request Headers

Request Headers Provisional headers are shown content-type:application/json Origin:http://localhost:8100 Referer:http://localhost:8100/ User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36

Request Payload {notification: {title: "Notification title", body: "Notification body", sound: "default",…},…} data : {hello: "This is a Firebase Cloud Messaging Device new Message!"} notification : {title: "Notification title", body: "Notification body", sound: "default",…} to : "/topics/cooking"

Upvotes: 0

Views: 2033

Answers (1)

dhruv
dhruv

Reputation: 151

got this working by adding headers as requestoptions ,

let headers: Headers = new Headers({
  'Content-Type': 'application/json',
  'Authorization': 'key='+this.someKey
});
let options = new RequestOptions({ headers: headers });

Upvotes: 2

Related Questions