Skip to content

Instantly share code, notes, and snippets.

@milankamilya
Last active November 14, 2019 06:55
Show Gist options
  • Save milankamilya/eb592fa0905f2e06728eb67627034766 to your computer and use it in GitHub Desktop.
Save milankamilya/eb592fa0905f2e06728eb67627034766 to your computer and use it in GitHub Desktop.
var apn = require('apn');
var options = {
token: {
key: './keys/AuthKey_XXXXXXXXXX.p8',
keyId: "XXXXXXXXXX",
teamId: "XXXXXXXXXX"
},
production: false
};
var apnProvider = new apn.Provider(options);
function sendPushNotification(body){
var notification = new apn.Notification();
notification.expiry = Math.floor(Date.now() / 1000) + 60; // Expires 1 min from now.
notification.badge = 1;
notification.alert = body.alertMessage;
notification.sound = "ping.aiff";
notification.payload = body.payload;
notification.topic = "<application bundle identifier>";
// Generate this UUID and store that in DB to remove the notification in future
notification.id = "a986cc6c-f5a6-11e9-802a-5aa538984bd8";
// We can achieve the same feature using 'apns-collapse-id'.
// 'apns-collapse-id' ID need to be set. apn library provides a collapseId
// property which is linked with 'apns-collapse-id'.
// More Info: https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingwithAPNs.html#//apple_ref/doc/uid/TP40008194-CH11-SW1
// notification.collapseId = "a986cc6c-f5a6-11e9-802a-5aa538984bd8";
apnProvider.send(notification, body.deviceToken).then( (result) => {
console.log('APNS Response : \n', result);
res.send('POST App Push Notification');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment