向FCM发送POST请求,不接受Angular中的授权标头 [英] sending POST request to FCM not accepting authorization header in angular

查看:191
本文介绍了向FCM发送POST请求,不接受Angular中的授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试使用FCM使用Firebase云消息传递将通知从Angular Web应用程序发送到移动应用程序.

So I'm trying to use FCM to send notifications from Angular web application to mobile app using Firebase cloud messaging..

但是我收到401响应请求缺少身份验证密钥", 我在POSTMAN上测试了相同的请求,并且可以正常工作,但在角度上却不能.

but I'm getting 401 response "the request was missing an Authentication Key", I tested the the same request on POSTMAN and it was working but on angular it is not..

这是我的代码:

pushNotification(title , body){


this.http.post('https://fcm.googleapis.com/fcm/send' , {
  header: {
    'Content-Type':  'application/json',
    'Authorization' :  'key='+ environment.cloudMessages.serverKey
  }, 
  body: {
    "notification": {
      "title": title   ,
      "body":  body,
      "mutable_content": true      
      },
      "priority":"high",
  }
}).subscribe(res => {
  console.log(res);
})

}
}

我的密钥是来自云消息传递的服务器密钥.

Where my key is the server key from cloud messaging..

推荐答案


//Interfaces containing payload
export interface FCMData {
  body: string;
  title: string;
}

export interface FCMPayload {
  to: string;
  collapse_key: string;
  data: FCMData;
}

// this method sends the notification
  async sendNotifications(message: string) {

    const headers: HttpHeaders = new HttpHeaders({
      'Content-Type': 'application/json',
      Authorization: 'key=' + AppSettings.SERVER_TOKEN
    });
 

      const data: FCMData = {
        body: message,
        title: 'Sup!'
      };
      const fcmPayload: FCMPayload = {
        to: reg.token,
        collapse_key: 'type_a',
        data
      };

      this.http.post('https://fcm.googleapis.com/fcm/send' , fcmPayload , {headers}).subscribe(res => {
        console.log(res);
      });
    );
  }

要在Kotlin中接收数据有效载荷, 在您的FirebaseMessagingService内部->覆盖onMessageReceived的乐趣

To Receive the Data Payload in Kotlin, Inside your FirebaseMessagingService -> override fun onMessageReceived


 if (remoteMessage.data.isNotEmpty()) {
    const body= (remoteMessage.data as ArrayMap).valueAt(0)
 }

这篇关于向FCM发送POST请求,不接受Angular中的授权标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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