HTTP.post 到 FCM 服务器不起作用 [英] HTTP.post to FCM Server not working

查看:15
本文介绍了HTTP.post 到 FCM 服务器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 HTTP 原生模块的 Ionic 2 向 FCM 服务器发出发布请求用于推送通知.我使用的代码是:

I am using Ionic 2 with HTTP native module to make a post request to FCM server for push notifications. The code I am using is:

        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 的 post 请求,它可以完美地传递推送通知.

I tried the post request with Postman, it works perfectly fine delivering push notifications.

Postman 的代码是:

The code with Postman is:

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 发布请求的标头中,但它适用于邮递员.

  1. I am unable to add content-type to the header in the HTTP post request, but it works with postman.

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

If I try to add a function(response) { to get the response from the server, it gives me an error. The documentation for the same is at https://github.com/wymsee/cordova-HTTP

推荐答案

你为什么使用 HTTP 原生模块?Angular 有一个内置的 Http.

why are you using HTTP native module? Angular has a built in Http.

使用这个(在你的 NgModule 中从 @angular/http 导入 HttpModule)你可以调用

Using this one (importing HttpModule from @angular/http in your NgModule) you can just call

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

    });

这篇关于HTTP.post 到 FCM 服务器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆