0
votes

I am using ionic 2

I installed the plugin

HTTP.post to FCM Server not working

cordova plugin add cordova-plugin-fcm

Here is my code

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

someKey;
t;
constructor(public http: Http) {
      this.t="cwJHK1Ma-Mo:APA91bFRpBudeF9QhTosUs6-N7sSEW3D104cUO3M6gHFyTTDEodTWL5VVMbbbvqbhb5zkgRZwkIMC4p7PYbG7Pmfo6vb4IFB50ZTJN0W-2iJoJ-ZUdeBtcvjJ2nImflgUgtNplA210HP"
      this.someKey="AAAAbZuUvR8:APA91bF3-tKjl5FmXx...."
      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": this.t
       };
    let headers: Headers = new Headers({
      'Content-Type': 'application/json',
      'Authorization': 'key='+this.someKey
    });
    let options = new RequestOptions({ headers: headers });

    console.log(JSON.stringify(headers));

      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);
      });
 }

But I am getting this err

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

EXCEPTION: Response with status: 401 OK for URL: https://fcm.googleapis.com/fcm/send

How can i fix this issue.

Kindly advice me,

Thanks.

1

1 Answers

0
votes

Hey man I use the https://firebase.google.com/docs/cloud-messaging/admin/send-messages it's easier and you don't have to handle manually the requests.