@@ -96,18 +96,40 @@ export class APNS {
96
96
}
97
97
98
98
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 ) ) ) ;
103
102
}
104
103
105
- return Promise . all ( allPromises ) . then ( ( results ) => {
104
+ return Promise . all ( allPromises ) . then ( ( results ) => {
106
105
// flatten all
107
106
return [ ] . concat . apply ( [ ] , results ) ;
108
107
} ) ;
109
108
}
110
109
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
+
111
133
static _validateAPNArgs ( apnsArgs ) {
112
134
if ( apnsArgs . topic ) {
113
135
return true ;
0 commit comments