7
7
8
8
"github.com/code-payments/code-server/pkg/code/common"
9
9
code_data "github.com/code-payments/code-server/pkg/code/data"
10
+ push_data "github.com/code-payments/code-server/pkg/code/data/push"
10
11
push_lib "github.com/code-payments/code-server/pkg/push"
11
12
)
12
13
@@ -18,11 +19,11 @@ const (
18
19
chatMessageDataPush dataPushType = "ChatMessage"
19
20
)
20
21
21
- // sendDataPushNotificationToOwner is a generic utility for sending data push
22
+ // sendRawDataPushNotificationToOwner is a generic utility for sending raw data push
22
23
// notification to the devices linked to an owner account.
23
24
//
24
25
// todo: Duplicated code with other send push utitilies
25
- func sendDataPushNotificationToOwner (
26
+ func sendRawDataPushNotificationToOwner (
26
27
ctx context.Context ,
27
28
data code_data.Provider ,
28
29
pusher push_lib.Provider ,
@@ -31,7 +32,7 @@ func sendDataPushNotificationToOwner(
31
32
kvs map [string ]string ,
32
33
) error {
33
34
log := logrus .StandardLogger ().WithFields (logrus.Fields {
34
- "method" : "sendDataPushNotificationToOwner " ,
35
+ "method" : "sendRawDataPushNotificationToOwner " ,
35
36
"owner" : owner .PublicKey ().ToBase58 (),
36
37
})
37
38
@@ -68,3 +69,69 @@ func sendDataPushNotificationToOwner(
68
69
}
69
70
return nil
70
71
}
72
+
73
+ // sendMutableNotificationToOwner is a generic utility for sending mutable
74
+ // push notification to the devices linked to an owner account. It's a
75
+ // special data push where the notification content is replaced by the contents
76
+ // of a kv pair payload.
77
+ //
78
+ // todo: Duplicated code with other send push utitilies
79
+ func sendMutableNotificationToOwner (
80
+ ctx context.Context ,
81
+ data code_data.Provider ,
82
+ pusher push_lib.Provider ,
83
+ owner * common.Account ,
84
+ notificationType dataPushType ,
85
+ titleKey string ,
86
+ kvs map [string ]string ,
87
+ ) error {
88
+ log := logrus .StandardLogger ().WithFields (logrus.Fields {
89
+ "method" : "sendMutableNotificationToOwner" ,
90
+ "owner" : owner .PublicKey ().ToBase58 (),
91
+ })
92
+
93
+ kvs [dataPushTypeKey ] = string (notificationType )
94
+
95
+ pushTokenRecords , err := getPushTokensForOwner (ctx , data , owner )
96
+ if err != nil {
97
+ log .WithError (err ).Warn ("failure getting push tokens for owner" )
98
+ return err
99
+ }
100
+
101
+ seenPushTokens := make (map [string ]struct {})
102
+ for _ , pushTokenRecord := range pushTokenRecords {
103
+ // Dedup push tokens, since they may appear more than once per app install
104
+ if _ , ok := seenPushTokens [pushTokenRecord .PushToken ]; ok {
105
+ continue
106
+ }
107
+
108
+ log := log .WithField ("push_token" , pushTokenRecord .PushToken )
109
+
110
+ // Try push
111
+ var err error
112
+ switch pushTokenRecord .TokenType {
113
+ case push_data .TokenTypeFcmApns :
114
+ err = pusher .SendMutableAPNSPush (
115
+ ctx ,
116
+ pushTokenRecord .PushToken ,
117
+ titleKey ,
118
+ kvs ,
119
+ )
120
+ case push_data .TokenTypeFcmAndroid :
121
+ // todo: anything special required for Android?
122
+ err = pusher .SendDataPush (
123
+ ctx ,
124
+ pushTokenRecord .PushToken ,
125
+ kvs ,
126
+ )
127
+ }
128
+
129
+ if err != nil {
130
+ log .WithError (err ).Warn ("failure sending push notification" )
131
+ onPushError (ctx , data , pusher , pushTokenRecord )
132
+ }
133
+
134
+ seenPushTokens [pushTokenRecord .PushToken ] = struct {}{}
135
+ }
136
+ return nil
137
+ }
0 commit comments