@@ -104,34 +104,74 @@ APNS.prototype.send = function(data, devices) {
104104 let coreData = data . data ;
105105 let expirationTime = data [ 'expiration_time' ] ;
106106 let notification = generateNotification ( coreData , expirationTime ) ;
107+ let allPromises = [ ] ;
107108
108- let promises = devices . map ( ( device ) => {
109+ // Start by clustering the devices per connections
110+ let devicesPerConnIndex = devices . reduce ( ( memo , device ) => {
109111 let qualifiedConnIndexs = chooseConns ( this . conns , device ) ;
110- // We can not find a valid conn, just ignore this device
111112 if ( qualifiedConnIndexs . length == 0 ) {
112113 log . error ( LOG_PREFIX , 'no qualified connections for %s %s' , device . appIdentifier , device . deviceToken ) ;
113- return Promise . resolve ( {
114+ let promise = Promise . resolve ( {
114115 transmitted : false ,
115116 device : {
116117 deviceToken : device . deviceToken ,
117118 deviceType : 'ios'
118119 } ,
119120 result : { error : 'No connection available' }
120121 } ) ;
122+ allPromises . push ( promise ) ;
123+ } else {
124+ let apnDevice = new apn . Device ( device . deviceToken ) ;
125+ apnDevice . connIndex = qualifiedConnIndexs [ 0 ] ;
126+ if ( device . appIdentifier ) {
127+ apnDevice . appIdentifier = device . appIdentifier ;
128+ }
129+ memo [ apnDevice . connIndex ] = memo [ apnDevice . connIndex ] || [ ] ;
130+ memo [ apnDevice . connIndex ] . push ( apnDevice ) ;
121131 }
122- let conn = this . conns [ qualifiedConnIndexs [ 0 ] ] ;
123- let apnDevice = new apn . Device ( device . deviceToken ) ;
124- apnDevice . connIndex = qualifiedConnIndexs [ 0 ] ;
125- // Add additional appIdentifier info to apn device instance
126- if ( device . appIdentifier ) {
127- apnDevice . appIdentifier = device . appIdentifier ;
128- }
129- return new Promise ( ( resolve , reject ) => {
130- apnDevice . callback = resolve ;
131- conn . pushNotification ( notification , apnDevice ) ;
132+ return memo ;
133+ } , { } )
134+
135+ allPromises = Object . keys ( devicesPerConnIndex ) . reduce ( ( memo , connIndex ) => {
136+ let devices = devicesPerConnIndex [ connIndex ] ;
137+ // Create a promise, attach the callback
138+ let promises = devices . map ( ( apnDevice ) => {
139+ return new Promise ( ( resolve , reject ) => {
140+ apnDevice . callback = resolve ;
141+ } ) ;
132142 } ) ;
133- } ) ;
134- return Parse . Promise . when ( promises ) ;
143+ let conn = this . conns [ connIndex ] ;
144+ conn . pushNotification ( notification , devices ) ;
145+ return memo . concat ( promises ) ;
146+ } , allPromises )
147+
148+ // let promises = devices.map((device) => {
149+ // let qualifiedConnIndexs = chooseConns(this.conns, device);
150+ // // We can not find a valid conn, just ignore this device
151+ // if (qualifiedConnIndexs.length == 0) {
152+ // log.error(LOG_PREFIX, 'no qualified connections for %s %s', device.appIdentifier, device.deviceToken);
153+ // return Promise.resolve({
154+ // transmitted: false,
155+ // device: {
156+ // deviceToken: device.deviceToken,
157+ // deviceType: 'ios'
158+ // },
159+ // result: {error: 'No connection available'}
160+ // });
161+ // }
162+ // let conn = this.conns[qualifiedConnIndexs[0]];
163+ // let apnDevice = new apn.Device(device.deviceToken);
164+ // apnDevice.connIndex = qualifiedConnIndexs[0];
165+ // // Add additional appIdentifier info to apn device instance
166+ // if (device.appIdentifier) {
167+ // apnDevice.appIdentifier = device.appIdentifier;
168+ // }
169+ // return new Promise((resolve, reject) => {
170+ // apnDevice.callback = resolve;
171+ // conn.pushNotification(notification, apnDevice);
172+ // });
173+ // });
174+ return Parse . Promise . when ( allPromises ) ;
135175}
136176
137177function handleTransmissionError ( conns , errCode , notification , apnDevice ) {
0 commit comments