-
Notifications
You must be signed in to change notification settings - Fork 5
/
messages_builder.go
54 lines (46 loc) · 1.63 KB
/
messages_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package notifications
import (
"fmt"
"firebase.google.com/go/v4/messaging"
"github.com/desmos-labs/athena/v2/types"
)
// BuildMessage builds the notification message that should be sent based on the given recipient, notification and data
func (m *Module) BuildMessage(recipient types.NotificationRecipient, config types.NotificationData) (types.NotificationMessage, error) {
var androidConfig *messaging.AndroidConfig
var apnsConfig *messaging.APNSConfig
var webpushConfig *messaging.WebpushConfig
if dataWithConfig, ok := config.(types.NotificationDataWithConfig); ok {
androidConfig = dataWithConfig.GetAndroidConfig()
apnsConfig = dataWithConfig.GetAPNSConfig()
webpushConfig = dataWithConfig.GetWebpushConfig()
}
switch recipient := recipient.(type) {
case *types.NotificationTopicRecipient:
return types.NewSingleNotificationMessage(&messaging.Message{
Topic: recipient.Topic,
Data: config.GetAdditionalData(),
Notification: config.GetNotification(),
Android: androidConfig,
APNS: apnsConfig,
Webpush: webpushConfig,
}), nil
case *types.NotificationUserRecipient:
tokens, err := m.getUserTokens(recipient.Address)
if err != nil {
return nil, err
}
if len(tokens) == 0 {
return nil, nil
}
return types.NewMultiNotificationMessage(&messaging.MulticastMessage{
Tokens: tokens,
Data: config.GetAdditionalData(),
Notification: config.GetNotification(),
Android: androidConfig,
APNS: apnsConfig,
Webpush: webpushConfig,
}), nil
default:
return nil, fmt.Errorf("invalid notification recipient: %T", recipient)
}
}