-
Notifications
You must be signed in to change notification settings - Fork 10
/
notifications.go
116 lines (99 loc) · 2.41 KB
/
notifications.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
package main
import (
"context"
"log"
"os"
"sync"
"firebase.google.com/go/messaging"
firebase "firebase.google.com/go"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)
var firebaseMu sync.Mutex
func firebaseApp() (*firebase.App, error) {
firebaseMu.Lock()
defer firebaseMu.Unlock()
creds, err := google.CredentialsFromJSON(context.Background(), []byte(os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")), "https://www.googleapis.com/auth/firebase.messaging")
if err != nil {
return nil, err
}
return firebase.NewApp(context.Background(), nil, option.WithCredentials(creds))
}
func notifyDataMessage(data map[string]string, token string) error {
app, err := firebaseApp()
if err != nil {
return err
}
client, err := app.Messaging(context.Background())
if err != nil {
return err
}
iosCustomData := make(map[string]interface{})
for key, value := range data {
iosCustomData[key] = value
}
_, err = client.Send(context.Background(), &messaging.Message{
Token: token,
Data: data,
Android: &messaging.AndroidConfig{
Priority: "high",
},
APNS: &messaging.APNSConfig{
Headers: map[string]string{
"apns-priority": "10",
},
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
ContentAvailable: true,
},
},
},
})
return err
}
func notifyAlertMessage(title, body string, data map[string]string, token string) error {
app, err := firebaseApp()
if err != nil {
return err
}
client, err := app.Messaging(context.Background())
if err != nil {
return err
}
if data["click_action"] == "" {
data["click_action"] = "FLUTTER_NOTIFICATION_CLICK"
}
data["title"] = title
data["body"] = body
iosCustomData := make(map[string]interface{})
for key, value := range data {
iosCustomData[key] = value
}
status, err := client.Send(context.Background(), &messaging.Message{
Token: token,
Data: data,
Android: &messaging.AndroidConfig{
CollapseKey: "breez",
Priority: "high",
},
APNS: &messaging.APNSConfig{
Headers: map[string]string{
"apns-priority": "5",
},
Payload: &messaging.APNSPayload{
Aps: &messaging.Aps{
Alert: &messaging.ApsAlert{
Title: title,
Body: body,
},
CustomData: iosCustomData,
},
},
},
})
log.Printf("Alert Notification Status = %v, Error = %v", status, err)
return err
}
func isUnregisteredError(err error) bool {
return messaging.IsRegistrationTokenNotRegistered(err)
}