Hello,
I’m trying to figure out, how to show and increase Push Notification from my Google Firebase with Badge Counter with app icon of Ionic2 application in the background with Android device.
For example, function below lets to increment or decrements Notification Badge Counter with button click in home.html form home.ts just as example:
async increaseBadges(badgeNumber: string) {
try {
let badge = await this.badge.increase(Number(badgeNumber));
console.log(badge);
} catch (e) {
console.log(e);
}
}
Code in app.component.ts popups Push Notification from Google Firebase only with the opened app in foreground mode.
Advice would be helpful:
initPushNotification() {
this.push.hasPermission()
.then((res: any) => {
if (res.isEnabled) {
console.log('We have permission to send push notifications');
} else {
console.log('We don\'t have permission to send push notifications');
}
});
const options: PushOptions = {
android: {
senderID: 'My sender ID'
},
ios: {
alert: 'true',
badge: true,
sound: 'false'
},
windows: {}
};
const pushObject: PushObject = this.push.init(options);
pushObject.on('notification').subscribe((notification: any) => {
console.log('Received a notification', notification);
let confirmAlert = this.alertCtrl.create({
title: 'New Notification',
message: JSON.stringify(notification),
buttons: [{
text: 'Ignore',
role: 'cancel'
}, {
text: 'View',
handler: () => { }
}]
});
confirmAlert.present();
});
pushObject.on('registration').
subscribe((registration: any) =>
console.log('Device registered', registration));
pushObject.on('error').
subscribe(error =>
console.error('Error with Push plugin', error));
}