{"id":13396790,"url":"https://github.com/sideshow/apns2","last_synced_at":"2025-05-13T17:10:29.713Z","repository":{"id":37335573,"uuid":"49032710","full_name":"sideshow/apns2","owner":"sideshow","description":"⚡ HTTP/2 Apple Push Notification Service (APNs) push provider for Go — Send push notifications to iOS, tvOS, Safari and OSX apps, using the APNs HTTP/2 protocol.","archived":false,"fork":false,"pushed_at":"2024-10-25T08:03:32.000Z","size":1373,"stargazers_count":3077,"open_issues_count":34,"forks_count":343,"subscribers_count":74,"default_branch":"master","last_synced_at":"2025-04-22T15:04:12.563Z","etag":null,"topics":["apns","apns-http","apns2","apple","apple-push-notifications","golang","http2","jwt","jwt-token","notifications","safari-push-notifications"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sideshow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-01-05T00:56:53.000Z","updated_at":"2025-04-21T10:18:04.000Z","dependencies_parsed_at":"2024-12-09T17:05:56.845Z","dependency_job_id":"33d6a4d7-af8e-45cd-a4f0-8be0e181cd57","html_url":"https://github.com/sideshow/apns2","commit_stats":{"total_commits":156,"total_committers":29,"mean_commits":5.379310344827586,"dds":0.3205128205128205,"last_synced_commit":"83ca0a4a861ad44ad97eb8852d0f060720901b80"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sideshow%2Fapns2","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sideshow%2Fapns2/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sideshow%2Fapns2/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sideshow%2Fapns2/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sideshow","download_url":"https://codeload.github.com/sideshow/apns2/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990468,"owners_count":21995774,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["apns","apns-http","apns2","apple","apple-push-notifications","golang","http2","jwt","jwt-token","notifications","safari-push-notifications"],"created_at":"2024-07-30T18:01:03.228Z","updated_at":"2025-05-13T17:10:24.700Z","avatar_url":"https://github.com/sideshow.png","language":"Go","readme":"# APNS/2\n\nAPNS/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.\n\n[![Build Status](https://github.com/sideshow/apns2/actions/workflows/tests.yml/badge.svg)](https://github.com/sideshow/apns2/actions/workflows/tests.yml) [![Coverage Status](https://coveralls.io/repos/sideshow/apns2/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/sideshow/apns2?branch=master) [![GoDoc](https://godoc.org/github.com/sideshow/apns2?status.svg)](https://godoc.org/github.com/sideshow/apns2)\n\n## Features\n\n- Uses new Apple APNs HTTP/2 connection\n- Fast - See [notes on speed](https://github.com/sideshow/apns2/wiki/APNS-HTTP-2-Push-Speed)\n- Works with go 1.7 and later\n- Supports new Apple Token Based Authentication (JWT)\n- Supports new iOS 10 features such as Collapse IDs, Subtitles and Mutable Notifications\n- Supports new iOS 15 features interruptionLevel and relevanceScore\n- Supports persistent connections to APNs\n- Supports VoIP/PushKit notifications (iOS 8 and later)\n- Modular \u0026 easy to use\n- Tested and working in APNs production environment\n\n## Install\n\n- Make sure you have [Go](https://golang.org/doc/install) installed and have set your [GOPATH](https://golang.org/doc/code.html#GOPATH).\n- Install apns2:\n\n```sh\ngo get -u github.com/sideshow/apns2\n```\n\nIf you are running the test suite you will also need to install testify:\n\n```sh\ngo get -u github.com/stretchr/testify\n```\n\n## Example\n\n```go\npackage main\n\nimport (\n  \"log\"\n  \"fmt\"\n\n  \"github.com/sideshow/apns2\"\n  \"github.com/sideshow/apns2/certificate\"\n)\n\nfunc main() {\n\n  cert, err := certificate.FromP12File(\"../cert.p12\", \"\")\n  if err != nil {\n    log.Fatal(\"Cert Error:\", err)\n  }\n\n  notification := \u0026apns2.Notification{}\n  notification.DeviceToken = \"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7\"\n  notification.Topic = \"com.sideshow.Apns2\"\n  notification.Payload = []byte(`{\"aps\":{\"alert\":\"Hello!\"}}`) // See Payload section below\n\n  // If you want to test push notifications for builds running directly from XCode (Development), use\n  // client := apns2.NewClient(cert).Development()\n  // For apps published to the app store or installed as an ad-hoc distribution use Production()\n\n  client := apns2.NewClient(cert).Production()\n  res, err := client.Push(notification)\n\n  if err != nil {\n    log.Fatal(\"Error:\", err)\n  }\n\n  fmt.Printf(\"%v %v %v\\n\", res.StatusCode, res.ApnsID, res.Reason)\n}\n```\n\n## JWT Token Example\n\nInstead of using a `.p12` or `.pem` certificate as above, you can optionally use\nAPNs JWT _Provider Authentication Tokens_. First you will need a signing key (`.p8` file), Key ID and Team ID [from Apple](http://help.apple.com/xcode/mac/current/#/dev54d690a66). Once you have these details, you can create a new client:\n\n```go\nauthKey, err := token.AuthKeyFromFile(\"../AuthKey_XXX.p8\")\nif err != nil {\n  log.Fatal(\"token error:\", err)\n}\n\ntoken := \u0026token.Token{\n  AuthKey: authKey,\n  // KeyID from developer account (Certificates, Identifiers \u0026 Profiles -\u003e Keys)\n  KeyID:   \"ABC123DEFG\",\n  // TeamID from developer account (View Account -\u003e Membership)\n  TeamID:  \"DEF123GHIJ\",\n}\n...\n\nclient := apns2.NewTokenClient(token)\nres, err := client.Push(notification)\n```\n\n- You can use one APNs signing key to authenticate tokens for multiple apps.\n- A signing key works for both the development and production environments.\n- A signing key doesn’t expire but can be revoked.\n\n## Notification\n\nAt a minimum, a _Notification_ needs a _DeviceToken_, a _Topic_ and a _Payload_.\n\n```go\nnotification := \u0026apns2.Notification{\n  DeviceToken: \"11aa01229f15f0f0c52029d8cf8cd0aeaf2365fe4cebc4af26cd6d76b7919ef7\",\n  Topic: \"com.sideshow.Apns2\",\n  Payload: []byte(`{\"aps\":{\"alert\":\"Hello!\"}}`),\n}\n```\n\nYou can also set an optional _ApnsID_, _Expiration_ or _Priority_.\n\n```go\nnotification.ApnsID =  \"40636A2C-C093-493E-936A-2A4333C06DEA\"\nnotification.Expiration = time.Now()\nnotification.Priority = apns2.PriorityLow\n```\n\n## Payload\n\nYou can use raw bytes for the `notification.Payload` as above, or you can use the payload builder package which makes it easy to construct APNs payloads.\n\n```go\n// {\"aps\":{\"alert\":\"hello\",\"badge\":1},\"key\":\"val\"}\n\npayload := payload.NewPayload().Alert(\"hello\").Badge(1).Custom(\"key\", \"val\")\n\nnotification.Payload = payload\nclient.Push(notification)\n```\n\nRefer to the [payload](https://godoc.org/github.com/sideshow/apns2/payload) docs for more info.\n\n## Response, Error handling\n\nAPNS/2 draws the distinction between a valid response from Apple indicating whether or not the _Notification_ was sent or not, and an unrecoverable or unexpected _Error_;\n\n- An `Error` is returned if a non-recoverable error occurs, i.e. if there is a problem with the underlying _http.Client_ connection or _Certificate_, the payload was not sent, or a valid _Response_ was not received.\n- A `Response` is returned if the payload was successfully sent to Apple and a documented response was received. This struct will contain more information about whether or not the push notification succeeded, its _apns-id_ and if applicable, more information around why it did not succeed.\n\nTo check if a `Notification` was successfully sent;\n\n```go\nres, err := client.Push(notification)\nif err != nil {\n  log.Println(\"There was an error\", err)\n  return\n}\n\nif res.Sent() {\n  log.Println(\"Sent:\", res.ApnsID)\n} else {\n  fmt.Printf(\"Not Sent: %v %v %v\\n\", res.StatusCode, res.ApnsID, res.Reason)\n}\n```\n\n## Context \u0026 Timeouts\n\nFor better control over request cancellations and timeouts APNS/2 supports\ncontexts. Using a context can be helpful if you want to cancel all pushes when\nthe parent process is cancelled, or need finer grained control over individual\npush timeouts. See the [Google post](https://blog.golang.org/context) for more\ninformation on contexts.\n\n```go\nctx, cancel = context.WithTimeout(context.Background(), 10 * time.Second)\nres, err := client.PushWithContext(ctx, notification)\ndefer cancel()\n```\n\n## Speed \u0026 Performance\n\nAlso see the wiki page on [APNS HTTP 2 Push Speed](https://github.com/sideshow/apns2/wiki/APNS-HTTP-2-Push-Speed).\n\nFor best performance, you should hold on to an `apns2.Client` instance and not re-create it every push. The underlying TLS connection itself can take a few seconds to connect and negotiate, so if you are setting up an `apns2.Client` and tearing it down every push, then this will greatly affect performance. (Apple suggest keeping the connection open all the time).\n\nYou should also limit the amount of `apns2.Client` instances. The underlying transport has a http connection pool itself, so a single client instance will be enough for most users (One instance can potentially do 4,000+ pushes per second). If you need more than this then one instance per CPU core is a good starting point.\n\nSpeed is greatly affected by the location of your server and the quality of your network connection. If you're just testing locally, behind a proxy or if your server is outside USA then you're not going to get great performance. With a good server located in AWS, you should be able to get [decent throughput](https://github.com/sideshow/apns2/wiki/APNS-HTTP-2-Push-Speed).\n\n## Command line tool\n\nAPNS/2 has a command line tool that can be installed with `go get github.com/sideshow/apns2/apns2`. Usage:\n\n```\napns2 --help\nusage: apns2 --certificate-path=CERTIFICATE-PATH --topic=TOPIC [\u003cflags\u003e]\n\nListens to STDIN to send notifications and writes APNS response code and reason to STDOUT.\n\nThe expected format is: \u003cDeviceToken\u003e \u003cAPNS Payload\u003e\nExample: aff0c63d9eaa63ad161bafee732d5bc2c31f66d552054718ff19ce314371e5d0 {\"aps\": {\"alert\": \"hi\"}}\nFlags:\n      --help               Show context-sensitive help (also try --help-long and --help-man).\n  -c, --certificate-path=CERTIFICATE-PATH\n                           Path to certificate file.\n  -t, --topic=TOPIC        The topic of the remote notification, which is typically the bundle ID for your app\n  -m, --mode=\"production\"  APNS server to send notifications to. `production` or `development`. Defaults to `production`\n      --version            Show application version.\n```\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2016 Adam Jones\n\nPermission 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:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","funding_links":[],"categories":["Library","Go","Messaging","消息","Relational Databases","消息系统","消息传递","机器学习"],"sub_categories":["Advanced Console UIs","检索及分析资料库","Search and Analytic Databases","SQL 查询语句构建库","交流"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsideshow%2Fapns2","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsideshow%2Fapns2","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsideshow%2Fapns2/lists"}