简体   繁体   English

Angular fire Messaging 无法接收通知,我该如何解决?

[英]Angular fire Messaging Not able to recieve Notificaation, How Can I Fix this?

I'm not able to receive the notification Please help me to solve the problem.我无法收到通知请帮助我解决问题。 If anyone knows the better solution for the firebase web notification in the ionic / angular web app please help me.如果有人知道离子/角度网络应用程序中 firebase 网络通知的更好解决方案,请帮助我。

FcmService.ts FcmService.ts

import { Injectable } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/messaging';
import { AngularFireFunctions } from '@angular/fire/functions';
import { ToastController } from '@ionic/angular';
import { tap } from 'rxjs/operators';

// Import firebase to fix temporary bug in AngularFire
import * as app from 'firebase';
import { Observable, BehaviorSubject } from 'rxjs';
import { AngularFirestore } from '@angular/fire/firestore';


@Injectable({
  providedIn: 'root'
})
export class FcmService {
  token;

  currentMessage = new BehaviorSubject(null);
  messages$:Observable<any>;

  constructor(
    private db:AngularFirestore,
    private afMessaging: AngularFireMessaging,
    private fun: AngularFireFunctions,
    private toastController: ToastController
  ) {

    this.afMessaging.messaging.subscribe(
      (_messaging) => {
        _messaging.onMessage = _messaging.onMessage.bind(_messaging);
        _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
      }
    )

  }


  getPermission() {
    this.afMessaging.requestToken
      .subscribe(
        (token) => { 
          this.token = token

          console.log('Permission granted! Save to the server!', token); },
        (error) => { console.error(error); },  
      );
  }




  sub(topic) {
    this.fun
      .httpsCallable('subscribeToTopic')({ topic, token: this.token })
      .pipe(tap(_ => this.makeToast(`subscribed to ${topic}`)))
      .subscribe();
  }

  unsub(topic) {
    this.fun
      .httpsCallable('unsubscribeFromTopic')({ topic, token: this.token })
      .pipe(tap(_ => this.makeToast(`unsubscribed from ${topic}`)))
      .subscribe();
  }

  async makeToast(message) {
    const toast = await this.toastController.create({
      message,
      duration: 5000,
      position: 'top',
      showCloseButton: true,
      closeButtonText: 'dismiss'
    });
    toast.present();
  }


  updateData(data){
    this.db.collection('discont').add(data)
  }


  receiveMessage() {
    this.afMessaging.messages.subscribe(
      (payload) => {
        console.log("new message received. ", payload);
        this.currentMessage.next(payload);
      })
  }

    enter code here**strong text**

}

this is the page where I called the fcmService这是我调用 fcmService 的页面

home.ts主页.ts

   import { FcmService } from '../services/fcm.service';

         constructor(
    private fcm: FcmService,
    ){}

     ngOnInit(){
          this.fcm.receiveMessage()
          this.messageData = this.fcm.currentMessage
    }

I have Imported this file to the 'src' directory firebase-messaging-sw.js我已将此文件导入到“src”目录中 firebase-messaging-sw.js

importScripts('https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.5.0/firebase-messaging.js');

firebase.initializeApp({
    'messagingSenderId': '884242388518'
});

const messaging = firebase.messaging();

angular.json角度.json

"assets": [

              "src/firebase-messaging-sw.js",
              {

                "glob": "**/*",
                "input": "src/assets",
                "output": "assets"
              }
]

I'm not able to receive the notification Please help me to solve the problem.我无法收到通知 请帮我解决问题。 If anyone knows the better solution for the firebase web notification in the ionic / angular web app please help me.如果有人知道 ionic / angular web 应用程序中 firebase web 通知的更好解决方案,请帮助我。

FcmService.ts FcmService.ts

import { Injectable } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/messaging';
import { AngularFireFunctions } from '@angular/fire/functions';
import { ToastController } from '@ionic/angular';
import { tap } from 'rxjs/operators';

// Import firebase to fix temporary bug in AngularFire
import * as app from 'firebase';
import { Observable, BehaviorSubject } from 'rxjs';
import { AngularFirestore } from '@angular/fire/firestore';


@Injectable({
  providedIn: 'root'
})
export class FcmService {
  token;

  currentMessage = new BehaviorSubject(null);
  messages$:Observable<any>;

  constructor(
    private db:AngularFirestore,
    private afMessaging: AngularFireMessaging,
    private fun: AngularFireFunctions,
    private toastController: ToastController
  ) {

    this.afMessaging.messaging.subscribe(
      (_messaging) => {
        _messaging.onMessage = _messaging.onMessage.bind(_messaging);
        _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
      }
    )

  }


  getPermission() {
    this.afMessaging.requestToken
      .subscribe(
        (token) => { 
          this.token = token

          console.log('Permission granted! Save to the server!', token); },
        (error) => { console.error(error); },  
      );
  }




  sub(topic) {
    this.fun
      .httpsCallable('subscribeToTopic')({ topic, token: this.token })
      .pipe(tap(_ => this.makeToast(`subscribed to ${topic}`)))
      .subscribe();
  }

  unsub(topic) {
    this.fun
      .httpsCallable('unsubscribeFromTopic')({ topic, token: this.token })
      .pipe(tap(_ => this.makeToast(`unsubscribed from ${topic}`)))
      .subscribe();
  }

  async makeToast(message) {
    const toast = await this.toastController.create({
      message,
      duration: 5000,
      position: 'top',
      showCloseButton: true,
      closeButtonText: 'dismiss'
    });
    toast.present();
  }


  updateData(data){
    this.db.collection('discont').add(data)
  }


  receiveMessage() {
    this.afMessaging.messages.subscribe(
      (payload) => {
        console.log("new message received. ", payload);
        this.currentMessage.next(payload);
      })
  }

    enter code here**strong text**

}

this is the page where I called the fcmService这是我调用 fcmService 的页面

home.ts家.ts

   import { FcmService } from '../services/fcm.service';

         constructor(
    private fcm: FcmService,
    ){}

     ngOnInit(){
          this.fcm.receiveMessage()
          this.messageData = this.fcm.currentMessage
    }

I have Imported this file to the 'src' directory firebase-messaging-sw.js我已将此文件导入到“src”目录 firebase-messaging-sw.js

importScripts('https://www.gstatic.com/firebasejs/7.5.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.5.0/firebase-messaging.js');

firebase.initializeApp({
    'messagingSenderId': '884242388518'
});

const messaging = firebase.messaging();

angular.json angular.json

"assets": [

              "src/firebase-messaging-sw.js",
              {

                "glob": "**/*",
                "input": "src/assets",
                "output": "assets"
              }
]

Firebase has stated that messaging will not work over http. Firebase 已声明消息传递将无法通过 http 工作。 If you are serving a web app locally with a specific ip address you may need to use the flag --ssl otherwise make sure that your build is being hosted with an ssl certificate.如果您使用特定的 IP 地址在本地提供 Web 应用程序,您可能需要使用标志--ssl否则请确保您的构建是使用 ssl 证书托管的。

Github Issue Github 问题

Check towards the end for the reference.检查最后以获取参考。

Also, I believe the bug has been fixed so you might want to remove the "bug fix" snippet此外,我相信该错误已得到修复,因此您可能希望删除“错误修复”片段

// No longer needed
_messaging.onMessage = _messaging.onMessage.bind(_messaging);
_messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);

and handle the result regularly.并定期处理结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 8,如何发送从 http 调用中收到的用户信息,并订阅另一个组件? - Angular 8, How can I send the userinformation I recieve from a http call in a service with subscribe to another component? 另一个应用程序是发送我的Url params如何以角度接收它? - Another application is sending on my Url params How can i recieve it in angular? 如何解决这个 Angular 路由问题? - How can I fix this Angular routing problem? 如何让我的 Angular BehaviorSubject 触发? - How can I get my Angular BehaviorSubject to fire? 如何使用@angular/fire 连接两个数据库? - How can I connect two databases with @angular/fire? 错误在./node_modules/@angular/fire/messaging/messaging.js中 - ERROR in ./node_modules/@angular/fire/messaging/messaging.js 无法在我的 Angular 项目中安装 @angular/fire - Not able to install @angular/fire in my angular project 如何从 Angular 服务接收带有参数的数据 - How to recieve data with parameter from Angular service 如何单击 Bootstrap 4 输入组内的 HTML 输入来触发绑定的 Angular 事件 - How can I click an HTML input inside of a Bootstrap 4 input-group to fire a bound Angular event 未处理的承诺拒绝:this._next 不是函数:@angular/fire/messaging 中的区域 - Unhandled Promise rejection: this._next is not a function : Zone in @angular/fire/messaging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM