@@ -2,6 +2,7 @@ package push
22
33import (
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}
0 commit comments