具有FCM推送通知的Ionic 2

时间:2017-05-31 12:14:19

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

我正在使用离子2

我安装了插件

HTTP.post to FCM Server not working

cordova plugin add cordova-plugin-fcm

这是我的代码

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

但我得到这个错误

控制台告诉错误该请求缺少一个验证密钥(FCM令牌)。请参阅" Authentification" FCM文档的详细信息,请访问firebase.google.com/docs/cloud-messaging/server。错误401

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

我该如何解决这个问题。

请建议我,

感谢。

1 个答案:

答案 0 :(得分:0)

嘿,我使用https://firebase.google.com/docs/cloud-messaging/admin/send-messages它更容易,您不必手动处理请求。