【发布时间】:2020-05-29 02:59:21
【问题描述】:
我已经在这个平台上审查并实施了一些已经提出和回答的问题。我能够在屏幕上推送默认弹出窗口,但是我无法摆脱如何在我的自定义列表中添加这些新的推送通知并在 UI 中显示给最终用户。我想收集新的通知并将其存储在我的应用程序变量中,以便我可以解决它并向用户显示所有通知。
下面是我实现的代码。
Package.json
"dependencies": {
"@agm/core": "^1.0.0",
"@angular-devkit/core": "^8.3.16",
"@angular-devkit/schematics": "^8.3.16",
"@angular/animations": "^8.2.12",
"@angular/cdk": "^8.2.3",
"@angular/common": "^8.2.13",
"@angular/compiler": "^8.2.12",
"@angular/core": "^8.2.12",
"@angular/forms": "^8.2.12",
"@angular/platform-browser": "^8.2.12",
"@angular/platform-browser-dynamic": "^8.2.12",
......
"@angular/fire": "^5.2.3",
"@angular/platform-server": "8.0.0",
"@nguniversal/module-map-ngfactory-loader": "8.0.0-rc.1",
"aspnet-prerendering": "^3.0.1",
"firebase": "^7.5.0"
},
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');
// Initialize the Firebase app in the service worker by passing in the
firebase.initializeApp({
apiKey: 'xyz',
authDomain: 'xyz',
databaseURL: 'xyz"',
projectId: 'xyz',
storageBucket: 'xyz',
messagingSenderId: '168382776603',
appId: '1:168382776603:web:65c285a048d740401a3fbe'
});
// Retrieve an instance of Firebase Messaging so that it can handle background
const messaging = firebase.messaging();
app.module.ts
imports: [
FormsModule,
BrowserModule,
AppRoutingModule,
.....,
AngularFireModule.initializeApp({
apiKey: 'xyz',
authDomain: 'xyz',
databaseURL: 'xyz"',
projectId: 'xyz',
storageBucket: 'xyz',
messagingSenderId: '168382776603',
appId: '1:168382776603:web:65c285a048d740401a3fbe'
}),
AngularFireMessagingModule,
]
FirebasePushNotificationService.ts
export class FirebasePushNotificationService {
constructor(private angularFireMessaging: AngularFireMessaging) {
this.angularFireMessaging.messaging.subscribe(
(messagingContext: any) => {
messagingContext.onMessage = messagingContext.onMessage.bind(messagingContext);
messagingContext.onTokenRefresh = messagingContext.onTokenRefresh.bind(messagingContext);
});
}
currentMessage = new BehaviorSubject(null);
requestPermission = () => this.angularFireMessaging.requestToken;
removeToken = (token: string) => this.angularFireMessaging.deleteToken(token);
receiveMessage = () => this.angularFireMessaging.messages;
}
app.component.ts (onInit):我需要在 receiveMessage() 中调试器
this.firebasePushNotificationService.requestPermission().subscribe(
token => {
console.log('Push Token', token);
},
error => {
console.log('Push Error', error);
}
);
this.firebasePushNotificationService.receiveMessage().subscribe(message => {
**// I am not able to get debugger here**
console.log(message);
});
【问题讨论】:
标签: angular firebase push-notification