简体   繁体   English

通过 angular 应用收听 firebase 云消息时出现问题

[英]problem listening to firebase cloud messaging via angular app

I am able to get the token on angular and successfully sent a message via node.js local server but I cannot receive a message on angular app.我能够在 angular 上获取令牌并通过 node.js 本地服务器成功发送消息,但我无法在 angular 应用程序上收到消息。

Versions: -Angular 14 -Firebase Cloud Messaging API (V1) not Cloud Messaging API (Legacy)版本:-Angular 14 -Firebase Cloud Messaging API (V1) 不是 Cloud Messaging API (Legacy)

instructions followed: https://github.com/angular/angularfire/blob/master/docs/messaging/messaging.md说明如下: https://github.com/angular/angularfire/blob/master/docs/messaging/messaging.md

app.module应用模块

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { AngularFireModule } from '@angular/fire/compat';
import { AngularFireMessagingModule } from '@angular/fire/compat/messaging';

import { environment } from 'src/environments/environment';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireMessagingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts应用程序组件.ts

import { Component, OnInit } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/compat/messaging';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'notif-boilerplate-web';

  constructor(private afMessaging: AngularFireMessaging) { }


  ngOnInit() {
    this.requestPermission()
    this.listen()
  }

  requestPermission() {
    this.afMessaging.requestToken.subscribe({
      next: token => {
        console.log(token)
      }
    })
  }

  listen() {
    this.afMessaging.messages
      .subscribe((message) => { console.log(message); });
  }
}

index.html index.html

<!doctype html>
<html lang="en">
<head>
  <link rel="manifest" href="./manifest.json">
  <meta charset="utf-8">
  <title>NotifBoilerplateWeb</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <app-root></app-root>
</body>
</html>

firebase-messaging-sw.js firebase-messaging-sw.js

importScripts("https://www.gstatic.com/firebasejs/9.15.0/firebase-app-compat.js");
importScripts("https://www.gstatic.com/firebasejs/9.15.0/firebase-messaging-compat.js");

firebase.initializeApp({
    apiKey: "***************",
    authDomain: "*********.firebaseapp.com",
    projectId: "**************",
    storageBucket: "***********.appspot.com",
    messagingSenderId: "*************",
    appId: "*************",
    measurementId: "*****************",
    vapidKey: "******************-vzYU5mGBnfis-ADYy7k9V88yTg"
})

const messaging = firebase.messaging();

console.log(messaging)

messaging.onMessage((payload) => {
    console.log('Message received. ', payload);
    // ...
});

manifest.json清单.json

{
    "gcm_sender_id": "10****************"
}

angular.json angular.json

            "assets": [
              "src/favicon.ico",
              "src/assets",
              "src/firebase-messaging-sw.js",
              "src/manifest.json"
            ]

在此处输入图像描述

https://firebase.google.com/docs/cloud-messaging/js/send-multiple#receive_and_handle_topic_messages https://firebase.google.com/docs/cloud-messaging/js/send-multiple#receive_and_handle_topic_messages

Receive and handle topic messages接收和处理主题消息

The behavior of messages differs depending on whether the page is in the foreground (has focus), or in the background, hidden behind other tabs, or completely closed.消息的行为取决于页面是在前台(有焦点),还是在后台,隐藏在其他选项卡后面,或者完全关闭。 In all cases the page must handle the onMessage callback, but in background cases you may also need to handle onBackgroundMessage or configure the display notification to allow the user to bring your web app into the foreground.在所有情况下,页面都必须处理 onMessage 回调,但在后台情况下,您可能还需要处理 onBackgroundMessage 或配置显示通知以允许用户将您的 web 应用程序带到前台。

messaging.onBackgroundMessage(function (payload) {
    console.log('Received background message ', payload);
});

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

相关问题 使用 React Native Firebase 消息应用程序在 OnSnapshot 上收听消息时出现问题 - Problem with listening to messages OnSnapshot with a React Native Firebase Messaging App 在 Firebase 云消息传递中,React 本机应用程序未通过 TOPIC 接收通知 - React native app not receiving notifications via TOPIC in Firebase cloud messaging Firebase 云消息在 Android 和 Flutter 应用程序上不工作 - Firebase Cloud Messaging not working on Android with Flutter app 未收到通过 iOS 应用程序中的 firebase 云功能发送的推送通知,尽管我从消息控制台获取 - Not receiving push notification sent via firebase cloud functions in iOS app though I get the from messaging console Firebase 云消息令牌 - Firebase Cloud Messaging Token Firebase 云消息 PHP - Firebase Cloud Messaging PHP 如何通过firebase云消息发送定时通知到flutter app - how to send scheduled notification to flutter app through firebase cloud messaging 在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递 - Sending FCM Push Notification in Kotlin Client App - Firebase Cloud Messaging Firebase云消息在Android应用程序中的主题订阅使用Delphi 11 - Topic subscription of Firebase Cloud Messaging in Android app using Delphi 11 Firebase 云消息 - getMessaging 不是 function - Firebase Cloud Messaging - getMessaging is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM