forked from googlearchive/go-gcm
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcm.go
143 lines (129 loc) · 6.11 KB
/
gcm.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package gcm provides send and receive GCM functionality.
package gcm
import (
"net/http"
"time"
)
// HTTPMessage defines a downstream GCM HTTP message.
type HTTPMessage struct {
To string `json:"to,omitempty"`
RegistrationIDs []string `json:"registration_ids,omitempty"`
CollapseKey string `json:"collapse_key,omitempty"`
Priority string `json:"priority,omitempty"`
ContentAvailable bool `json:"content_available,omitempty"`
MutableContent bool `json:"mutable_content,omitempty"`
TimeToLive *uint `json:"time_to_live,omitempty"`
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
Data Data `json:"data,omitempty"`
Notification *Notification `json:"notification,omitempty"`
}
// HTTPResponse is the GCM connection server response to an HTTP downstream message.
type HTTPResponse struct {
StatusCode int
MulticastID int64 `json:"multicast_id"`
Success uint `json:"success"`
Failure uint `json:"failure"`
CanonicalIds uint `json:"canonical_ids"`
Results []HTTPResult `json:"results,omitempty"`
// Topic message HTTP response only.
MessageID uint `json:"message_id,omitempty"`
Error string `json:"error,omitempty"`
RetryAfter time.Duration `json:"retry_after,omitempty"`
}
// HTTPResult represents the result of a single processed HTTP request.
type HTTPResult struct {
MessageID string `json:"message_id,omitempty"`
RegistrationID string `json:"registration_id,omitempty"`
Error string `json:"error,omitempty"`
}
// XMPPMessage defines a downstream GCM XMPP message.
type XMPPMessage struct {
To string `json:"to,omitempty"`
MessageID string `json:"message_id"`
MessageType string `json:"message_type,omitempty"`
CollapseKey string `json:"collapse_key,omitempty"`
Priority string `json:"priority,omitempty"`
ContentAvailable bool `json:"content_available,omitempty"`
MutableContent bool `json:"mutable_content,omitempty"`
TimeToLive *uint `json:"time_to_live,omitempty"`
DeliveryReceiptRequested bool `json:"delivery_receipt_requested,omitempty"`
DryRun bool `json:"dry_run,omitempty"`
Data Data `json:"data,omitempty"`
Notification *Notification `json:"notification,omitempty"`
}
// Data defines the custom payload of a GCM message.
type Data map[string]interface{}
// Notification defines the notification payload of a GCM message.
// NOTE: contains keys for both Android and iOS notifications.
type Notification struct {
// Common fields.
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Sound string `json:"sound,omitempty"`
ClickAction string `json:"click_action,omitempty"`
BodyLocKey string `json:"body_loc_key,omitempty"`
BodyLocArgs string `json:"body_loc_args,omitempty"`
TitleLocKey string `json:"title_loc_key,omitempty"`
TitleLocArgs string `json:"title_loc_args,omitempty"`
// Android-only fields.
Icon string `json:"icon,omitempty"`
Tag string `json:"tag,omitempty"`
Color string `json:"color,omitempty"`
// iOS-only fields
Badge string `json:"badge,omitempty"`
}
// Config is a container for GCM client configuration data.
type Config struct {
SenderID string `json:"sender_id"`
APIKey string `json:"api_key"`
// Deprecated: for backwards compatibility UseFCM is still present, but it no longer has any effect. The GCM
// endpoints are on the path to deletion, and Google has confirmed that the FCM endpoints are backwards compatible.
// All HTTP and XMPP push will be sent using the FCM endpoints.
UseFCM bool `json:"use_fcm"`
Sandbox bool `json:"sandbox"`
MonitorConnection bool `json:"monitor_connection"`
Debug bool `json:"debug"`
PingInterval int `json:"ping_interval"`
PingTimeout int `json:"ping_timeout"`
XMPPConnectTimeout time.Duration `json:"xmpp_connect_timeout`
OmitInternalRetry bool `json:"omit_internal_retry"`
HTTPTimeout time.Duration `json:"http_timeout"`
HTTPTransport http.RoundTripper `json:"-"`
}
// CCSMessage is an XMPP message sent from CCS.
// The CCS message can be an upstream message (device to server) or a
// message from CCS (e.g. a delivery receipt, a control, etc).
type CCSMessage struct {
From string `json:"from, omitempty"`
MessageID string `json:"message_id, omitempty"`
MessageType string `json:"message_type, omitempty"`
RegistrationID string `json:"registration_id,omitempty"`
Error string `json:"error,omitempty"`
ErrorDescription string `json:"error_description,omitempty"`
Category string `json:"category, omitempty"`
Data Data `json:"data,omitempty"`
ControlType string `json:"control_type,omitempty"`
}
// MessageHandler is the type for a function that handles a CCS message.
type MessageHandler func(cm CCSMessage) error
// Client defines an interface for the GCM client.
type Client interface {
ID() string
SendHTTP(m HTTPMessage) (*HTTPResponse, error)
SendXMPP(m XMPPMessage) (string, int, error)
Close() error
}