Я совершенно новичок в ионной и угловой. У меня проблема в моем ионном проекте. Я установил угловые плагины для Firebase и Firebase.
Ошибка:
Default FirebaseApp is not initialized in this process com.xxx.xxx Make sure to call FirebaseApp.initializeApp(Context) first.
Операции с Firestore работают правильно. Но единственная проблема - когда я пытаюсь получить токен устройства, он выдает ошибку.
Вот мой код
app.module.ts
import { NgModule , AfterViewInit } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicModule, IonicRouteStrategy , IonSlides } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AngularFirestore , AngularFirestoreModule } from '@angular/fire/firestore';
import { AngularFireModule } from '@angular/fire';
import { Firebase } from '@ionic-native/firebase/ngx';
import { FcmService } from './services/notification/fcm.service';
@NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [
CommonModule,
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
IonicStorageModule.forRoot(),
HttpClientModule,
FormsModule,
ReactiveFormsModule,
AngularFireModule.initializeApp(environment.firebaseconfig),
AngularFirestoreModule
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA],
providers: [
StatusBar,
SplashScreen,
IonSlides,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
AngularFirestore,
FcmService,
Firebase
],
bootstrap: [AppComponent]
})
export class AppModule {
}
app.component.ts
import { Component } from '@angular/core';
import { Platform, Events } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { FcmService } from './services/notification/fcm.service';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html'
})
export class AppComponent {
constructor(
private platform: Platform,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private fcm: FcmService,
) {
this.initializeApp();
}
private notificationSetup() {
this.fcm.getToken();
this.fcm.onNotifications().subscribe(
(msg) => {
if (this.platform.is('ios')) {
this.alertSerivce.presentToast(msg.aps.alert);
} else {
this.alertSerivce.presentToast(msg.body);
}
});
}
initializeApp() {
this.platform.ready().then(() => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.notificationSetup();
});
}
}
fcm.service.ts
import { Injectable } from '@angular/core';
import { AngularFirestore } from '@angular/fire/firestore';
import { Firebase } from '@ionic-native/firebase/ngx';
import { Platform } from '@ionic/angular';
@Injectable({
providedIn: 'root'
})
export class FcmService {
constructor(
private firebase: Firebase,
private afs: AngularFirestore,
private platform: Platform,
) { }
async getToken() {
let token: string;
if (this.platform.is('android')) {
await this.firebase.getToken().then((fcmtoken) => {
token = fcmtoken
console.log('Permission granted! Save to the server!', fcmtoken);
}).catch((err) => {
console.log('Error while getting fcm token', err)
})
}
if (this.platform.is('ios')) {
await this.firebase.getToken().then((fcmtoken) => {
token = fcmtoken
console.log('Permission granted! Save to the server!', fcmtoken);
}).catch((err) => {
console.log('Error while getting fcm token', err)
})
await this.firebase.grantPermission();
}
this.alert.presentToast(`FCMToken: ${token}`)
this.saveToken(token);
return token
}
private saveToken(token) {
if (!token) return;
this.storage.set(constants.LocalStorageKeys.fcmToken, token)
return token
}
onNotifications() {
return this.firebase.onNotificationOpen();
}
}
package.json
"dependencies": {
"@angular/common": "^7.2.2",
"@angular/core": "^7.2.2",
"@angular/fire": "^5.1.2",
"@ionic-native/core": "^5.0.0",
"@ionic-native/firebase": "^5.0.0",
...
}
Я что-то упустил? Я застрял в этом выпуске от 2 дней.
Я следил за этим
https://medium.freecodecamp.org/how-to-get-push-notifications-working-with-ionic-4-and-firebase-ad87cc92394e