Я отправляю push-уведомление, используя node-apn
NodeJS 8. Но я вижу, что push-уведомление отправляется один раз, а подписка на событие срабатывает несколько раз.Зависит от количества отправленных push-уведомлений. Похоже, что каждый раз, когда ti показывает на 1 строку меньше.
Вот вывод для 2 уведомлений:
TRANSMITTED: {"Type":"1","UserID":"XXX","Seq":0,"aps":{"sound":"CC.mp4","alert":"Test local push #0"}}, token 02565428df5c4ef480e48cdf85165334f03bc092a2f2445e867897ff0f3e0f34
TRANSMITTED: {"Type":"1","UserID":"XXX","Seq":1,"aps":{"sound":"CC.mp4","alert":"Test local push #1"}}, token 8fa1977a9eb648d4bc865900bcf85a76503acb7f4ded4eb284809167c3f8a902
ERROR: {"Type":"1","UserID":"XXX","Seq":0,"aps":{"sound":"CC.mp4","alert":"Test local push #0"}}, token 02565428df5c4ef480e48cdf85165334f03bc092a2f2445e867897ff0f3e0f34, code: 8
TRANSMITTED: {"Type":"1","UserID":"XXX","Seq":1,"aps":{"sound":"CC.mp4","alert":"Test local push #1"}}, token 8fa1977a9eb648d4bc865900bcf85a76503acb7f4ded4eb284809167c3f8a902
ERROR: {"Type":"1","UserID":"XXX","Seq":1,"aps":{"sound":"CC.mp4","alert":"Test local push #1"}}, token 8fa1977a9eb648d4bc865900bcf85a76503acb7f4ded4eb284809167c3f8a902, code: 8
Для 3 уведомлений это выглядит так:
TRANSMITTED: {"Seq":0,"aps":{"sound":"CC.mp4","alert":"Test local push #0"}}, token 02565428df5c4ef480e48cdf85165334f03bc092a2f2445e867897ff0f3e0f34
TRANSMITTED: {"Seq":1,"aps":{"sound":"CC.mp4","alert":"Test local push #1"}}, token 8fa1977a9eb648d4bc865900bcf85a76503acb7f4ded4eb284809167c3f8a902
TRANSMITTED: {"Seq":2,"aps":{"sound":"CC.mp4","alert":"Test local push #2"}}, token 2d72933e029a4f5c92e49e74050296718c386b450645429fa1cdb13d16ded23c
ERROR: {"Seq":0,"aps":{"sound":"CC.mp4","alert":"Test local push #0"}}, token 02565428df5c4ef480e48cdf85165334f03bc092a2f2445e867897ff0f3e0f34, code: 8
TRANSMITTED: {"Seq":1,"aps":{"sound":"CC.mp4","alert":"Test local push #1"}}, token 8fa1977a9eb648d4bc865900bcf85a76503acb7f4ded4eb284809167c3f8a902
TRANSMITTED: {"Seq":2,"aps":{"sound":"CC.mp4","alert":"Test local push #2"}}, token 2d72933e029a4f5c92e49e74050296718c386b450645429fa1cdb13d16ded23c
ERROR: {"Seq":1,"aps":{"sound":"CC.mp4","alert":"Test local push #1"}}, token 8fa1977a9eb648d4bc865900bcf85a76503acb7f4ded4eb284809167c3f8a902, code: 8
TRANSMITTED: {"Seq":2,"aps":{"sound":"CC.mp4","alert":"Test local push #2"}}, token 2d72933e029a4f5c92e49e74050296718c386b450645429fa1cdb13d16ded23c
ERROR: {"Seq":2,"aps":{"sound":"CC.mp4","alert":"Test local push #2"}}, token 2d72933e029a4f5c92e49e74050296718c386b450645429fa1cdb13d16ded23c, code: 8
И аналогично для 4, 5 и т. Д. Количество нажатий.
Вот пример кода, который я использую для его воспроизведения (токен - фальшивка):
const apns = require('apn');
const path = require('path');
const certPath = path.resolve(__dirname, './config/cert/push/cert.pem');
const certKey = path.resolve(__dirname, './config/cert/push/key.pem');
var options = {
gateway: 'gateway.sandbox.push.apple.com',
cert: certPath,
key: certKey,
passphrase: 'FaKePaSs',
ca: null,
port: 2195,
enhanced: true,
maxConnections: 10,
cacheLength: 10
};
const service = new apns.Connection(options);
service.on('transmitted', function(notification, device) {
console.log('TRANSMITTED: '+ JSON.stringify(notification) +', token ' + device.token.toString('hex'));
});
service.on('transmissionError', function(errCode, notification, device) {
console.log('ERROR: '+ JSON.stringify(notification) + ', token ' + device.token.toString('hex') + ', code: ' + errCode);
});
const notifications = [{
payload: {"Seq": 0},
alert: 'Test local push #',
expiry: Math.floor(Date.now() / 1000) + 3600,
sound: 'CC.mp4',
token: '02565428df5c4ef480e48cdf85165334f03bc092a2f2445e867897ff0f3e0f34'
}, {
payload: {"Seq": 0},
alert: 'Test local push #',
expiry: Math.floor(Date.now() / 1000) + 3600,
sound: 'CC.mp4',
token: '8fa1977a9eb648d4bc865900bcf85a76503acb7f4ded4eb284809167c3f8a902'
}, {
payload: {"Seq": 0},
alert: 'Test local push #',
expiry: Math.floor(Date.now() / 1000) + 3600,
sound: 'CC.mp4',
token: '2d72933e029a4f5c92e49e74050296718c386b450645429fa1cdb13d16ded23c'
}];
notifications.forEach(function (n, i) {
n.payload.Seq = i;
n.alert = n.alert + i;
const device = new apns.Device(n.token);
const note = new apns.Notification();
note.expiry = n.expiry;
note.alert = n.alert;
note.payload = n.payload;
note.sound = n.sound;
service.pushNotification(note, device);
});