HTTP.post到FCM服务器无法正常工作

时间:2016-12-08 07:20:32

标签: http push-notification ionic2 cordova-plugins firebase-cloud-messaging

我正在使用带有HTTP native module的Ionic 2向FCM服务器发出推送通知请求。我正在使用的代码是:

        HTTP.post(
          "https://fcm.googleapis.com/fcm/send",
          {
            "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",
          },
          {
            Authorization: {
              key: "AUTHORIZATION KEY HERE"
           }
          })

它给了我一个错误:

Unimplemented console API: Unhandled Promise rejection:
Unimplemented console API: Error: Uncaught (in promise): [object Object]

我尝试了Postman的发布请求,它可以很好地发送推送通知。

Postman的代码是:

POST /fcm/send HTTP/1.1
Host: fcm.googleapis.com
Content-Type: application/json
Authorization: key=Authorisation Key
Cache-Control: no-cache
Postman-Token: 446e253b-179a-d19b-21ea-82d9bb5d4e1c

{
  "to": "Device Token",
  "data": {
    "hello": "This is a Firebase Cloud Messagin  hbhj g Device Gr new v Message!",
   }
     "notification":{
    "title":"Notification title",
    "body":"Notification body",
    "sound":"default",
    "click_action":"FCM_PLUGIN_ACTIVITY",
    "icon":"fcm_push_icon"
  },
}

问题:

  1. 我无法将content-type添加到HTTP帖子请求中的标头,但它适用于邮递员。

  2. 如果我尝试添加function(response) {来获取服务器的响应,则会给我一个错误。该文档的文档位于https://github.com/wymsee/cordova-HTTP

1 个答案:

答案 0 :(得分:0)

为什么使用HTTP native module? Angular内置Http

使用此版本(从HttpModule @angular/http导入NgModule),您只需致电

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({
  '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);
  });
}
push.on('notification', (data) => {
 if (data.additionalData.foreground) {
          // if application open, show popup
          let confirmAlert = this.alertCtrl.create({
            title: data.title,
            message: data.message,
            buttons: [{
              text: 'Ignore',
              role: 'cancel'
            }, {
              text: 'Go to',
              handler: () => {
                //TODO: Your logic here
                this.navCtrl.setRoot(EventsPage, {message: data.message});
              }
            }]
          });
          confirmAlert.present();
        } else {
          //if user NOT using app and push notification comes
          //TODO: Your logic on click of push notification directly
          this.navCtrl.setRoot(EventsPage, {message: data.message});
        }
      });
      push.on('error', (e) => {
        alert(e);
      });

    });