我有一个Angular 15的项目,我想增加FCM到它.
- 我安装了@ANGLING/Fire包.
- 将其导入到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 { provideFirebaseApp, initializeApp } from '@angular/fire/app';
import { getMessaging, provideMessaging } from '@angular/fire/messaging';
@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
AppRoutingModule,
provideFirebaseApp(() =>
initializeApp({
apiKey: 'AIzaSyDsH-k0rqpA19L1kzVG5tBxPjIIdZOaE1A',
authDomain: 'labifysystem.firebaseapp.com',
projectId: 'labifysystem',
storageBucket: 'labifysystem.appspot.com',
messagingSenderId: '648676237340',
appId: '1:648676237340:web:61d9762eddc4291c0c3fd9',
measurementId: 'G-43TJCGGH4H',
})
),
provideMessaging(() => getMessaging()),
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule {}
如何在@ANGLING/FIRE版本^7.5.0中请求权限并获取FCM令牌??
我找到了许多使用这种语法的教程和文章,但它在版本7中不是有效的语法,在@Angel/Fire版本6中可以使用.
import { Component } from '@angular/core';
import { AngularFireMessaging } from '@angular/fire/messaging';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {
title = 'fcm-angular-demo';
message$: Observable<any>;
constructor(private messaging: AngularFireMessaging) {
// Request permission to receive notifications
this.messaging.requestPermission.subscribe(
() => {
console.log('Permission granted');
},
(error) => {
console.log('Permission denied', error);
}
);
// Get the current FCM token
this.messaging.getToken.subscribe(
(token) => {
console.log('Token', token);
// You can send this token to your server and store it there
// You can also use this token to subscribe to topics
},
(error) => {
console.log('Token error', error);
}
);
// Listen for messages from FCM
this.message$ = this.messaging.messages;
this.message$.subscribe(
(message) => {
console.log('Message', message);
// You can display the message or do something else with it
},
(error) => {
console.log('Message error', error);
}
);
}
}