PushWithContext sends a Notification to the APNs gateway. Context carries a deadline and a cancellation signal and allows you to close long running requests when the context timeout is exceeded. Context can be nil, for backwards compatibility. If the underlying http.Client is not currently connecte
(ctx Context, n *Notification)
| 166 | // return a Response indicating whether the notification was accepted or |
| 167 | // rejected by the APNs gateway, or an error if something goes wrong. |
| 168 | func (c *Client) PushWithContext(ctx Context, n *Notification) (*Response, error) { |
| 169 | payload, err := json.Marshal(n) |
| 170 | if err != nil { |
| 171 | return nil, err |
| 172 | } |
| 173 | |
| 174 | url := c.Host + "/3/device/" + n.DeviceToken |
| 175 | request, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(payload)) |
| 176 | if err != nil { |
| 177 | return nil, err |
| 178 | } |
| 179 | |
| 180 | if c.Token != nil { |
| 181 | c.setTokenHeader(request) |
| 182 | } |
| 183 | |
| 184 | setHeaders(request, n) |
| 185 | |
| 186 | response, err := c.HTTPClient.Do(request) |
| 187 | if err != nil { |
| 188 | return nil, err |
| 189 | } |
| 190 | defer response.Body.Close() |
| 191 | |
| 192 | r := &Response{} |
| 193 | r.StatusCode = response.StatusCode |
| 194 | r.ApnsID = response.Header.Get("apns-id") |
| 195 | r.ApnsUniqueID = response.Header.Get("apns-unique-id") |
| 196 | |
| 197 | decoder := json.NewDecoder(response.Body) |
| 198 | if err := decoder.Decode(r); err != nil && err != io.EOF { |
| 199 | return &Response{}, err |
| 200 | } |
| 201 | return r, nil |
| 202 | } |
| 203 | |
| 204 | // CloseIdleConnections closes any underlying connections which were previously |
| 205 | // connected from previous requests but are now sitting idle. It will not |