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- { }
47+ // 2) Android -> FCM
48+ if ( pushUri . PlatformType == ( int ) Platforms . Android )
49+ return await _novuProvider . UpdateUserSubscriberFcm ( pushUri . UserId , code , pushUri . DeviceId ) ;
5650
57- //if (existingPushUri == null)
58- //pushUri = _pushUriService.SavePushUri(pushUri);
59-
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,29 +61,22 @@ public async Task<bool> UnRegister(PushUri pushUri)
7261
7362public async Task < bool > RegisterUnit ( PushUri pushUri )
7463{
75- //string deviceId = pushUri.DeviceId;
76- List < PushRegistrationDescription > usersDevices = null ;
77-
78- //try
79- //{
80- //usersDevices = await _unitNotificationProvider.GetRegistrationsByUUID(pushUri.PushLocation);
81- //}
82- //catch (TimeoutException)
83- //{ }
64+ if ( pushUri == null || ! pushUri . UnitId . HasValue || string . IsNullOrWhiteSpace ( pushUri . PushLocation ) )
65+ return false ;
8466
85- if ( pushUri . UnitId . HasValue && ! string . IsNullOrWhiteSpace ( pushUri . PushLocation ) )
86- await _novuProvider . UpdateUnitSubscriberFcm ( pushUri . UnitId . Value , pushUri . PushLocation , pushUri . DeviceId ) ;
67+ var unitId = pushUri . UnitId . Value ;
68+ var code = pushUri . PushLocation ;
8769
70+ // 1) iOS -> APNS
71+ if ( pushUri . PlatformType == ( int ) Platforms . iOS )
72+ return await _novuProvider . UpdateUnitSubscriberApns ( unitId , code , pushUri . DeviceId ) ;
8873
89- //if (usersDevices == null || !usersDevices.Any(x => x.Tags.Contains(string.Format("unitId:{0}", pushUri.UnitId.ToString()))))
90- //await _unitNotificationProvider.RegisterPush(pushUri);
91- //else
92- //{
93- //await _unitNotificationProvider.UnRegisterPushByUUID(pushUri.PushLocation);
94- //await _unitNotificationProvider.RegisterPush(pushUri);
95- //}
74+ // 2) Android -> FCM
75+ if ( pushUri . PlatformType == ( int ) Platforms . Android )
76+ return await _novuProvider . UpdateUnitSubscriberFcm ( unitId , code , pushUri . DeviceId ) ;
9677
97- return true ;
78+ // 3) TODO: Web Push (other platforms)
79+ return false ;
9880}
9981
10082public async Task < bool > UnRegisterUnit ( PushUri pushUri )
@@ -118,7 +100,25 @@ public async Task<bool> PushMessage(StandardPushMessage message, string userId,
118100profile = await _userProfileService . GetProfileByUserIdAsync ( userId ) ;
119101
120102if ( profile != null && profile . SendMessagePush )
121- 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+ }
122122
123123return true ;
124124}
@@ -132,8 +132,24 @@ public async Task<bool> PushNotification(StandardPushMessage message, string use
132132profile = await _userProfileService . GetProfileByUserIdAsync ( userId ) ;
133133
134134if ( profile != null && profile . SendNotificationPush )
135- await _notificationProvider . SendAllNotifications ( message . Title , message . SubTitle , userId , string . Format ( "N{0}" , message . MessageId ) , ( ( int ) PushSoundTypes . Notifiation ) . ToString ( ) , true , 1 , "#000000" ) ;
136-
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+ }
137153return true ;
138154}
139155
@@ -167,7 +183,26 @@ public async Task<bool> PushCall(StandardPushCall call, string userId, UserProfi
167183color = priority . Color ;
168184
169185if ( profile != null && profile . SendPush )
170- 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+ }
171206
172207return true ;
173208}
@@ -184,15 +219,6 @@ public async Task<bool> PushCallUnit(StandardPushCall call, int unitId, Departme
184219if ( priority != null )
185220color = priority . Color ;
186221
187- try
188- {
189- await _unitNotificationProvider . SendAllNotifications ( call . SubTitle , call . Title , unitId , string . Format ( "C{0}" , call . CallId ) , ConvertCallPriorityToSound ( ( int ) call . Priority , priority ) , true , call . ActiveCallCount , color ) ;
190- }
191- catch ( Exception ex )
192- {
193- Framework . Logging . LogException ( ex ) ;
194- }
195-
196222try
197223{
198224await _novuProvider . SendUnitDispatch ( call . Title , call . SubTitle , unitId , call . DepartmentCode , string . Format ( "C{0}" , call . CallId ) , ConvertCallPriorityToSound ( ( int ) call . Priority , priority ) , true , call . ActiveCallCount , color ) ;