【问题标题】:HTTP.post to FCM Server not workingHTTP.post 到 FCM 服务器不工作
【发布时间】:2016-12-08 07:20:31
【问题描述】:

我正在使用带有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

【问题讨论】:

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


    【解决方案1】:

    你为什么使用HTTP native module? Angular 有一个内置的Http

    使用这个(从@angular/http 导入HttpModule 在你的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);
          });
    
        });
    

    【讨论】:

    • 它给了我例外:响应状态:401 OK,根据文档是无效密钥...但检查了两次 headers.append('Authorization', 'key=my authentication key here' );
    • 您能否检查您的请求中的request headers 是否包含正确的值?
    • 它以红色显示“to”键 ,, ..... { "notification": { "title": "Notification title", "body": "Notification body", "sound" : "default", "click_action": "FCM_PLUGIN_ACTIVITY", "icon": "fcm_push_icon" }, "data": { "hello": "这是 Firebase Cloud Messagin hbhj g Device Gr new v Message!" }, "to": "这部分在解析视图中显示为红色" }
    • 我也尝试过使用“to”主题:“/topics/cooking”仍然显示 401 错误
    • **'key='+someKey 和 'key=someKey' 一样吗? ** 因为控制台告诉我“请求缺少身份验证密钥(FCM 令牌)。请参阅 FCM 文档的“身份验证”部分,firebase.google.com/docs/cloud-messaging/server。错误 401”,我仍然尝试使用旧服务器密钥不工作:(
    猜你喜欢
    • 2017-07-06
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 2015-05-15
    相关资源
    最近更新 更多