Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update dep. and doc #3

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License (MIT)

Copyright (c) 2016 Adam Jones
Copyright (c) 2023 Laurent Gaches

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

APNS/2 is a go package designed for simple, flexible and fast Apple Push Notifications on iOS, OSX and Safari using the new HTTP/2 Push provider API.

[![Build Status](https://github.com/lgaches/apns2/actions/workflows/tests.yml/badge.svg)](https://github.com/sideshow/apns2/actions/workflows/tests.yml) [![Coverage Status](https://coveralls.io/repos/lgaches/apns2/badge.svg?branch=master&service=github)](https://coveralls.io/github/sideshow/apns2?branch=master) [![GoDoc](https://godoc.org/github.com/lgaches/apns2?status.svg)](https://godoc.org/github.com/sideshow/apns2)
[![Build Status](https://github.com/lgaches/apns2/actions/workflows/tests.yml/badge.svg)](https://github.com/lgaches/apns2/actions/workflows/tests.yml) [![Coverage Status](https://coveralls.io/repos/lgaches/apns2/badge.svg?branch=master&service=github)](https://coveralls.io/github/lgaches/apns2?branch=main) [![GoDoc](https://godoc.org/github.com/lgaches/apns2?status.svg)](https://godoc.org/github.com/lgaches/apns2)

## Features

Expand All @@ -23,7 +23,7 @@ APNS/2 is a go package designed for simple, flexible and fast Apple Push Notific
- Install apns2:

```sh
go get -u github.com/sideshow/apns2
go get -u github.com/lgaches/apns2
```

If you are running the test suite you will also need to install testify:
Expand All @@ -41,8 +41,8 @@ import (
"log"
"fmt"

"github.com/sideshow/apns2"
"github.com/sideshow/apns2/certificate"
"github.com/lgaches/apns2"
"github.com/lgaches/apns2/certificate"
)

func main() {
Expand Down Expand Up @@ -133,7 +133,7 @@ notification.Payload = payload
client.Push(notification)
```

Refer to the [payload](https://godoc.org/github.com/sideshow/apns2/payload) docs for more info.
Refer to the [payload](https://godoc.org/github.com/lgaches/apns2/payload) docs for more info.

## Response, Error handling

Expand Down Expand Up @@ -184,30 +184,33 @@ Speed is greatly affected by the location of your server and the quality of your

## Command line tool

APNS/2 has a command line tool that can be installed with `go get github.com/sideshow/apns2/apns2`. Usage:
APNS/2 has a command line tool that can be installed with `go get github.com/lgaches/apns2/apns2`. Usage:

```
```sh
apns2 --help
usage: apns2 --certificate-path=CERTIFICATE-PATH --topic=TOPIC [<flags>]
or: apns2 --authkey-path=AUTHKEY-PATH --key-id=KEY-ID --team-id=TEAM-ID --topic=TOPIC [<flags>]

Listens to STDIN to send notifications and writes APNS response code and reason to STDOUT.

The expected format is: <DeviceToken> <APNS Payload>
Example: aff0c63d9eaa63ad161bafee732d5bc2c31f66d552054718ff19ce314371e5d0 {"aps": {"alert": "hi"}}
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
-c, --certificate-path=CERTIFICATE-PATH
Path to certificate file.
-t, --topic=TOPIC The topic of the remote notification, which is typically the bundle ID for your app
-m, --mode="production" APNS server to send notifications to. `production` or `development`. Defaults to `production`
--version Show application version.
-help Show context-sensitive help.
-certificate-path Path to the certificate file. (Certificates, Identifiers & Profiles -> Certificates -> Production)
-authkey-path Path to the P8 file. (Certificates, Identifiers & Profiles -> Keys)
-key-id Key ID from developer account (Certificates, Identifiers & Profiles -> Keys)
-team-id Team ID from developer account (View Account -> Membership details)
-topic The topic of the remote notification, which is typically the bundle ID for your app
-mode APNS server to send notifications to. `production` or `development`. Defaults to `production`
```

## License

The MIT License (MIT)

Copyright (c) 2016 Adam Jones
Copyright (c) 2023 Laurent Gaches

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
49 changes: 38 additions & 11 deletions apns2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,58 @@ import (

"github.com/lgaches/apns2"
"github.com/lgaches/apns2/certificate"
)

var (
certificatePath = flag.String("certificate-path", "", "Path to certificate file.")
topic = flag.String("topic", "", "The topic of the remote notification, which is typically the bundle ID for your app")
mode = flag.String("mode", "production", "APNS server to send notifications to. `production` or `development`. Defaults to `production`")
"github.com/lgaches/apns2/token"
)

func main() {
certificatePath := flag.String("certificate-path", "", "Path to certificate file.")
authKeyPath := flag.String("authkey-path", "", "path to the P8 file. (Certificates, Identifiers & Profiles -> Keys)")
keyID := flag.String("key-id", "", "Key ID from developer account (Certificates, Identifiers & Profiles -> Keys)")
teamID := flag.String("team-id", "", "Team ID from developer account (View Account -> Membership)")
topic := flag.String("topic", "", "The topic of the remote notification, which is typically the bundle ID for your app")
mode := flag.String("mode", "production", "APNS server to send notifications to. `production` or `development`. Defaults to `production`")

flag.Parse()

cert, pemErr := certificate.FromPemFile(*certificatePath, "")
var client *apns2.Client

if pemErr != nil {
log.Fatalf("Error retrieving certificate `%v`: %v", certificatePath, pemErr)
if certificatePath == nil || *certificatePath != "" {
cert, pemErr := certificate.FromPemFile(*certificatePath, "")
if pemErr != nil {
log.Fatalf("Error retrieving certificate `%v`: %v", certificatePath, pemErr)
}
client = apns2.NewClient(cert)
} else if *authKeyPath != "" || *teamID != "" || *keyID != "" {
authKey, authErr := token.AuthKeyFromFile(*authKeyPath)

if authErr != nil {
log.Fatalf("Error retrieving Token `%v`: %v", authKeyPath, authErr)
}

authToken := &token.Token{
AuthKey: authKey,
KeyID: *keyID,
TeamID: *teamID,
}

client = apns2.NewTokenClient(authToken)
} else {
flag.Usage()
os.Exit(1)
}

client := apns2.NewClient(cert)
if *topic == "" {
flag.Usage()
os.Exit(1)
}

if *mode == "development" {
client.Development()
} else {
client.Production()
}

fmt.Println("Ready to send push notifications. Enter tokens and payloads then press enter. Example: aff0c63d9eaa63ad161bafee732d5bc2c31f66d552054718ff19ce314371e5d0 {\"aps\": {\"alert\": \"hi\"}}")
scanner := bufio.NewScanner(os.Stdin)

for scanner.Scan() {
Expand All @@ -54,7 +81,7 @@ func main() {
if err != nil {
log.Fatal("Error: ", err)
} else {
fmt.Printf("%v: '%v'\n", res.StatusCode, res.Reason)
fmt.Printf("%v: '%v' . %v - %v - %v\n", res.StatusCode, res.Reason, res.ApnsID, res.Timestamp, res.ApnsUniqueID)
}
}
}
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (c *Client) PushWithContext(ctx Context, n *Notification) (*Response, error
r := &Response{}
r.StatusCode = response.StatusCode
r.ApnsID = response.Header.Get("apns-id")
r.ApnsUniqueID = response.Header.Get("apns-unique-id")

decoder := json.NewDecoder(response.Body)
if err := decoder.Decode(r); err != nil && err != io.EOF {
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module github.com/lgaches/apns2

go 1.21
go 1.19

require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.15.0
golang.org/x/net v0.18.0
golang.org/x/crypto v0.16.0
golang.org/x/net v0.19.0
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.15.0 h1:frVn1TEaCEaZcn3Tmd7Y2b5KKPaZ+I32Q2OA3kYp5TA=
golang.org/x/crypto v0.15.0/go.mod h1:4ChreQoLWfG3xLDer1WdlH5NdlQ3+mwnQq1YTKY+72g=
golang.org/x/net v0.18.0 h1:mIYleuAkSbHh0tCv7RvjL3F6ZVbLjq4+R7zbOn3Kokg=
golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ=
golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
2 changes: 2 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ type Response struct {
// If the value of StatusCode is 410, this is the last time at which APNs
// confirmed that the device token was no longer valid for the topic.
Timestamp Time

ApnsUniqueID string
}

// Sent returns whether or not the notification was successfully sent.
Expand Down
6 changes: 4 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ github.com/golang-jwt/jwt/v4
# github.com/pmezard/go-difflib v1.0.0
## explicit
github.com/pmezard/go-difflib/difflib
# github.com/stretchr/objx v0.5.0
## explicit; go 1.12
# github.com/stretchr/testify v1.8.4
## explicit; go 1.20
github.com/stretchr/testify/assert
# golang.org/x/crypto v0.15.0
# golang.org/x/crypto v0.16.0
## explicit; go 1.18
golang.org/x/crypto/pkcs12
golang.org/x/crypto/pkcs12/internal/rc2
# golang.org/x/net v0.18.0
# golang.org/x/net v0.19.0
## explicit; go 1.18
golang.org/x/net/http/httpguts
golang.org/x/net/http2
Expand Down
Loading