简体   繁体   English

Firebase 服务人员和 Angular

[英]Firebase service worker and Angular

I want to show a notification with action buttons.我想显示带有操作按钮的通知。
This is only possible with the serviceWorker.showNotification API.这仅适用于 serviceWorker.showNotification API。

I have an Angular 9 application with the firebase-messaging-sw.js service worker as mentioned in the Firebase Messaging docs.我有一个 Angular 9 应用程序和firebase-messaging-sw.js服务工作者,如 Firebase 消息传递文档中所述。

The service worker is used when the page is in the background.当页面在后台时使用服务工作者。
In the firebase-messaging-sw.js I send the notification with:firebase-messaging-sw.js我发送通知:

self.registration.showNotification(notificationTitle,
        notificationOptions);

If the app is in the foreground, I need to show the same notification with actions buttons.如果应用程序在前台,我需要使用操作按钮显示相同的通知。 So I need in the Angular Service TypeScript file to access also the same firebase service worker, but self.registration does of course not work.所以我需要在 Angular 服务 TypeScript 文件中访问同样的 firebase 服务工作者,但self.registration当然不起作用。

How can I access the already registered Firebase service worker to show a notification with action buttons in my Angular TypeScript file?如何访问已注册的 Firebase 服务人员以在我的 Angular TypeScript 文件中显示带有操作按钮的通知?

create firebase-messaging-sw.js Push messaging requires a service worker.创建 firebase-messaging-sw.js 推送消息需要服务工作者。 This allows your app to detect new messages, even after the app has been closed by the user.这允许您的应用程序检测新消息,即使在用户关闭应用程序之后也是如此。 and create this file in the same directory of manifest.json file which is in src/ directory.并在 src/ 目录下的 manifest.json 文件的同一目录中创建此文件。

Note:- Before importing the below script you need to check the latest version it's better if you import the latest version of the CDN link, so here i importing 7.6.0 version links.注意:- 在导入以下脚本之前,您需要检查最新版本,最好导入最新版本的 CDN 链接,所以这里我导入 7.6.0 版本的链接。

importScripts('https://www.gstatic.com/firebasejs/7.6.0/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.6.0/firebase-messaging.js');
firebase.initializeApp({
    apiKey: “from firebase config”,
    authDomain: “from firebase config”,
    databaseURL: “from firebase config”,
    projectId: “from firebase config”,
    storageBucket: “from firebase config”,
    messagingSenderId: “from firebase config”,
    appId: “from firebase config”,
    measurementId: “from firebase config”
});
const messaging = firebase.messaging();

we need to register these files in angular-cli.json我们需要在 angular-cli.json 中注册这些文件

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

And you can do reference below code for service code.您可以参考下面的服务代码代码。

import { Injectable } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/messaging';
import { BehaviorSubject } from 'rxjs'
@Injectable()
export class MessagingService {
    currentMessage = new BehaviorSubject(null);
    constructor(private angularFireMessaging: AngularFireMessaging) {
    this.angularFireMessaging.messaging.subscribe(
    (_messaging) => {
    _messaging.onMessage = _messaging.onMessage.bind(_messaging);
    _messaging.onTokenRefresh = _messaging.onTokenRefresh.bind(_messaging);
    }
    )
    }
    requestPermission() {
    this.angularFireMessaging.requestToken.subscribe(
    (token) => {
    console.log(token);
    },
    (err) => {
    console.error('Unable to get permission to notify.', err);
    }
    );
    }
    receiveMessage() {
    this.angularFireMessaging.messages.subscribe(
    (payload) => {
    console.log("new message received. ", payload);
    this.currentMessage.next(payload);
    })
    }
}

let's understand its functions requestPermission() : Browser/ device will ask to user for permission to receive notification.让我们了解它的功能requestPermission() :浏览器/设备将要求用户允许接收通知。 After permission is granted by the user, firebase will return a token that can use as a reference to send a notification to the browser.receiveMessage() : This function will be triggered when a new message has received.用户授予权限后,firebase 将返回一个令牌,可用作向浏览器发送通知的参考。receiveMessage browser.receiveMessage() :当收到新消息时,将触发此 function。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM