Skip to content

Commit a6fe7fb

Browse files
authored
Improve chat message pushes for APNS (#51)
* Improve chat message pushes for APNS * Set default body of mutable APNS push to ... * Fix comments and adjust code placement
1 parent 76d9c8f commit a6fe7fb

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed

pkg/code/push/data.go

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/code-payments/code-server/pkg/code/common"
99
code_data "github.com/code-payments/code-server/pkg/code/data"
10+
push_data "github.com/code-payments/code-server/pkg/code/data/push"
1011
push_lib "github.com/code-payments/code-server/pkg/push"
1112
)
1213

@@ -18,11 +19,11 @@ const (
1819
chatMessageDataPush dataPushType = "ChatMessage"
1920
)
2021

21-
// sendDataPushNotificationToOwner is a generic utility for sending data push
22+
// sendRawDataPushNotificationToOwner is a generic utility for sending raw data push
2223
// notification to the devices linked to an owner account.
2324
//
2425
// todo: Duplicated code with other send push utitilies
25-
func sendDataPushNotificationToOwner(
26+
func sendRawDataPushNotificationToOwner(
2627
ctx context.Context,
2728
data code_data.Provider,
2829
pusher push_lib.Provider,
@@ -31,7 +32,7 @@ func sendDataPushNotificationToOwner(
3132
kvs map[string]string,
3233
) error {
3334
log := logrus.StandardLogger().WithFields(logrus.Fields{
34-
"method": "sendDataPushNotificationToOwner",
35+
"method": "sendRawDataPushNotificationToOwner",
3536
"owner": owner.PublicKey().ToBase58(),
3637
})
3738

@@ -68,3 +69,69 @@ func sendDataPushNotificationToOwner(
6869
}
6970
return nil
7071
}
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+
}

pkg/code/push/notifications.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,13 @@ func SendChatMessagePushNotification(
199199
"message_content": base64.StdEncoding.EncodeToString(marshalledContent),
200200
}
201201

202-
err = sendDataPushNotificationToOwner(
202+
err = sendMutableNotificationToOwner(
203203
ctx,
204204
data,
205205
pusher,
206206
owner,
207207
chatMessageDataPush,
208+
chatTitle,
208209
kvs,
209210
)
210211
if err != nil {

pkg/push/fcm/provider.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ func (p *provider) SendLocalizedAndroidPush(ctx context.Context, pushToken, titl
130130
})
131131
}
132132

133+
// SendMutableAPNSPush implements push.Provider.SendMutableAPNSPush
134+
func (p *provider) SendMutableAPNSPush(ctx context.Context, pushToken, titleKey string, kvs map[string]string) error {
135+
defer metrics.TraceMethodCall(ctx, metricsStructName, "SendMutableAPNSPush").End()
136+
137+
_, err := p.client.Send(ctx, &messaging.Message{
138+
Token: pushToken,
139+
Data: kvs,
140+
APNS: &messaging.APNSConfig{
141+
Payload: &messaging.APNSPayload{
142+
Aps: &messaging.Aps{
143+
Alert: &messaging.ApsAlert{
144+
TitleLocKey: titleKey,
145+
Body: "...",
146+
},
147+
MutableContent: true,
148+
},
149+
},
150+
},
151+
})
152+
return err
153+
}
154+
133155
// SendDataPush implements push.Provider.SendDataPush
134156
func (p *provider) SendDataPush(ctx context.Context, pushToken string, kvs map[string]string) error {
135157
defer metrics.TraceMethodCall(ctx, metricsStructName, "SendDataPush").End()

pkg/push/memory/provider.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ func (p *provider) SendLocalizedAndroidPush(ctx context.Context, pushToken, titl
4444
return simulateSendingPush(pushToken)
4545
}
4646

47+
// SendMutableAPNSPush implements push.Provider.SendMutableAPNSPush
48+
func (p *provider) SendMutableAPNSPush(ctx context.Context, pushToken, titleKey string, kvs map[string]string) error {
49+
return simulateSendingPush(pushToken)
50+
}
51+
4752
// SendDataPush implements push.Provider.SendDataPush
4853
func (p *provider) SendDataPush(ctx context.Context, pushToken string, kvs map[string]string) error {
4954
return simulateSendingPush(pushToken)

pkg/push/provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ type Provider interface {
1414
// SendLocalizedAPNSPush sends a localized APNS push
1515
SendLocalizedAPNSPush(ctx context.Context, pushToken, titleKey, bodyKey string, bodyArgs ...string) error
1616

17+
// SendMutableAPNSPush sends a push over APNS with a text body that's mutable
18+
// on the client using custom key value pairs
19+
SendMutableAPNSPush(ctx context.Context, pushToken, titleKey string, kvs map[string]string) error
20+
1721
// SendLocalizedAndroidPush sends a localized Android push
1822
SendLocalizedAndroidPush(ctx context.Context, pushToken, titleKey, bodyKey string, bodyArgs ...string) error
1923

0 commit comments

Comments
 (0)