I’m trying to use Firebase Cloud Messaging with Ionic Native to send background messages.
The problem I encounter is, that the on.notification handler doesn’t get called, when I send silent notifications.
I tested it with regular ones and it works perfectly. However, as soon as I send silent ones, it doesn’t work.
Here is my FCM message:
'message': {
'token': '<my-token>',
'apns': {
'payload': {
'aps': {
'data': 'here is my data',
'badge': 1,
'content-available': 1,
}
},
'notId': 1
}
}
Furthermore, here is my code within the app:
import { Injectable } from '@angular/core';
import { Push, PushObject, PushOptions } from '@ionic-native/push';
import { AlertController, Platform } from 'ionic-angular';
import { FcmDataProvider } from './fcm.data';
@Injectable()
export class FcmProvider {
/*
* FIREBASE CLOUD MESSAGING
*/
constructor(private push: Push,
private alertCtrl: AlertController,
private platform: Platform,
private fcmDataProv: FcmDataProvider) {
}
getPermission(): Promise<{isEnabled: boolean}> {
// Listen for res.isEnabled.
return this.push.hasPermission();
}
initPush() {
console.log("Init push!");
const options: PushOptions = this.initPushOptions();
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification);
if(this.platform.is('ios')) {
this.handleIOSNotification(notification, pushObject);
} else if(this.platform.is('android')) {
this.handleAndroidNotification(notification);
}
this.presentSuccessAlert(notification.message);
});
pushObject.on('registration').subscribe(
(registration: any) => {
console.log('Device registered', registration);
// TODO: Send registration.registrationId to server and update it.
}
);
pushObject.on('error').subscribe(
error => console.error('Error with Push plugin', error)
);
}
private initPushOptions(): PushOptions {
return {
android: {
sound: true,
vibrate: true,
clearBadge: true
},
ios: {
alert: true,
badge: true,
sound: true,
clearBadge: true
},
windows: {}, // Lol
browser: {
pushServiceURL: 'http://push.api.phonegap.com/v1/push'
}
};
}
private handleIOSNotification(data, push: PushObject) {
console.log("===========");
console.log("Dissecting data: ");
console.log("Keys: data");
console.log(Object.keys(data));
console.log("Keys: data.additionalData");
console.log(Object.keys(data.additionalData));
console.log("Keys: data.additionalData.data");
console.log(Object.keys(data.additionalData.data));
console.log("data.additionalData.data");
console.log(data.additionalData.data);
console.log("Keys: data.additionalData.moreData");
console.log(Object.keys(data.additionalData.moreData));
console.log("data.additionalData.moreData");
console.log(data.additionalData.moreData);
push.finish().then(
() => console.log("Finished processing push data")
).catch(() => console.error(
"Something went wrong with push.finish for ID=", data.additionalData.notId
));
}
private handleAndroidNotification(data) {
console.log(data.data);
}
private presentSuccessAlert(message: string): void {
let alert = this.alertCtrl.create({
title: "Neue Benachrichtigung",
message: message,
buttons: ["Ok"]
});
alert.present();
}
}
And here is the output of console.log in XCode:
Push Plugin notId 1
Warning: Application delegate received call to -application:didReceiveRemoteNotification:fetchCompletionHandler: but the completion handler was never called.
Notification received
Push Plugin key: content-available
Push Plugin key: data
Push Plugin key: badge
Does anyone know what is going wrong?