🐛 Bug fixes in apple push notifications
This commit is contained in:
parent
bf6dbfdca0
commit
39d9d8a839
@ -132,11 +132,9 @@ public class NotificationService
|
|||||||
.Where(s => s.AccountId == notification.AccountId)
|
.Where(s => s.AccountId == notification.AccountId)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
var tasks = new List<Task>();
|
var tasks = subscribers
|
||||||
foreach (var subscriber in subscribers)
|
.Select(subscriber => _PushSingleNotification(notification, subscriber))
|
||||||
{
|
.ToList();
|
||||||
tasks.Add(_PushSingleNotification(notification, subscriber));
|
|
||||||
}
|
|
||||||
|
|
||||||
await Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
@ -166,7 +164,7 @@ public class NotificationService
|
|||||||
}).ToList();
|
}).ToList();
|
||||||
await _db.BulkInsertAsync(notifications);
|
await _db.BulkInsertAsync(notifications);
|
||||||
}
|
}
|
||||||
|
|
||||||
var subscribers = await _db.NotificationPushSubscriptions
|
var subscribers = await _db.NotificationPushSubscriptions
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
var tasks = new List<Task>();
|
var tasks = new List<Task>();
|
||||||
@ -175,6 +173,7 @@ public class NotificationService
|
|||||||
notification.AccountId = subscriber.AccountId;
|
notification.AccountId = subscriber.AccountId;
|
||||||
tasks.Add(_PushSingleNotification(notification, subscriber));
|
tasks.Add(_PushSingleNotification(notification, subscriber));
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,7 +189,7 @@ public class NotificationService
|
|||||||
}).ToList();
|
}).ToList();
|
||||||
await _db.BulkInsertAsync(notifications);
|
await _db.BulkInsertAsync(notifications);
|
||||||
}
|
}
|
||||||
|
|
||||||
var accountsId = accounts.Select(x => x.Id).ToList();
|
var accountsId = accounts.Select(x => x.Id).ToList();
|
||||||
var subscribers = await _db.NotificationPushSubscriptions
|
var subscribers = await _db.NotificationPushSubscriptions
|
||||||
.Where(s => accountsId.Contains(s.AccountId))
|
.Where(s => accountsId.Contains(s.AccountId))
|
||||||
@ -201,54 +200,76 @@ public class NotificationService
|
|||||||
notification.AccountId = subscriber.AccountId;
|
notification.AccountId = subscriber.AccountId;
|
||||||
tasks.Add(_PushSingleNotification(notification, subscriber));
|
tasks.Add(_PushSingleNotification(notification, subscriber));
|
||||||
}
|
}
|
||||||
|
|
||||||
await Task.WhenAll(tasks);
|
await Task.WhenAll(tasks);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task _PushSingleNotification(Notification notification, NotificationPushSubscription subscription)
|
private async Task _PushSingleNotification(Notification notification, NotificationPushSubscription subscription)
|
||||||
{
|
{
|
||||||
switch (subscription.Provider)
|
try
|
||||||
{
|
{
|
||||||
case NotificationPushProvider.Google:
|
var body = string.Empty;
|
||||||
if (_fcm == null)
|
switch (subscription.Provider)
|
||||||
throw new InvalidOperationException("The firebase cloud messaging is not initialized.");
|
{
|
||||||
await _fcm.SendAsync(new
|
case NotificationPushProvider.Google:
|
||||||
{
|
if (_fcm == null)
|
||||||
message = new
|
throw new InvalidOperationException("The firebase cloud messaging is not initialized.");
|
||||||
|
|
||||||
|
if (!string.IsNullOrEmpty(notification.Subtitle) || !string.IsNullOrEmpty(notification.Content))
|
||||||
{
|
{
|
||||||
token = subscription.DeviceToken,
|
body = string.Join("\n",
|
||||||
notification = new
|
notification.Subtitle ?? string.Empty,
|
||||||
{
|
notification.Content ?? string.Empty).Trim();
|
||||||
title = notification.Title,
|
|
||||||
body = string.Join("\n", notification.Subtitle, notification.Content),
|
|
||||||
},
|
|
||||||
data = notification.Meta
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
break;
|
await _fcm.SendAsync(new
|
||||||
case NotificationPushProvider.Apple:
|
|
||||||
if (_apns == null)
|
|
||||||
throw new InvalidOperationException("The apple notification push service is not initialized.");
|
|
||||||
await _apns.SendAsync(new
|
|
||||||
{
|
{
|
||||||
apns = new
|
message = new
|
||||||
{
|
{
|
||||||
alert = new
|
token = subscription.DeviceToken,
|
||||||
|
notification = new
|
||||||
{
|
{
|
||||||
title = notification.Title,
|
title = notification.Title ?? string.Empty, body
|
||||||
subtitle = notification.Subtitle,
|
},
|
||||||
content = notification.Content,
|
data = notification.Meta ?? new Dictionary<string, object>()
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
break;
|
||||||
|
|
||||||
|
case NotificationPushProvider.Apple:
|
||||||
|
if (_apns == null)
|
||||||
|
throw new InvalidOperationException("The apple notification push service is not initialized.");
|
||||||
|
|
||||||
|
await _apns.SendAsync(new
|
||||||
|
{
|
||||||
|
aps = new
|
||||||
|
{
|
||||||
|
alert = new
|
||||||
|
{
|
||||||
|
title = notification.Title ?? string.Empty,
|
||||||
|
subtitle = notification.Subtitle ?? string.Empty,
|
||||||
|
body = notification.Content ?? string.Empty
|
||||||
|
}
|
||||||
|
},
|
||||||
|
meta = notification.Meta ?? new Dictionary<string, object>()
|
||||||
},
|
},
|
||||||
meta = notification.Meta,
|
deviceToken: subscription.DeviceToken,
|
||||||
},
|
apnsId: notification.Id.ToString(),
|
||||||
deviceToken: subscription.DeviceToken,
|
apnsPriority: notification.Priority,
|
||||||
apnsId: notification.Id.ToString(),
|
apnPushType: ApnPushType.Alert
|
||||||
apnsPriority: notification.Priority,
|
);
|
||||||
apnPushType: ApnPushType.Alert
|
break;
|
||||||
);
|
|
||||||
break;
|
default:
|
||||||
default:
|
throw new InvalidOperationException($"Provider not supported: {subscription.Provider}");
|
||||||
throw new InvalidOperationException($"Provider not supported: {subscription.Provider}");
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Log the exception
|
||||||
|
// Consider implementing a retry mechanism
|
||||||
|
// Rethrow or handle as needed
|
||||||
|
throw new Exception($"Failed to send notification to {subscription.Provider}: {ex.Message}", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user