【问题标题】:Error sending firebase push Notification from Angular从 Angular 发送 Firebase 推送通知时出错
【发布时间】:2020-11-20 06:45:20
【问题描述】:

我制作了一个 Flutter 应用程序,每次更新我的数据库时都会收到通知。

使用 Angular 单独处理数据库。但是,当我从 Angular 向 Firebase 发出 POST 请求以发送通知时,我在记录响应时收到以下错误:

代码如下:

export class AppComponent {
  constructor(private CustomService: CustomService) {}
  onButtonClick() {
    this.CustomService.pushNotification().subscribe((Response) => {
      console.log(Response);
    });
  }
}

这是服务代码:

export class CustomService{
  // I also do not know if this is the correct format to send
  myNotification: Object = {
    notification: {
      title: 'Angualr Notification',
      body: 'Displayed from Angular: App is in backgroud or closed!',
    },
    data: {
      title: 'Angular Notification',
      body: 'Displayed From Angular: App is open',
      click_action: 'FLUTTER_NOTIFICATION_CLICK',
    },
    topic: 'all_users',
  };

  FirebaseProjectURL: string "https://console.firebase.google.com/project/dummy-project-123/";

  pushNotification(): Observable<Object> {
    return this.http.post<Object>(this.FirebaseProjectURL, this.myNotification);
  }
}

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    您的请求不正确,您的 URL 似乎也不是指向数据库而是指向项目。 Firebase 数据库存储 URL 通常也看起来像这样:https://databaseName.firebaseio.com。这意味着您应该在您的 firebase 项目中设置一个数据库,然后使用该 URL 来发出请求。

    我建议还阅读更多有关 firebase 数据库如何工作的文档:https://firebase.google.com/docs/database

    【讨论】:

    • 我熟悉数据库,但仍在学习如何处理推送通知(特别是从 Web 发送到移动设备的推送请求)。您可以查看我的回复。
    【解决方案2】:

    所以经过大量搜索,这就是我应该做的:

    export class MyCrudService {
      // Request Body
      myNotification: Object = {
        condition: "('all_users' in topics)",
        notification: {
          title: 'Notification title',
          body: 'Notification message',
        },
        data: {
          title: 'Notification title',
          body: 'Displayed',
          click_action: 'FLUTTER_NOTIFICATION_CLICK',
        },
      };
    
      // Request URL and Header
      FirebaseProjectURL: string = 'https://fcm.googleapis.com/fcm/send';
      headers: HttpHeaders = new HttpHeaders({
        'Content-Type': 'application/json',
        Authorization:'key=AAAA7k...5mHaW', // The full Server key
      });
      options = { headers: this.headers };
    
      pushNotification(): Observable<Object> {
        return this.http.post<Object>(
          this.FirebaseProjectURL,
          this.myNotification,
          this.options
        );
      }
    }
    

    我很幸运地在互联网上找到了“myNotification”,但在文档中找不到关于 POST 请求正文的任何​​内容。

    如果有人有关于 myNotification 应该有的可能“键”的链接,我将不胜感激。

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 2019-11-11
      • 2019-03-18
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多