@@ -96,18 +96,40 @@ export class APNS {
9696 }
9797
9898 let notification = APNS . _generateNotification ( coreData , expirationTime , appIdentifier ) ;
99- let promise = providers [ 0 ]
100- . send ( notification , devices . map ( device => device . deviceToken ) )
101- . then ( this . _handlePromise . bind ( this ) ) ;
102- allPromises . push ( promise ) ;
99+ const deviceIds = devices . map ( device => device . deviceToken ) ;
100+ let promise = this . sendThroughProvider ( notification , deviceIds , providers ) ;
101+ allPromises . push ( promise . then ( this . _handlePromise . bind ( this ) ) ) ;
103102 }
104103
105- return Promise . all ( allPromises ) . then ( ( results ) => {
104+ return Promise . all ( allPromises ) . then ( ( results ) => {
106105 // flatten all
107106 return [ ] . concat . apply ( [ ] , results ) ;
108107 } ) ;
109108 }
110109
110+ sendThroughProvider ( notification , devices , providers ) {
111+ return providers [ 0 ]
112+ . send ( notification , devices )
113+ . then ( ( response ) => {
114+ if ( response . failed
115+ && response . failed . length > 0
116+ && providers && providers . length > 1 ) {
117+ let devices = response . failed . map ( ( failure ) => { return failure . device ; } ) ;
118+ // Reset the failures as we'll try next connection
119+ response . failed = [ ] ;
120+ return this . sendThroughProvider ( notification ,
121+ devices ,
122+ providers . slice ( 1 , providers . length ) ) . then ( ( retryResponse ) => {
123+ response . failed = response . failed . concat ( retryResponse . failed ) ;
124+ response . sent = response . sent . concat ( retryResponse . sent ) ;
125+ return response ;
126+ } ) ;
127+ } else {
128+ return response ;
129+ }
130+ } ) ;
131+ }
132+
111133 static _validateAPNArgs ( apnsArgs ) {
112134 if ( apnsArgs . topic ) {
113135 return true ;
0 commit comments