@@ -16,7 +16,7 @@ module.exports = function (dependencies) {
1616
1717 Provider . prototype = Object . create ( EventEmitter . prototype ) ;
1818
19- Provider . prototype . send = function send ( notification , recipients ) {
19+ Provider . prototype . send = async function send ( notification , recipients ) {
2020 const builtNotification = {
2121 headers : notification . headers ( ) ,
2222 body : notification . compile ( ) ,
@@ -26,38 +26,41 @@ module.exports = function (dependencies) {
2626 recipients = [ recipients ] ;
2727 }
2828
29- return Promise . all (
30- recipients . map ( token => this . client . write ( builtNotification , token , 'device' , 'post' ) )
31- ) . then ( responses => {
32- const sent = [ ] ;
33- const failed = [ ] ;
34-
35- responses . forEach ( response => {
36- if ( response . status || response . error ) {
37- failed . push ( response ) ;
38- } else {
39- sent . push ( response ) ;
40- }
41- } ) ;
42- return { sent , failed } ;
29+ const sentNotifications = await Promise . all (
30+ recipients . map (
31+ async token => await this . client . write ( builtNotification , token , 'device' , 'post' )
32+ )
33+ ) ;
34+ const sent = [ ] ;
35+ const failed = [ ] ;
36+
37+ sentNotifications . forEach ( sentNotification => {
38+ if ( sentNotification . status || sentNotification . error ) {
39+ failed . push ( sentNotification ) ;
40+ } else {
41+ sent . push ( sentNotification ) ;
42+ }
4343 } ) ;
44+
45+ return { sent, failed } ;
4446 } ;
4547
46- Provider . prototype . manageChannels = function manageChannels ( notification , bundleId , action ) {
48+ Provider . prototype . manageChannels = async function manageChannels (
49+ notifications ,
50+ bundleId ,
51+ action
52+ ) {
4753 let type = 'channels' ;
4854 let method = 'post' ;
4955
56+ if ( ! Array . isArray ( notifications ) ) {
57+ notifications = [ notifications ] ;
58+ }
59+
5060 switch ( action ) {
5161 case 'create' :
5262 type = 'channels' ;
5363 method = 'post' ;
54- if ( notification [ 'push-type' ] == null ) {
55- // Add live activity push type if it's not already provided.
56- // Live activity is the only current type supported.
57- // Note, this seems like it should be lower cased, but the
58- // docs shows it in the current format.
59- notification [ 'push-type' ] = 'LiveActivity' ;
60- }
6164 break ;
6265 case 'read' :
6366 type = 'channels' ;
@@ -76,25 +79,64 @@ module.exports = function (dependencies) {
7679 bundleId,
7780 error : new VError ( `the action "${ action } " is not supported` ) ,
7881 } ;
79- return Promise . resolve ( error ) ;
82+ return error ;
8083 }
8184 }
8285
83- const builtNotification = {
84- headers : notification . headers ( ) ,
85- body : notification . compile ( ) ,
86- } ;
86+ const sentNotifications = await Promise . all (
87+ notifications . map ( async notification => {
88+ if ( action == 'create' ) {
89+ notification . addPushTypeToPayloadIfNeeded ( ) ;
90+ }
91+ const builtNotification = {
92+ headers : notification . headers ( ) ,
93+ body : notification . compile ( ) ,
94+ } ;
8795
88- return this . client . write ( builtNotification , bundleId , type , method ) ;
96+ return await this . client . write ( builtNotification , bundleId , type , method ) ;
97+ } )
98+ ) ;
99+ const sent = [ ] ;
100+ const failed = [ ] ;
101+
102+ sentNotifications . forEach ( sentNotification => {
103+ if ( sentNotification . status || sentNotification . error ) {
104+ failed . push ( sentNotification ) ;
105+ } else {
106+ sent . push ( sentNotification ) ;
107+ }
108+ } ) ;
109+
110+ return { sent, failed } ;
89111 } ;
90112
91- Provider . prototype . broadcast = function broadcast ( notification , bundleId ) {
92- const builtNotification = {
93- headers : notification . headers ( ) ,
94- body : notification . compile ( ) ,
95- } ;
113+ Provider . prototype . broadcast = async function broadcast ( notifications , bundleId ) {
114+ if ( ! Array . isArray ( notifications ) ) {
115+ notifications = [ notifications ] ;
116+ }
117+
118+ const sentNotifications = await Promise . all (
119+ notifications . map ( async notification => {
120+ const builtNotification = {
121+ headers : notification . headers ( ) ,
122+ body : notification . compile ( ) ,
123+ } ;
124+
125+ return await this . client . write ( builtNotification , bundleId , 'broadcasts' , 'post' ) ;
126+ } )
127+ ) ;
128+ const sent = [ ] ;
129+ const failed = [ ] ;
130+
131+ sentNotifications . forEach ( sentNotification => {
132+ if ( sentNotification . status || sentNotification . error ) {
133+ failed . push ( sentNotification ) ;
134+ } else {
135+ sent . push ( sentNotification ) ;
136+ }
137+ } ) ;
96138
97- return this . client . write ( builtNotification , bundleId , 'broadcasts' , 'post' ) ;
139+ return { sent , failed } ;
98140 } ;
99141
100142 Provider . prototype . shutdown = function shutdown ( callback ) {
0 commit comments