Skip to content

Commit c9b7569

Browse files
committed
code/push: only send push to most recent token
1 parent cad9b0a commit c9b7569

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

pkg/code/push/data.go

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package push
22

33
import (
44
"context"
5+
"slices"
56

67
"github.com/sirupsen/logrus"
78

@@ -98,47 +99,43 @@ func sendMutableNotificationToOwner(
9899
log.WithError(err).Warn("failure getting push tokens for owner")
99100
return err
100101
}
102+
if len(pushTokenRecords) == 0 {
103+
log.Debug("No push tokens found")
104+
return nil
105+
}
101106

102-
log.WithField("tokens", pushTokenRecords).Info("Found push tokens")
103-
104-
seenPushTokens := make(map[string]struct{})
105-
for _, pushTokenRecord := range pushTokenRecords {
106-
// Dedup push tokens, since they may appear more than once per app install
107-
if _, ok := seenPushTokens[pushTokenRecord.PushToken]; ok {
108-
continue
109-
}
110-
111-
log := log.WithField("push_token", pushTokenRecord.PushToken)
107+
log.WithField("tokens", pushTokenRecords).Debug("Found push tokens")
108+
slices.SortFunc(pushTokenRecords, func(a, b *push_data.Record) int {
109+
return b.CreatedAt.Compare(a.CreatedAt)
110+
})
112111

113-
// Try push
114-
var err error
115-
switch pushTokenRecord.TokenType {
116-
case push_data.TokenTypeFcmApns:
117-
log.Info("Sending mutable push")
118-
err = pusher.SendMutableAPNSPush(
119-
ctx,
120-
pushTokenRecord.PushToken,
121-
titleKey,
122-
string(notificationType),
123-
titleKey, // All mutable pushes have a thread ID that's the title
124-
kvs,
125-
)
126-
case push_data.TokenTypeFcmAndroid:
127-
// todo: anything special required for Android?
128-
log.Info("Sending data push")
129-
err = pusher.SendDataPush(
130-
ctx,
131-
pushTokenRecord.PushToken,
132-
kvs,
133-
)
134-
}
112+
pushTokenRecord := pushTokenRecords[0]
113+
log = log.WithField("push_token", pushTokenRecord.PushToken)
135114

136-
if err != nil {
137-
log.WithError(err).Warn("failure sending push notification")
138-
onPushError(ctx, data, pusher, pushTokenRecord)
139-
}
115+
// Try push
116+
switch pushTokenRecord.TokenType {
117+
case push_data.TokenTypeFcmApns:
118+
err = pusher.SendMutableAPNSPush(
119+
ctx,
120+
pushTokenRecord.PushToken,
121+
titleKey,
122+
string(notificationType),
123+
titleKey, // All mutable pushes have a thread ID that's the title
124+
kvs,
125+
)
126+
case push_data.TokenTypeFcmAndroid:
127+
// todo: anything special required for Android?
128+
err = pusher.SendDataPush(
129+
ctx,
130+
pushTokenRecord.PushToken,
131+
kvs,
132+
)
133+
}
140134

141-
seenPushTokens[pushTokenRecord.PushToken] = struct{}{}
135+
if err != nil {
136+
log.WithError(err).Warn("failure sending push notification")
137+
onPushError(ctx, data, pusher, pushTokenRecord)
142138
}
139+
143140
return nil
144141
}

pkg/code/server/grpc/chat/v2/server.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,8 +1425,6 @@ func (s *Server) sendPushNotifications(chatId chat.ChatId, chatTitle string, sen
14251425
return
14261426
}
14271427

1428-
log.WithField("members", members).Info("Found members for push")
1429-
14301428
var eg errgroup.Group
14311429
eg.SetLimit(min(32, len(members)))
14321430

@@ -1437,7 +1435,7 @@ func (s *Server) sendPushNotifications(chatId chat.ChatId, chatTitle string, sen
14371435
"isSender": m.MemberId == sender,
14381436
"isMuted": m.IsMuted,
14391437
"isUnsubscribed": m.IsUnsubscribed,
1440-
}).Info("skipping member")
1438+
}).Debug("skipping member")
14411439

14421440
continue
14431441
}
@@ -1450,7 +1448,6 @@ func (s *Server) sendPushNotifications(chatId chat.ChatId, chatTitle string, sen
14501448

14511449
m := m
14521450
eg.Go(func() error {
1453-
log.WithField("member", m.MemberId.String()).Info("sending push notification")
14541451
err = push_util.SendChatMessagePushNotificationV2(
14551452
context.Background(),
14561453
s.data,

0 commit comments

Comments
 (0)