Skip to content

Instantly share code, notes, and snippets.

@shingohry
Last active June 18, 2018 14:59
Show Gist options
  • Save shingohry/77ef8ccc9a3cae8ddcd2cc45139e4aed to your computer and use it in GitHub Desktop.
Save shingohry/77ef8ccc9a3cae8ddcd2cc45139e4aed to your computer and use it in GitHub Desktop.
Send iOS 12 grouped remote notifications with node-apn
// References:
// https://developer.apple.com/videos/play/wwdc2018/711/
// https://github.com/node-apn/node-apn/blob/master/doc/notification.markdown#convenience-setters
// https://eladnava.com/send-push-notifications-to-ios-devices-using-xcode-8-and-swift-3/
var apn = require('apn');
// Set up apn with the APNs Auth Key
var apnProvider = new apn.Provider({
token: {
key: 'xxxxx.p8', // Path to the key p8 file
keyId: 'yyyyy', // The Key ID of the p8 file (available at https://developer.apple.com/account/ios/certificate/key)
teamId: 'zzzzz', // The Team ID of your Apple Developer Account (available at https://developer.apple.com/account/#/membership/)
},
production: false // Set to true if sending a notification to a production iOS app
});
// Enter the device token from the Xcode console
var deviceToken = 'aaaaa';
// Prepare a new notification
var notification = new apn.Notification();
// Specify your iOS app's Bundle ID (accessible within the project editor)
notification.topic = 'com.example.app';
// Set expiration to 1 hour from now (in case device is offline)
notification.expiry = Math.floor(Date.now() / 1000) + 3600;
// Display the following message
notification.alert = {"body" : "Remote notification with threadId and summary", "summary-arg" : "thread-id-1"};
notification.threadId = "thread-id-1";
notification.category = "category-id-1";
// Actually send the notification
apnProvider.send(notification, deviceToken).then(function(result) {
// Check the result for any failed devices
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment