Skip to content

Commit 9b5e9eb

Browse files
authored
Merge pull request #264 from Resgrid/develop
RE1-T88 Looking at using FCM to forward APNS messages with Novu
2 parents 84356ce + 6306057 commit 9b5e9eb

File tree

1 file changed

+78
-23
lines changed

1 file changed

+78
-23
lines changed

Providers/Resgrid.Providers.Messaging/NovuProvider.cs

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Resgrid.Model;
88
using Resgrid.Model.Providers;
99
using Resgrid.Providers.Bus.Models;
10+
using SharpCompress.Common;
1011
using System.Text;
1112

1213

@@ -132,7 +133,7 @@ private async Task<bool> UpdateSubscriberFcm(string id, string token, string fcm
132133
}
133134
}
134135

135-
private async Task<bool> UpdateSubscriberApns(string id, string token, string apnsId)
136+
private async Task<bool> UpdateSubscriberApns(string id, string token, string apnsId, string fcmId)
136137
{
137138
try
138139
{
@@ -144,16 +145,40 @@ private async Task<bool> UpdateSubscriberApns(string id, string token, string ap
144145
request.Headers.Add("idempotency-key", Guid.NewGuid().ToString());
145146
request.Headers.Add("Authorization", $"ApiKey {ChatConfig.NovuSecretKey}");
146147

147-
var payload = new
148+
string jsonContent = string.Empty;
149+
if (!string.IsNullOrWhiteSpace(apnsId))
148150
{
149-
providerId = "apns",
150-
credentials = new
151+
var payload = new
151152
{
152-
deviceTokens = new string[] { token }
153-
},
154-
integrationIdentifier = apnsId
155-
};
156-
string jsonContent = JsonConvert.SerializeObject(payload, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
153+
providerId = "apns",
154+
credentials = new
155+
{
156+
deviceTokens = new string[] { token }
157+
},
158+
integrationIdentifier = apnsId
159+
};
160+
161+
jsonContent = JsonConvert.SerializeObject(payload, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
162+
}
163+
else if (!string.IsNullOrWhiteSpace(fcmId))
164+
{
165+
var payload = new
166+
{
167+
providerId = "fcm",
168+
credentials = new
169+
{
170+
deviceTokens = new string[] { token }
171+
},
172+
integrationIdentifier = fcmId
173+
};
174+
175+
jsonContent = JsonConvert.SerializeObject(payload, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
176+
}
177+
178+
if (string.IsNullOrWhiteSpace(jsonContent))
179+
{
180+
return false;
181+
}
157182

158183
request.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
159184
HttpResponseMessage response = await client.SendAsync(request);
@@ -197,7 +222,7 @@ public async Task<bool> UpdateUserSubscriberFcm(string userId, string code, stri
197222

198223
public async Task<bool> UpdateUserSubscriberApns(string userId, string code, string token)
199224
{
200-
return await UpdateSubscriberApns($"{code}_User_{userId}", token, ChatConfig.NovuResponderApnsProviderId);
225+
return await UpdateSubscriberApns($"{code}_User_{userId}", token, ChatConfig.NovuResponderApnsProviderId, null);
201226
}
202227

203228
public async Task<bool> UpdateUnitSubscriberFcm(int unitId, string code, string token)
@@ -207,7 +232,8 @@ public async Task<bool> UpdateUnitSubscriberFcm(int unitId, string code, string
207232

208233
public async Task<bool> UpdateUnitSubscriberApns(int unitId, string code, string token)
209234
{
210-
return await UpdateSubscriberApns($"{code}_Unit_{unitId}", token, ChatConfig.NovuUnitApnsProviderId);
235+
//return await UpdateSubscriberApns($"{code}_Unit_{unitId}", token, ChatConfig.NovuUnitApnsProviderId);
236+
return await UpdateSubscriberApns($"{code}_Unit_{unitId}", token, null, ChatConfig.NovuUnitFcmProviderId);
211237
}
212238

213239
private async Task<bool> SendNotification(string title, string body, string recipientId, string eventCode,
@@ -259,21 +285,50 @@ private async Task<bool> SendNotification(string title, string body, string reci
259285
type = type,
260286
}
261287
},
288+
apns = new
289+
{
290+
badge = count,
291+
sound = new
292+
{
293+
name = sound,
294+
critical = channelName == "calls" ? 1 : 0,
295+
volume = 1.0f
296+
},
297+
type = type,
298+
category = channelName,
299+
eventCode = eventCode,
300+
payload = new
301+
{
302+
aps = new
303+
{
304+
badge = count,
305+
sound = new
306+
{
307+
name = sound,
308+
critical = channelName == "calls" ? 1 : 0,
309+
volume = 1.0f
310+
},
311+
category = channelName,
312+
eventCode = eventCode,
313+
customType = type
314+
},
315+
},
316+
},
262317
},
263-
apns = new Dictionary<string, object>
264-
{
265-
["badge"] = count,
266-
["sound"] = new
318+
apns = new Dictionary<string, object>
267319
{
268-
name = sound,
269-
critical = channelName == "calls" ? 1 : 0,
270-
volume = 1.0f
320+
["badge"] = count,
321+
["sound"] = new
322+
{
323+
name = sound,
324+
critical = channelName == "calls" ? 1 : 0,
325+
volume = 1.0f
326+
},
327+
["type"] = type,
328+
["category"] = channelName,
329+
["eventCode"] = eventCode,
330+
["gcm.message_id"] = "123"
271331
},
272-
["type"] = type,
273-
["category"] = channelName,
274-
["eventCode"] = eventCode,
275-
["gcm.message_id"] = "123"
276-
},
277332
},
278333
to = new[]{ new
279334
{

0 commit comments

Comments
 (0)