On the server side, the nodejs platform for sending Apple push notifications is using node-apn
Here is an example with a badge - this is the number of pushes.
const apn = require("apn");
let tokens = ["<insert token here>", "<insert token here>"];
let service = new apn.Provider({
cert: "certificates/cert.pem",
key: "certificates/key.pem",
});
let note = new apn.Notification({
alert: "Breaking News: I just sent my first Push Notification",
badge: countSendedPushes()
});
note.topic = "<bundle identifier>";
service.send(note, tokens).then( result => {
console.log("sent:", result.sent.length);
console.log("failed:", result.failed.length);
console.log(result.failed);
});
In fact, every time I send a push and I increment in my database badge + 1
When push was read on the device, I decrement minus one badge - 1
Every time I send a new push, I always send the current number of badges But if the device is offline it can not push, but the number in the database is already incremented.
How I can correct count badges ?