Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'apn' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
// Make iOS device token lowercased and check that it doesn't containt special characters
const iOSDeviceToken = input.deviceToken.toLowerCase()
const tokenRegex = /^[A-Za-z0-9 ]+$/
const isValid = tokenRegex.test(iOSDeviceToken);
if (!isValid) {
this.props.updateOutput({
loading: false,
text: 'Failed: Wrong device token. iOS device token cannot contain special characters. It must be a string with 64 hexadecimal symbols. Example: "03ds25c845d461bcdad7802d2vf6fc1dfde97283bf75cn993eb6dca835ea2e2f"'
})
return
}
// Notification
const notification = new APN.Notification()
notification.expiry = Math.floor(Date.now() / 1000) + 3600
const collapseId = input.collapseId
if (collapseId && collapseId !== '') {
notification.collapseId = collapseId
}
// New iOS 13+ mandatory header, `apns-push-type`. Can be either `alert` or `background`.
// The value of this header must accurately reflect the contents of the notification's payload.
// More here: https://github.com/node-apn/node-apn/pull/656/commits/cd44a3e2604eebdd5db04235daf035cf353f544a
notification.pushType = "alert"
try {
const json = JSON.parse(input.message)
notification.rawPayload = json
Sender.prototype.send = function (data) {
// Create a new APN connection.
this.connection = this.connection || this.createConnection();
// Token can be string or array.
if (! _.isArray(data.token)) data.token = [data.token];
// Create notification.
var notification = new apn.Notification();
_.merge(notification, _.omit(data, 'token'));
// Push notification.
this.connection.pushNotification(notification, data.token);
};
users.forEach( (user) => {
let note = new apn.Notification();
note.alert = `Hey ${user.name}, I just sent my first Push Notification`;
// The topic is usually the bundle identifier of your application.
note.topic = "";
console.log(`Sending: ${note.compile()} to ${user.devices}`);
service.send(note, user.devices).then( result => {
console.log("sent:", result.sent.length);
console.log("failed:", result.failed.length);
console.log(result.failed);
});
});
- Breaking news
- Announcements
- Sport results
*/
const apn = require("apn");
let tokens = ["", ""];
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",
});
// The topic is usually the bundle identifier of your application.
note.topic = "";
console.log(`Sending: ${note.compile()} to ${tokens}`);
service.send(note, tokens).then( result => {
console.log("sent:", result.sent.length);
console.log("failed:", result.failed.length);
console.log(result.failed);
});
// For one-shot notification tasks you may wish to shutdown the connection
// after everything is sent, but only call shutdown if you need your
module.exports.test = function (deviceToken) {
var myDevice = new apn.Device(deviceToken);
var note = new apn.Notification();
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 0;
note.sound = 'ping.aiff';
note.alert = {};
note.alert.body = 'openHAB is offline';
note.payload = {'messageFrom': 'Caroline'};
apnConnection.pushNotification(note, myDevice);
}
module.exports.sendAppleNotification = function (deviceToken, message, payload) {
var myDevice = new apn.Device(deviceToken);
var note = new apn.Notification(payload);
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
note.badge = 0;
note.sound = 'ping.aiff';
note.alert = {};
note.alert.body = message;
apnConnection.pushNotification(note, myDevice);
}
APN.prototype.exec = function(command, params) {
if (command == "Push") {
var token = this.users[params.user];
var note = new apns.Notification();
note.device = new apns.Device(token);
note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
if (params.sound) note.sound = params.sound;
if (params.msg) note.alert = params.msg;
if (params.url) note.payload = {'url': params.url};
this.apnsConnection.sendNotification(note);
} else {
this.log(command);
}
};
if (notificationObj) {
const notificationObjAndHash = { ...notificationObj, messageHash }
const messageFingerprint = getMessageFingerprint(notificationObjAndHash)
if (deviceType === 'APN') {
if (!apnProvider) {
logger.error('APN provider not configured, notification failed')
return
}
if ((await isNotificationDupe(messageFingerprint, config)) > 0) {
logger.warn(`Duplicate. Notification already recently sent. Skipping.`)
return
}
// iOS notifications
const notification = new apn.Notification({
alert: notificationObj.message,
sound: 'default',
payload: notificationObj.payload,
topic: apnBundle
})
await apnProvider.send(notification, deviceToken).then(async result => {
if (result.sent.length) {
await logNotificationSent(
messageFingerprint,
ethAddress,
'mobile-ios'
)
logger.debug('APN sent: ', result.sent.length)
}
if (result.failed) {
logger.error('APN failed: ', result.failed)
static ios(config, body, tokens) {
const apnProvider = new Provider({
token: {
key: config.iosCert,
keyId: config.iosKeyId,
teamId: config.iosTeamId
},
production: config.iosEnv.toLowerCase() === 'production'
});
const note = new Notification({
body: body.message,
title: body.title,
topic: config.bundle
});
apnProvider.send(note, tokens)
.then(() => {
apnProvider.shutdown();
console.log('iOS Push Notification sent!');
process.exit(1);
}, err => {
console.log(err);
});
}
value: function ios(config, body, tokens) {
var apnProvider = new _apn.Provider({
token: {
key: config.iosCert,
keyId: config.iosKeyId,
teamId: config.iosTeamId
},
production: config.iosEnv.toLowerCase() === 'production'
});
var note = new _apn.Notification({
body: body.message,
title: body.title,
topic: config.bundle
});
apnProvider.send(note, tokens).then(function () {
apnProvider.shutdown();
console.log('iOS Push Notification sent!');
process.exit(1);
}, function (err) {
console.log(err);
});
}
}, {