New protocol format as-of iOS 7 docs.

Enable the old protocol by passing `legacy: true` to the connection initialiser.
This commit is contained in:
Andrew Naylor 2013-09-25 18:40:32 +01:00
Родитель 12a8c1417c
Коммит ef13e4446d
3 изменённых файлов: 111 добавлений и 28 удалений

Просмотреть файл

@ -61,6 +61,8 @@ Options:
- `fastMode` {Boolean} Whether to aggresively empty the notification buffer while connected - if set to true node-apn may enter a tight loop under heavy load while delivering notifications. (Defaults to: `false`)
- `legacy` {Boolean} WHether to use the pre-iOS 7 protocol format. (Defaults to `false`)
**Important:** In a development environment you must set `gateway` to `gateway.sandbox.push.apple.com`.
## apn.Feedback([options])
@ -192,6 +194,16 @@ The maximum number of retries which should be performed when sending a notificat
The UNIX timestamp representing when the notification should expire. This does not contribute to the 256 byte payload size limit.
### notification.priority
From [Apples' Documentation][notificationFormat], Provide one of the following values:
* 10 - The push message is sent immediately. (Default)
> The push notification must trigger an alert, sound, or badge on the device. It is an error use this priority for a push that contains only the content-available key.
* 5 - The push message is sent at a time that conserves power on the device receiving it.
This value is not valid when the connection is in legacy mode.
### notification.encoding
The encoding to use when transmitting the notification to APNS, defaults to `utf8`. `utf16le` is also possible but as each character is represented by a minimum of 2 bytes, will at least halve the possible payload size. If in doubt leave as default.
@ -200,7 +212,6 @@ The encoding to use when transmitting the notification to APNS, defaults to `utf
This object represents the root JSON object that you can add custom information for your application to. The properties below will only be added to the payload (under `aps`) when the notification is prepared for sending.
### notification.badge
The value to specify for `payload.aps.badge`
@ -305,3 +316,4 @@ If you wish to disable the automatic resending functionality please consult the
[errors]:https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW4 "The Binary Interface and Notification Formats"
[feedback]:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW3 "The Feedback Service"
[pl]:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW1 "Local and Push Notification Programming Guide: Apple Push Notification Service"
[notificationFormat]:https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html#//apple_ref/doc/uid/TP40008194-CH101-SW9 "The Binary Interface and Notification Format"

Просмотреть файл

@ -42,6 +42,7 @@ if(process.env.DEBUG) {
* @config {Number} [connectionTimeout=0] The duration the socket should stay alive with no activity in milliseconds. 0 = Disabled.
* @config {Boolean} [buffersNotifications=true] Whether to buffer notifications and resend them after failure.
* @config {Boolean} [fastMode=false] Whether to aggresively empty the notification buffer while connected.
* @config {Boolean} [legacy=false] Whether to use the old (pre-iOS 7) protocol format.
*/
function Connection (options) {
if(false === (this instanceof Connection)) {
@ -64,7 +65,9 @@ function Connection (options) {
autoAdjustCache: true,
maxConnections: 1,
connectionTimeout: 0,
buffersNotifications: true
buffersNotifications: true,
fastMode: false,
legacy: false
};
util.extend(this.options, options);
@ -559,37 +562,93 @@ Connection.prototype.transmitNotification = function(socket, notification) {
if (socket.currentId > 0xffffffff) {
socket.currentId = 0;
}
if (this.options.enhanced) {
data = new Buffer(1 + 4 + 4 + 2 + token.length + 2 + messageLength);
// Command
data[position] = 1;
position++;
if (this.options.legacy) {
if (this.options.enhanced) {
data = new Buffer(1 + 4 + 4 + 2 + token.length + 2 + messageLength);
// Command
data[position] = 1;
position++;
// Identifier
data.writeUInt32BE(notification._uid, position);
position += 4;
// Identifier
data.writeUInt32BE(notification._uid, position);
position += 4;
// Expiry
data.writeUInt32BE(notification.notification.expiry, position);
position += 4;
this.cacheNotification(socket, notification);
// Expiry
data.writeUInt32BE(notification.notification.expiry, position);
position += 4;
this.cacheNotification(socket, notification);
}
else {
data = new Buffer(1 + 2 + token.length + 2 + messageLength);
//Command
data[position] = 0;
position++;
}
// Token Length
data.writeUInt16BE(token.length, position);
position += 2;
// Device Token
position += token.copy(data, position, 0);
// Payload Length
data.writeUInt16BE(messageLength, position);
position += 2;
//Payload
position += data.write(message, position, encoding);
}
else {
data = new Buffer(1 + 2 + token.length + 2 + messageLength);
//Command
data[position] = 0;
position++;
// New Protocol uses framed notifications consisting of multiple items
// 1: Device Token
// 2: Payload
// 3: Notification Identifier
// 4: Expiration Date
// 5: Priority
// Each item has a 3 byte header: Type (1), Length (2) followed by data
var frameLength = 3 + 32 + 3 + messageLength + 3 + 4;
if(notification.notification.expiry > 0) {
frameLength += 3 + 4;
}
if(notification.notification.priority != 10) {
frameLength += 3 + 1;
}
// Frame has a 5 byte header: Type (1), Length (4) followed by items.
data = new Buffer(5 + frameLength);
data[position] = 2; position += 1;
// Frame Length
data.writeUInt32BE(frameLength, position); position += 4;
// Token Item
data[position] = 1; position += 1;
data.writeUInt16BE(token.length, position); position += 2;
position += token.copy(data, position, 0);
// Payload Item
data[position] = 2; position += 1;
data.writeUInt16BE(messageLength, position); position += 2;
position += data.write(message, position, encoding);
// Identifier Item
data[position] = 3; position += 1;
data.writeUInt16BE(4, position); position += 2;
data.writeUInt32BE(notification._uid, position); position += 4;
if(notification.notification.expiry > 0) {
// Expiry Item
data[position] = 4; position += 1;
data.writeUInt16BE(4, position); position += 2;
data.writeUInt32BE(notification.notification.expiry, position); position += 4;
}
if(notification.notification.priority != 10) {
// Priority Item
data[position] = 5; position += 1;
data.writeUInt16BE(1, position); position += 2;
data[position] = notification.notification.priority; position += 1;
}
this.cacheNotification(socket, notification);
}
// Token Length
data.writeUInt16BE(token.length, position);
position += 2;
// Device Token
position += token.copy(data, position, 0);
// Payload Length
data.writeUInt16BE(messageLength, position);
position += 2;
//Payload
position += data.write(message, position, encoding);
socket.busy = true;
return socket.write(data);

Просмотреть файл

@ -8,6 +8,7 @@ function Notification (payload) {
this.payload = payload || {};
this.expiry = 0;
this.identifier = 0;
this.priority = 10;
this.retryLimit = -1;
@ -41,6 +42,7 @@ Notification.prototype.clone = function (device) {
notification.payload = this.payload;
notification.expiry = this.expiry;
notification.identifier = this.identifier;
notification.priority = this.priority;
notification.device = device;
notification.alert = this.alert;
@ -63,6 +65,16 @@ Notification.prototype.setExpiry = function (expiry) {
return this;
};
/**
* Set the priority
* @param {Number} [priority=10] Priority value for the notification.
* @since v1.3.9
*/
Notification.prototype.setPriority = function (priority) {
this.priority = priority;
return this;
}
/**
* Set the "badge" value on the alert object
* @param {Number} [badge] Badge Value