1- using System ;
2- using System . IO ;
3- using System . Linq ;
4- using System . Reflection ;
5- using System . Collections . Generic ;
6- using System . Threading . Tasks ;
1+ using DnsClient ;
2+ using FirebaseAdmin . Messaging ;
73using Resgrid . Model ;
84using Resgrid . Model . Messages ;
95using Resgrid . Model . Providers ;
106using Resgrid . Model . Services ;
11- using DnsClient ;
7+ using System ;
8+ using System . Collections . Generic ;
9+ using System . Drawing ;
10+ using System . IO ;
11+ using System . Linq ;
12+ using System . Reflection ;
13+ using System . Threading . Tasks ;
1214
1315namespace Resgrid . Services
1416{
@@ -36,31 +38,18 @@ public async Task<bool> Register(PushUri pushUri)
3638if ( pushUri == null || String . IsNullOrWhiteSpace ( pushUri . DeviceId ) )
3739return false ;
3840
39- string deviceId = pushUri . DeviceId . GetHashCode ( ) . ToString ( ) ;
41+ var code = pushUri . PushLocation ;
4042
41- //We just store the full Device Id in the PushUri object, the hashed version is for Azure
42- //var existingPushUri = _pushUriService.GetPushUriByPlatformDeviceId((Platforms)pushUri.PlatformType, pushUri.DeviceId);
43- List < PushRegistrationDescription > usersDevices = null ;
43+ //1) iOS -> APNS
44+ if ( pushUri . PlatformType == ( int ) Platforms . iOS )
45+ return await _novuProvider . UpdateUserSubscriberApns ( pushUri . UserId , code , pushUri . DeviceId ) ;
4446
45- try
46- {
47- usersDevices = await _notificationProvider . GetRegistrationsByUserId ( pushUri . UserId ) ;
48-
49- if ( usersDevices == null || ! usersDevices . Any ( x=> x . Tags . Contains ( deviceId ) ) )
50- await _notificationProvider . RegisterPush ( pushUri ) ;
51- }
52- catch ( TimeoutException )
53- { }
54- catch ( TaskCanceledException )
55- { }
56-
57- //if (existingPushUri == null)
58- //pushUri = _pushUriService.SavePushUri(pushUri);
47+ // 2) Android -> FCM
48+ if ( pushUri . PlatformType == ( int ) Platforms . Android )
49+ return await _novuProvider . UpdateUserSubscriberFcm ( pushUri . UserId , code , pushUri . DeviceId ) ;
5950
60- //if (usersDevices == null || !usersDevices.Any(x => x.Tags.Contains(deviceId)))
61- //await _notificationProvider.RegisterPush(pushUri);
62-
63- return true ;
51+ // 3) TODO: Web Push (other platforms)
52+ return false ;
6453}
6554
6655public async Task < bool > UnRegister ( PushUri pushUri )
@@ -72,13 +61,22 @@ public async Task<bool> UnRegister(PushUri pushUri)
7261
7362public async Task < bool > RegisterUnit ( PushUri pushUri )
7463{
75- if ( pushUri . UnitId . HasValue && ! string . IsNullOrWhiteSpace ( pushUri . PushLocation ) && pushUri . PlatformType == ( int ) Platforms . iPhone ) // 1
76- await _novuProvider . UpdateUnitSubscriberAps ( pushUri . UnitId . Value , pushUri . PushLocation , pushUri . DeviceId ) ;
77- else // 2 (Android)
78- await _novuProvider . UpdateUnitSubscriberFcm ( pushUri . UnitId . Value , pushUri . PushLocation , pushUri . DeviceId ) ;
79- // Eventually 3 for Web Push
64+ if ( pushUri == null || ! pushUri . UnitId . HasValue || string . IsNullOrWhiteSpace ( pushUri . PushLocation ) )
65+ return false ;
8066
81- return true ;
67+ var unitId = pushUri . UnitId . Value ;
68+ var code = pushUri . PushLocation ;
69+
70+ // 1) iOS -> APNS
71+ if ( pushUri . PlatformType == ( int ) Platforms . iOS )
72+ return await _novuProvider . UpdateUnitSubscriberApns ( unitId , code , pushUri . DeviceId ) ;
73+
74+ // 2) Android -> FCM
75+ if ( pushUri . PlatformType == ( int ) Platforms . Android )
76+ return await _novuProvider . UpdateUnitSubscriberFcm ( unitId , code , pushUri . DeviceId ) ;
77+
78+ // 3) TODO: Web Push (other platforms)
79+ return false ;
8280}
8381
8482public async Task < bool > UnRegisterUnit ( PushUri pushUri )
@@ -102,7 +100,25 @@ public async Task<bool> PushMessage(StandardPushMessage message, string userId,
102100profile = await _userProfileService . GetProfileByUserIdAsync ( userId ) ;
103101
104102if ( profile != null && profile . SendMessagePush )
105- await _notificationProvider . SendAllNotifications ( message . Title , message . SubTitle , userId , string . Format ( "M{0}" , message . MessageId ) , ( ( int ) PushSoundTypes . Message ) . ToString ( ) , true , 1 , "#000000" ) ;
103+ {
104+ try
105+ {
106+ await _notificationProvider . SendAllNotifications ( message . Title , message . SubTitle , userId , string . Format ( "M{0}" , message . MessageId ) , ( ( int ) PushSoundTypes . Message ) . ToString ( ) , true , 1 , "#000000" ) ;
107+ }
108+ catch ( Exception ex )
109+ {
110+ Framework . Logging . LogException ( ex ) ;
111+ }
112+
113+ try
114+ {
115+ await _novuProvider . SendUserMessage ( message . Title , message . SubTitle , userId , message . DepartmentCode , string . Format ( "M{0}" , message . MessageId ) , null ) ;
116+ }
117+ catch ( Exception ex )
118+ {
119+ Framework . Logging . LogException ( ex ) ;
120+ }
121+ }
106122
107123return true ;
108124}
@@ -116,8 +132,24 @@ public async Task<bool> PushNotification(StandardPushMessage message, string use
116132profile = await _userProfileService . GetProfileByUserIdAsync ( userId ) ;
117133
118134if ( profile != null && profile . SendNotificationPush )
119- await _notificationProvider . SendAllNotifications ( message . Title , message . SubTitle , userId , string . Format ( "N{0}" , message . MessageId ) , ( ( int ) PushSoundTypes . Notifiation ) . ToString ( ) , true , 1 , "#000000" ) ;
120-
135+ {
136+ try
137+ {
138+ await _notificationProvider . SendAllNotifications ( message . Title , message . SubTitle , userId , string . Format ( "N{0}" , message . MessageId ) , ( ( int ) PushSoundTypes . Notifiation ) . ToString ( ) , true , 1 , "#000000" ) ;
139+ }
140+ catch ( Exception ex )
141+ {
142+ Framework . Logging . LogException ( ex ) ;
143+ }
144+ try
145+ {
146+ await _novuProvider . SendUserMessage ( message . Title , message . SubTitle , userId , message . DepartmentCode , string . Format ( "N{0}" , message . MessageId ) , null ) ;
147+ }
148+ catch ( Exception ex )
149+ {
150+ Framework . Logging . LogException ( ex ) ;
151+ }
152+ }
121153return true ;
122154}
123155
@@ -151,7 +183,26 @@ public async Task<bool> PushCall(StandardPushCall call, string userId, UserProfi
151183color = priority . Color ;
152184
153185if ( profile != null && profile . SendPush )
154- await _notificationProvider . SendAllNotifications ( call . SubTitle , call . Title , userId , string . Format ( "C{0}" , call . CallId ) , ConvertCallPriorityToSound ( ( int ) call . Priority , priority ) , true , call . ActiveCallCount , color ) ;
186+ {
187+ // Legacy Push Notifications (Azure)
188+ try
189+ {
190+ await _notificationProvider . SendAllNotifications ( call . SubTitle , call . Title , userId , string . Format ( "C{0}" , call . CallId ) , ConvertCallPriorityToSound ( ( int ) call . Priority , priority ) , true , call . ActiveCallCount , color ) ;
191+ }
192+ catch ( Exception ex )
193+ {
194+ Framework . Logging . LogException ( ex ) ;
195+ }
196+
197+ try
198+ {
199+ await _novuProvider . SendUserDispatch ( call . Title , call . SubTitle , userId , call . DepartmentCode , string . Format ( "C{0}" , call . CallId ) , ConvertCallPriorityToSound ( ( int ) call . Priority , priority ) , true , call . ActiveCallCount , color ) ;
200+ }
201+ catch ( Exception ex )
202+ {
203+ Framework . Logging . LogException ( ex ) ;
204+ }
205+ }
155206
156207return true ;
157208}
@@ -168,15 +219,6 @@ public async Task<bool> PushCallUnit(StandardPushCall call, int unitId, Departme
168219if ( priority != null )
169220color = priority . Color ;
170221
171- try
172- {
173- await _unitNotificationProvider . SendAllNotifications ( call . SubTitle , call . Title , unitId , string . Format ( "C{0}" , call . CallId ) , ConvertCallPriorityToSound ( ( int ) call . Priority , priority ) , true , call . ActiveCallCount , color ) ;
174- }
175- catch ( Exception ex )
176- {
177- Framework . Logging . LogException ( ex ) ;
178- }
179-
180222try
181223{
182224await _novuProvider . SendUnitDispatch ( call . Title , call . SubTitle , unitId , call . DepartmentCode , string . Format ( "C{0}" , call . CallId ) , ConvertCallPriorityToSound ( ( int ) call . Priority , priority ) , true , call . ActiveCallCount , color ) ;