forked from linkedin/Iris-message-processor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fcmVendor.go
86 lines (68 loc) · 2.22 KB
/
fcmVendor.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"net/http"
"time"
fcm "github.com/appleboy/go-fcm"
gometrics "github.com/rcrowley/go-metrics"
)
type FCMVendor struct {
*Vendor
fcmClient *fcm.Client
}
const fcmMaxBodyLen = 64
// NewFCMVendor create new FCMVendor instance
func NewFCMVendor(dbClient *DbClient, config *Config, logger *Logger, metricsRegistry *gometrics.Registry) *FCMVendor {
vendor := NewVendor(dbClient, config, logger, "fcm")
httpClient := &http.Client{Timeout: config.FCMTimeout * time.Second}
if config.OutboundProxyParsedURL != nil {
httpClient.Transport = &http.Transport{
Proxy: http.ProxyURL(config.OutboundProxyParsedURL),
}
}
// Create a FCM client
fcmClient, err := fcm.NewClient(config.FCMAPIKey, fcm.WithHTTPClient(httpClient))
if err != nil {
logger.Errorf("Failed to properly initialize FCM client with error: %s", err)
}
fcmVendor := FCMVendor{
Vendor: vendor,
fcmClient: fcmClient,
}
if appConfig.MetricsDryRun == false && metricsRegistry != nil {
fcmVendor.Vendor.RegisterMetrics(*metricsRegistry)
}
return &fcmVendor
}
func (f *FCMVendor) SendMessage(messageQueueItem MessageQueueItem) bool {
// if message is not part of an incident skip sending
if messageQueueItem.incidentID == 0 {
return true
}
// truncate to fit in push notification char length limit
if len(messageQueueItem.plan) > fcmMaxBodyLen {
messageQueueItem.plan = messageQueueItem.plan[:fcmMaxBodyLen]
}
if len(messageQueueItem.message.Body) > fcmMaxBodyLen {
messageQueueItem.message.Body = messageQueueItem.message.Body[:fcmMaxBodyLen]
}
// send messages to all devices registered to this user
for _, deviceKey := range messageQueueItem.message.DeviceIDs {
msg := &fcm.Message{
To: deviceKey,
Notification: &fcm.Notification{
Title: messageQueueItem.plan,
Body: messageQueueItem.message.Body,
},
}
// Send the message without retries.
_, err := f.fcmClient.Send(msg)
if err != nil {
f.Vendor.logger.Errorf("Failed sending fcm push notification with error %s. Message: %+v", err, messageQueueItem)
f.Vendor.counterFailures.Inc(1)
} else {
f.Vendor.counterSuccesses.Inc(1)
}
}
// we don't want to retry push messages so always return true even if sending them failed
return true
}