I have Ionic Native Push notification enabled on my app, that’s through FirebaseX.
My problem is that I get notification if the app is open in foreground but if it’s in Background I get nothing.
When I pull up the app, it delivers.
Can anybody shine a light?
Here is my service:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FirebaseX } from '@ionic-native/firebase-x/ngx';
import { AngularFirestore } from '@angular/fire/firestore';
import { Platform } from '@ionic/angular';
import { LoginService } from './login.service';
@Injectable()
export class FCMProvider {
constructor(
public firebaseX: FirebaseX,
public afs: AngularFirestore,
private platform: Platform,
private loginService: LoginService
) {}
async getToken() {
let token;
if (this.platform.is('android')) {
token = await this.firebaseX.getToken();
}
if (this.platform.is('ios')) {
const hasPermission = await this.firebaseX.hasPermission();
if (!hasPermission) {
await this.firebaseX.grantPermission();
} else {
token = await this.firebaseX.getToken();
}
}
console.log(token);
return this.saveTokenToFirestore(token);
}
saveTokenToFirestore(token: string) {
if (!token) {
return;
}
const devicesRef = this.afs.collection('devices');
let userOs;
if (this.platform.is('ios')) {
userOs = 'ios';
} else {
userOs = 'android';
}
const docData = {
token,
os: userOs,
userID: this.loginService.getUID()
};
return devicesRef.doc(token).set(docData);
}
listenToNotifications() {
return this.firebaseX.onMessageReceived();
}
}
and on my app.component.ts I got this right after deviceReady
:
pushNotification() {
this.fcProvider.getToken();
this.fcProvider.listenToNotifications()
.pipe(
tap(msg => {
const toast = this.toastCtrl.create({
message: msg.body,
duration: 4000
});
toast.then(a => a.present());
})
)
.subscribe();
}
I’m sending the messages though postman:
{
"to": "e749fugfe793hfhwifh8988799f:...",
"name": "my_notification",
"data": {
"title": "Test",
"body": "This is just a test notification",
"notification_foreground": "true",
"notification_body" : "Notification body",
"notification_title": "Notification title",
"forceStart": "1"
},
"priority": "high"
}