coder fire
coder fire

Reputation: 1063

How to count push notifications APN

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 ?

Upvotes: 0

Views: 704

Answers (1)

James
James

Reputation: 82096

If it's becoming a problem then perhaps you shouldn't be optimistically updating the database? It looks like your push code returns a Promise so there is a natural flow there e.g.

service.send(...)
   .then(result => {
       // update badge count in DB
   });

Alternatively, if you need to update optimistically then you can just decrement the count if a push happens to fail

service.send(...)
   .then(result => {
      if (result.failed.length) {
         // decrement badge count in DB
      }
   });

Only thing I will say is, are you sure that when a push notification fails the APNS won't try to resend on your behalf? IIRC the APNS will keep trying to deliver the push for X period before eventually failing - it's worth making sure that a failure is a true failure in that it won't retry again later.


Based on the discussion in the comments, I see the problem is really more about not knowing for sure that the device has received the push notification as opposed to knowing whether the server has successfuly sent it - however, this does come back to my original point of perhaps your being too optimistic here.

Push notifications work off a "fire and forget" type architecture, there's no guarentee from APNS that your message will be delivered to the device and there's no feedback on whether it was or wasn't. Ultimately, the only reliable way of knowing the device got the message is for the device itself to tell you.

One way of doing this would be to add some additional data to your push notification e.g. a UUID, that the server could generate to represent the start of a message handshake. In your mobile app, on receipt of a push notification, it can then complete the handshake by telling the server it received the message by correlating the ID. Naturally, you'd move away from the notion of having a single "count" field and towards a fully fledged table e.g.

MessageID | DeviceToken | SentOn | ReceivedOn

Something like this would not only give you the count you need but could also gives you an audit trail, which I think would prove very useful down the line for determining stats like sent vs received, problematic devices etc.

Upvotes: 0

Related Questions