How to count push notifications APN -
on server side, nodejs platform sending apple push notifications using node-apn
here example badge - 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: sent 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 send push , increment in database badge + 1
when push read on device, decrement minus 1 badge - 1
every time send new push, send current number of badges if device offline can not push, number in database incremented.
how can correct count badges ?
if it's becoming problem perhaps shouldn't optimistically updating database? looks push code returns promise there natural flow there e.g.
service.send(...) .then(result => { // update badge count in db }); alternatively, if need update optimistically can decrement count if push happens fail
service.send(...) .then(result => { if (result.failed.length) { // decrement badge count in db } }); only thing is, sure when push notification fails apns won't try resend on behalf? iirc apns keep trying deliver push x period before failing - it's worth making sure failure true failure in won't retry again later.
based on discussion in comments, see problem more not knowing sure device has received push notification opposed knowing whether server has successfuly sent - however, come original point of perhaps being too optimistic here.
push notifications work off "fire , forget" type architecture, there's no guarentee apns message delivered device , there's no feedback on whether or wasn't. ultimately, reliable way of knowing device got message device tell you.
one way of doing add additional data push notification e.g. uuid, server generate represent start of message handshake. in mobile app, on receipt of push notification, can complete handshake telling server received message correlating id. naturally, you'd move away notion of having single "count" field , towards fledged table e.g.
messageid | devicetoken | senton | receivedon something not give count need gives audit trail, think prove useful down line determining stats sent vs received, problematic devices etc.
Comments
Post a Comment