【问题标题】:Ionic 2 with FCM Push notifications带有 FCM 推送通知的 Ionic 2
【发布时间】:2026-01-24 08:10:01
【问题描述】:

我正在使用离子 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 令牌)。请参阅 firebase.google.com/docs/cloud-messaging/server 上 FCM 文档的“身份验证”部分。错误 401

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

我该如何解决这个问题。

请多多指教,

谢谢。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】: