-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaefcm.go
162 lines (132 loc) · 3.43 KB
/
aefcm.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package aefire
import (
"firebase.google.com/go/messaging"
"strings"
"sync"
)
type MessageBuilder messaging.Message
func NewMessageBuilder() *MessageBuilder {
return &MessageBuilder{
Data: nil,
Notification: &messaging.Notification{},
Android: &messaging.AndroidConfig{
CollapseKey: "",
Priority: "",
TTL: nil,
RestrictedPackageName: "",
Data: StringMapOf(),
Notification: nil,
FCMOptions: nil,
},
Webpush: nil,
APNS: &messaging.APNSConfig{
Headers: StringMapOf(),
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
AlertString: "",
Alert: &messaging.ApsAlert{
Title: "",
SubTitle: "",
Body: "",
LocKey: "",
LocArgs: nil,
TitleLocKey: "",
TitleLocArgs: nil,
SubTitleLocKey: "",
SubTitleLocArgs: nil,
ActionLocKey: "",
LaunchImage: "",
},
Badge: nil,
Sound: "",
CriticalSound: nil,
ContentAvailable: false,
MutableContent: false,
Category: "",
ThreadID: "",
CustomData: nil,
},
CustomData: MapOf(),
},
FCMOptions: nil,
},
FCMOptions: nil,
Token: "",
Topic: "",
Condition: "",
}
}
func (m *MessageBuilder) AsMessage() *messaging.Message {
msg := messaging.Message(*m)
return &msg
}
func (m *MessageBuilder) SetToken(t string) *MessageBuilder {
m.Token = t
return m
}
func (m *MessageBuilder) Title(title string) *MessageBuilder {
m.Android.Data["title"] = title
if m.Android.Notification != nil {
m.Android.Notification.Title = title
}
m.APNS.Payload.Aps.Alert.Title = title
return m
}
func (m *MessageBuilder) Channel(channel string) *MessageBuilder {
if m.Android.Notification != nil {
m.Android.Notification.ChannelID = channel
}
m.Android.Data["channel"] = channel
return m
}
func (m *MessageBuilder) Body(body string) *MessageBuilder {
m.Android.Data["body"] = body
if m.Android.Notification != nil {
m.Android.Notification.Body = body
}
m.APNS.Payload.Aps.Alert.Body = body
return m
}
func (m *MessageBuilder) SetTopic(topic string) *MessageBuilder {
m.Topic = topic
return m
}
func (m *MessageBuilder) PutData(key, value string) *MessageBuilder {
m.Android.Data[key] = value
m.APNS.Payload.CustomData[key] = value
return m
}
func (m *MessageBuilder) Priority(p string) *MessageBuilder {
m.Android.Priority = p
return m
}
func (a *AEFire) MessageToTokens(tokens []string, msg *messaging.Message) map[string]error {
result := map[string]error{}
wg := sync.WaitGroup{}
wg.Add(len(tokens))
m := sync.Mutex{}
for _, token := range tokens {
go func(token string, msg messaging.Message) {
defer wg.Done()
msg.Token = token
_, err := a.FCM.Send(a, &msg)
if err != nil {
m.Lock()
result[token] = err
m.Unlock()
}
}(token, *msg)
}
wg.Wait()
return result
}
func TokenIID(token string) string {
if strings.Index(token, ":") > 0 {
return token[:strings.Index(token, ":")]
}
return ""
}
func (a *AEFire) MessageToQueryResults(msg *messaging.Message, collectionName, tokenFieldName, queryFieldName, queryValue string) map[string]error {
tokens := a.QueryStringField(a.Col(collectionName).Where(queryFieldName, "==", queryValue), tokenFieldName)
return a.MessageToTokens(StringMapValuesToSlice(tokens), msg)
}