forked from googlearchive/go-gcm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fcm.go
169 lines (137 loc) · 4.65 KB
/
fcm.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
163
164
165
166
167
168
169
package googlemessaging
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
)
const (
BaseURL = "https://fcm.googleapis.com/v1"
InstanceIdApiUrl = "https://iid.googleapis.com/iid/info"
FirebaseRequestScope = "https://www.googleapis.com/auth/firebase.messaging"
DefaultContentType = "application/json"
)
type ServiceAccount struct {
ServiceAccountType string `json:"type"`
ProjectId string `json:"project_id"`
PrivateKeyId string `json:"private_key_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
ClientId string `json:"client_id"`
AuthUri string `json:"auth_uri"`
TokenUri string `json:"token_uri"`
}
type fcmClient struct {
httpClient *http.Client
ServiceAccountConfig *ServiceAccount
}
func getServiceAccountFromContent(content string) (*ServiceAccount, error) {
serviceAccount := &ServiceAccount{}
err := json.Unmarshal([]byte(content), &serviceAccount)
if err != nil {
return nil, fmt.Errorf("error umarshalling service account file>%v", err)
}
return serviceAccount, nil
}
func NewFcmClient(serviceAccountFileContent string, ctx context.Context) (*fcmClient, error) {
serviceAccountConfig, err := getServiceAccountFromContent(serviceAccountFileContent)
if err != nil {
return nil, err
}
conf := &jwt.Config{
Email: serviceAccountConfig.ClientEmail,
PrivateKeyID: serviceAccountConfig.PrivateKeyId,
PrivateKey: []byte(serviceAccountConfig.PrivateKey),
Scopes: []string{FirebaseRequestScope},
TokenURL: google.JWTTokenURL,
}
return &fcmClient{
httpClient: conf.Client(ctx),
ServiceAccountConfig: serviceAccountConfig,
}, nil
}
// GetInstanceInfo returns app instance info. Reference: https://developers.google.com/instance-id/reference/server
func (c *fcmClient) GetInstanceInfo(token string) (*InstanceInformationResponse, error) {
instanceInfoUrl := fmt.Sprintf("%s/%s", InstanceIdApiUrl, token)
request, err := http.NewRequest(http.MethodGet, instanceInfoUrl, nil)
if err != nil {
return nil, fmt.Errorf("error build http request to %s", instanceInfoUrl)
}
request.Header.Set("access_token_auth", "true")
response, err := c.httpClient.Do(request)
if err != nil {
return nil, fmt.Errorf("error making a http request to %s > %v", instanceInfoUrl, err)
}
defer response.Body.Close()
switch response.StatusCode {
case 400:
errorResponseBytes, err := io.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("unable to read response from google: %w", err)
}
errorBody := ErrorResponse{}
err = json.Unmarshal(errorResponseBytes, &errorBody)
if err != nil {
return nil, fmt.Errorf("unable to read response from google: %w", err)
}
if errorBody.Error == "InvalidToken" {
return nil, ErrInvalidToken
}
return nil, ErrInvalidRequest
case 401, 403:
return nil, ErrInalidFCMServiceAccountFile
case 404:
return nil, ErrDeviceNotFound
}
if response.StatusCode > 299 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
responseBodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return &InstanceInformationResponse{}, fmt.Errorf("failed to read response body > %v", err)
}
responseBody := &InstanceInformationResponse{}
err = json.Unmarshal(responseBodyBytes, &responseBody)
if err != nil {
return nil, err
}
return responseBody, nil
}
func (c *fcmClient) Send(m FcmMessageBody) (*FcmSendHttpResponse, error) {
sendURL := fmt.Sprintf("%s/projects/%s/messages:send",
BaseURL, c.ServiceAccountConfig.ProjectId,
)
body, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("error marshaling message>%v", err)
}
resp, err := c.httpClient.Post(sendURL, DefaultContentType, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("error sending request to HTTP connection server>%v", err)
}
defer resp.Body.Close()
fcmResp := &FcmSendHttpResponse{Status: resp.StatusCode}
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body > %v", err)
}
if fcmResp.Status != http.StatusOK {
return fcmResp, fmt.Errorf("could not send a message as the server returned: %d", resp.StatusCode)
}
err = json.Unmarshal(responseBody, &fcmResp)
if err != nil {
return fcmResp, fmt.Errorf("error unmarshaling json from body: %v", err)
}
return fcmResp, nil
}
func SendPush(ctx context.Context, serviceAccountConfig string, m FcmMessageBody) (*FcmSendHttpResponse, error) {
c, err := NewFcmClient(serviceAccountConfig, ctx)
if err != nil {
return nil, err
}
return c.Send(m)
}