🐛 Fix send notification api push notification twice
This commit is contained in:
@@ -147,20 +147,21 @@ public class NotificationController(
|
|||||||
[FromQuery] bool save = false
|
[FromQuery] bool save = false
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
await nty.SendNotificationBatch(
|
var notification = new SnNotification
|
||||||
new SnNotification
|
{
|
||||||
{
|
CreatedAt = SystemClock.Instance.GetCurrentInstant(),
|
||||||
CreatedAt = SystemClock.Instance.GetCurrentInstant(),
|
UpdatedAt = SystemClock.Instance.GetCurrentInstant(),
|
||||||
UpdatedAt = SystemClock.Instance.GetCurrentInstant(),
|
Topic = request.Topic,
|
||||||
Topic = request.Topic,
|
Title = request.Title,
|
||||||
Title = request.Title,
|
Subtitle = request.Subtitle,
|
||||||
Subtitle = request.Subtitle,
|
Content = request.Content,
|
||||||
Content = request.Content,
|
};
|
||||||
Meta = request.Meta ?? [],
|
if (request.Meta != null)
|
||||||
},
|
{
|
||||||
request.AccountId,
|
notification.Meta = request.Meta;
|
||||||
save
|
}
|
||||||
);
|
|
||||||
|
await nty.SendNotificationBatch(notification, request.AccountId, save);
|
||||||
return Ok();
|
return Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -251,18 +251,29 @@ public class PushService
|
|||||||
notification.Meta
|
notification.Meta
|
||||||
);
|
);
|
||||||
|
|
||||||
// WS first
|
// Send to each account
|
||||||
foreach (var account in accounts)
|
foreach (var account in accounts)
|
||||||
{
|
{
|
||||||
notification.AccountId = account;
|
notification.AccountId = account;
|
||||||
|
|
||||||
|
// WebSocket
|
||||||
WebSocketService.SendPacketToAccount(account, new WebSocketPacket
|
WebSocketService.SendPacketToAccount(account, new WebSocketPacket
|
||||||
{
|
{
|
||||||
Type = "notifications.new",
|
Type = "notifications.new",
|
||||||
Data = notification
|
Data = notification
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
await DeliverPushNotification(notification);
|
// Push notifications
|
||||||
|
var subscriptions = await _db.PushSubscriptions
|
||||||
|
.Where(s => s.AccountId == account)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
if (subscriptions.Count > 0)
|
||||||
|
{
|
||||||
|
var tasks = subscriptions.Select(sub => SendPushNotificationAsync(sub, notification)).ToList();
|
||||||
|
await Task.WhenAll(tasks);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task SendPushNotificationAsync(SnNotificationPushSubscription subscription,
|
private async Task SendPushNotificationAsync(SnNotificationPushSubscription subscription,
|
||||||
@@ -391,4 +402,4 @@ public class PushService
|
|||||||
}));
|
}));
|
||||||
await _db.SaveChangesAsync();
|
await _db.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user