-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsend.go
78 lines (67 loc) · 1.71 KB
/
send.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
package univush
import (
"context"
"errors"
"firebase.google.com/go"
"firebase.google.com/go/messaging"
"github.com/sideshow/apns2"
"google.golang.org/api/option"
)
// ClientType - the type of the client "fcm" or "apns2"
type ClientType int
const (
// ClientTypeFCM - FCM client
ClientTypeFCM ClientType = 1
// ClientTypeAPNS2 - APNS2 client
ClientTypeAPNS2 ClientType = 2
)
// Client - a client connection manager
type Client struct {
fcm *messaging.Client
apns2 *apns2.Client
clientType ClientType
}
// NewFCMClient - constructs a new FCM client
func NewFCMClient(ctx context.Context, appID, appToken string) (*Client, error) {
app, err := firebase.NewApp(ctx, nil, option.WithAPIKey(appToken))
if err != nil {
return nil, err
}
client, err := app.Messaging(ctx)
if err != nil {
return nil, err
}
return &Client{
fcm: client,
clientType: ClientTypeFCM,
}, nil
}
// NewAPNS2CLient - constructs a new APNS2 client
func NewAPNS2CLient(ctx context.Context, cert []byte, passphrase string) (*Client, error) {
crt, err := CertBytes(cert, passphrase)
if err != nil {
return nil, err
}
return &Client{
apns2: apns2.NewClient(crt),
clientType: ClientTypeAPNS2,
}, nil
}
// Send - push the message
func (c *Client) Send(ctx context.Context, n *Notification) (*ClientResponse, error) {
switch c.clientType {
case ClientTypeAPNS2:
resp, err := c.apns2.PushWithContext(ctx, n.ToAPNS())
if err != nil {
return nil, err
}
return NewClientResponseFromAPNS2(resp), nil
case ClientTypeFCM:
id, err := c.fcm.Send(ctx, n.ToAndroid())
if err != nil {
return nil, err
}
return NewClientResponseFromFCM(id), nil
}
return nil, errors.New("invalid client specified")
}