I struggle to figure out why my iOS app can't register clicks on push notifications when the app is closed or terminated. It works just fine in foreground mode, and background mode as long as the app is not killed.
When the app "dead" the onNotificationOpen
callback is never fired, so i'm not able to route the user to the desired location in the app.
Can someone point out my mistakes, or provide me with a working example? The documentation for cordova-plugin-firebase is very minimal regarding push notifications.
app.component.ts
this.platform.ready().then(() => {
if (this.platform.is('cordova')) {
this.pushProvider.init();
this.platform.resume.subscribe(() => {
this.pushProvider.init();
});
}
});
push.provider.ts
import { Injectable } from '@angular/core';
import { Firebase } from '@ionic-native/firebase';
import { Platform } from 'ionic-angular';
@Injectable()
export class PushNotificationsProvider {
constructor (private firebase: Firebase, private platform: Platform) {
}
init () {
if (!this.platform.is('cordova')) {
return;
}
if (this.platform.is('ios')) {
this.firebase.grantPermission().then(() => {
this.initListeners();
});
} else if (this.platform.is('android')) {
this.firebase.hasPermission().then(data => {
if (data && data.isEnabled) {
this.initListeners();
}
});
}
}
initListeners () {
this.firebase.onNotificationOpen().subscribe((notification: any) => {
this.handleNotification(notification);
});
}
handleNotification (notification) {
if (!notification) {
return;
}
if (notification.tap) {
this.backgroundNotification(notification);
} else {
this.foregroundNotification(notification);
}
}
backgroundNotification (notification) {
const message = (notification.aps || {}).alert;
alert('*** FMC: opened in background: ' + message);
}
foregroundNotification (notification) {
const message = (notification.aps || {}).alert;
alert('*** FMC: opened in foreground: ' + message);
}
}
package.json
{
"ionic-angular": "3.9.2",
"cordova-plugin-firebase": "^1.0.2"
"@ionic-native/firebase": "^4.7.0"
}