Hello,
I’m trying to get and increase badges over the app icon with receiving of push notification, here is my home.ts
with message and get
request:
var mess = {
message: messTxt,
title: tit,
subtitle: 'Museit',
tickerText: 'Museit Notification',
vibrate: '1',
sound: '1',
badge: '1',
largeIcon: 'large_icon',
smallIcon: 'small_icon'
}
this.http.get(this.phpPathTok + 'sendPushNotification.php?token=' + tok + '&msg=' + JSON.stringify(mess))
.map(res => res.json())
.subscribe(data => {
console.log(data);
}, err => {
console.log("Oops!", err);
}
);
My server side php
based on this example, which uses Firebase
API_ACCESS_KEY
.
I am sending a JSON.stringify(mess)
to php. Then with receiving of push notification in app.components.ts
with initPushNotification();
:
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) => {
if (notification.additionalData.foreground || notification.additionalData.background) {
console.log('Received a notification', notification);
let confirmAlert = this.alertCtrl.create({
title: 'New Notification',
message: notification.message,
buttons: [{
text: 'Ignore',
role: 'cancel'
}, {
text: 'View',
handler: () => {
this.nav.push(DetailsPage, { message: notification.message });
}
}]
});
confirmAlert.present();
}
});
pushObject.on('registration').subscribe((registration: any) => {
console.log('Device registered', registration);
var t = registration.registrationId;
this.saveDeviceToken(t);
});
pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));
}
in result I got desired badge counter circle over the app icon with receiving push notification, but with receiving of second notification counter number is still 1.
What value I have to increase with +1
for each received notification, instead of this.badgeNumber;
variable in code below, which works only in foreground with this.increaseBadges(this.badgeNumber);
in initPushNotification();
?
badge: '1'
, of payload comes from the sender as fixed number, how to use it?
It looks like, code below does not participates in process at all:
async requestPermission() {
try {
let hasPermission = await this.badge.hasPermission();
console.log(hasPermission);
if (!hasPermission) {
let permission = await this.badge.requestPermission();
console.log(permission);
}
} catch (e) {
console.error(e);
}
}
async increaseBadges(badgeNumber: number) {
try {
let badge = await this.badge.increase(badgeNumber);
console.log("Badge notification counter: ", badge);
this.badgeNumber = badge;
var badgeAmount = badge;
this.setBadges(badgeAmount);
} catch (e) {
console.log(e);
}
}
async setBadges(badgeAmount: number) {
try {
let badges = await this.badge.set(badgeAmount);
console.log("Set badges: ", badges);
this.out = badges;
this.getBadges()
} catch (e) {
console.log(e);
}
}
async getBadges() {
try {
let badgeAmount = await this.badge.get();
console.log("Get badges amount: ", badgeAmount);
this.out = badgeAmount;
} catch (e) {
console.log(e);
}
}