我根据链接实现了 fcm
令牌是根据请求生成的。邮递员显示 200 ok 消息发送。但我不确定该服务是否正在侦听新消息,因为新消息未显示在控制台上。 版本详情
Angular CLI: 8.3.29
Node: 14.16.0
OS: win32 x64
Angular: 8.2.14
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.803.29
@angular-devkit/build-angular 0.803.29
@angular-devkit/build-optimizer 0.803.29
@angular-devkit/build-webpack 0.803.29
@angular-devkit/core 8.3.29
@angular-devkit/schematics 8.3.29
@angular/cli 8.3.29
@angular/fire 5.2.3
@angular/http 7.2.16
@ngtools/webpack 8.3.29
@schematics/angular 8.3.29
@schematics/update 0.803.29
rxjs 6.6.7
typescript 3.5.3
webpack 4.39.2
包
{
"name": "ang7",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve --open --port 5004",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e",
"crypto": "ng crypto"
},
"private": true,
"dependencies": {
"@angular-devkit/build-webpack": "~0.803.29",
"@angular/animations": "~8.2.14",
"@angular/common": "~8.2.14",
"@angular/compiler": "~8.2.14",
"@angular/core": "~8.2.14",
"@angular/fire": "^5.2.3",
"@angular/forms": "~8.2.14",
"@angular/http": "^7.2.16",
"@angular/platform-browser": "~8.2.14",
"@angular/platform-browser-dynamic": "~8.2.14",
"@angular/router": "~8.2.14",
"@ng-bootstrap/ng-bootstrap": "^8.0.1",
"@types/crypto-js": "^4.0.1",
"chart.js": "^2.9.3",
"core-js": "^2.5.4",
"crypto-js": "^4.0.0",
"decode-html-entities": "^1.1.10",
"firebase": "^7.24.0",
"html2canvas": "^1.0.0-rc.7",
"jwt-decode": "^2.2.0",
"ng-marquee": "^9.2.0",
"ng2-charts": "^2.2.3",
"ngx-owl-carousel": "^2.0.7",
"ngx-pagination": "^4.1.0",
"ngx-spinner": "^7.2.0",
"ngx-toastr": "^9.1.1",
"node-sass": "^4.14.1",
"rxjs": "~6.6.7",
"tslib": "^1.9.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.803.29",
"@angular/cli": "^8.3.29",
"@angular/compiler-cli": "^8.2.14",
"@angular/language-service": "~8.2.14",
"@types/jasmine": "~2.8.8",
"@types/jasminewd2": "~2.0.3",
"@types/node": "~8.9.4",
"codelyzer": "^5.0.1",
"jasmine-core": "~2.99.1",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~3.1.1",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~1.1.2",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.5.3"
}
}
firebase-messaging-sw.js
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({
config
});
const messaging = firebase.messaging();
messaging.service.ts
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(userid:any) {
this.angularFireMessaging.requestToken.subscribe(
(token) => {
console.log(token);
console.log(userid);
},
(err) => {
console.error('Unable to get permission to notify.', err);
}
);
}
receiveMessage() {
this.angularFireMessaging.messages.subscribe(
(payload:any) => {
console.log("new message received. ", payload);
this.currentMessage.next(payload);
})
}
}
环境.ts
export const environment = {
production: false,
// URL of development API
apiUrl: 'api',
firebase: {
config
}
};
app.module.ts
AngularFireModule.initializeApp(environment.firebase),
AngularFireDatabaseModule,
AngularFireAuthModule,
AngularFireMessagingModule,
app.component.ts
ngOnInit()
{
this.messageService.requestPermission(this.loggedInUserId);
this.messageService.receiveMessage()
this.message = this.messageService.currentMessage;
}