🐛 Bug fixes in apple push notifications

This commit is contained in:
LittleSheep 2025-05-28 01:27:18 +08:00
parent bf6dbfdca0
commit 39d9d8a839

View File

@ -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);
} }
@ -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);
} }
@ -201,16 +200,28 @@ 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)
{ {
try
{
var body = string.Empty;
switch (subscription.Provider) switch (subscription.Provider)
{ {
case NotificationPushProvider.Google: case NotificationPushProvider.Google:
if (_fcm == null) if (_fcm == null)
throw new InvalidOperationException("The firebase cloud messaging is not initialized."); throw new InvalidOperationException("The firebase cloud messaging is not initialized.");
if (!string.IsNullOrEmpty(notification.Subtitle) || !string.IsNullOrEmpty(notification.Content))
{
body = string.Join("\n",
notification.Subtitle ?? string.Empty,
notification.Content ?? string.Empty).Trim();
}
await _fcm.SendAsync(new await _fcm.SendAsync(new
{ {
message = new message = new
@ -218,28 +229,29 @@ public class NotificationService
token = subscription.DeviceToken, token = subscription.DeviceToken,
notification = new notification = new
{ {
title = notification.Title, title = notification.Title ?? string.Empty, body
body = string.Join("\n", notification.Subtitle, notification.Content),
}, },
data = notification.Meta data = notification.Meta ?? new Dictionary<string, object>()
} }
}); });
break; break;
case NotificationPushProvider.Apple: case NotificationPushProvider.Apple:
if (_apns == null) if (_apns == null)
throw new InvalidOperationException("The apple notification push service is not initialized."); throw new InvalidOperationException("The apple notification push service is not initialized.");
await _apns.SendAsync(new await _apns.SendAsync(new
{ {
apns = new aps = new
{ {
alert = new alert = new
{ {
title = notification.Title, title = notification.Title ?? string.Empty,
subtitle = notification.Subtitle, subtitle = notification.Subtitle ?? string.Empty,
content = notification.Content, body = notification.Content ?? string.Empty
} }
}, },
meta = notification.Meta, meta = notification.Meta ?? new Dictionary<string, object>()
}, },
deviceToken: subscription.DeviceToken, deviceToken: subscription.DeviceToken,
apnsId: notification.Id.ToString(), apnsId: notification.Id.ToString(),
@ -247,8 +259,17 @@ public class NotificationService
apnPushType: ApnPushType.Alert 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);
}
}
} }