🐛 Trying to fix push notification

This commit is contained in:
LittleSheep 2025-07-03 00:05:00 +08:00
parent 6449926334
commit 19b1e957dd

View File

@ -30,6 +30,9 @@ public class NotificationService(
string deviceToken string deviceToken
) )
{ {
var now = SystemClock.Instance.GetCurrentInstant();
// First check if a matching subscription exists
var existingSubscription = await db.NotificationPushSubscriptions var existingSubscription = await db.NotificationPushSubscriptions
.Where(s => s.AccountId == account.Id) .Where(s => s.AccountId == account.Id)
.Where(s => s.DeviceId == deviceId || s.DeviceToken == deviceToken) .Where(s => s.DeviceId == deviceId || s.DeviceToken == deviceToken)
@ -37,11 +40,18 @@ public class NotificationService(
if (existingSubscription is not null) if (existingSubscription is not null)
{ {
// Reset these audit fields to renew the lifecycle of this device token // Update the existing subscription directly in the database
await db.NotificationPushSubscriptions
.Where(s => s.Id == existingSubscription.Id)
.ExecuteUpdateAsync(setters => setters
.SetProperty(s => s.DeviceId, deviceId)
.SetProperty(s => s.DeviceToken, deviceToken)
.SetProperty(s => s.UpdatedAt, now));
// Return the updated subscription
existingSubscription.DeviceId = deviceId; existingSubscription.DeviceId = deviceId;
existingSubscription.DeviceToken = deviceToken; existingSubscription.DeviceToken = deviceToken;
db.Update(existingSubscription); existingSubscription.UpdatedAt = now;
await db.SaveChangesAsync();
return existingSubscription; return existingSubscription;
} }