<commit_before># go-apns2
	This project is under development

[![Build Status](https://travis-ci.org/sger/go-apns2.svg?branch=master)](https://travis-ci.org/sger/go-apns2)

Simple package for HTTP/2 [Apple Push Notification Service](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html).

## Installation

 Via `go-get`:

```sh
$ go get github.com/sger/go-apns2
```

## Usage

```go
package main

import (
	"crypto/tls"
	"fmt"
	"log"

	"github.com/sger/go-apns2"
	"github.com/sger/go-apns2/certificate"
)

func main() {
	var deviceToken = "<KEY>"
	var filename = "../certs/PushChatKey.p12"
	var password = "<PASSWORD>"

	// Setup payload must contains an aps root label and alert message
	payload := []byte(`{ "aps" : { "alert" : "Hello world" } }`)

	cert, key, err := p12.ReadFile(filename, password)
	if err != nil {
		log.Fatal(err)
	}

	certificate := tls.Certificate{
		Certificate: [][]byte{cert.Raw},
		PrivateKey:  key,
		Leaf:        cert,
	}

	// Setup a new http client
	client, err := apns2.NewClient(certificate, apns2.Development)

	if err != nil {
		log.Fatal(err)
	}

	// Send the Push Notification
	resp, err := client.SendPush(payload, deviceToken, &apns2.Headers{})

	if err != nil {
		log.Fatal(err)
	}

	// returns apns-id if request is success
	// otherwise returns error reason
	fmt.Println(resp)
}
```

## TODO
- [ ] Error Handling
- [ ] Payload Struct

Author
-----

__<NAME>__ 

- [![](https://img.shields.io/badge/twitter-sger-brightgreen.svg)](https://twitter.com/sger) <commit_msg>updating README.md<commit_after># go-apns2
	This project is under development

[![Build Status](https://travis-ci.org/sger/go-apns2.svg?branch=master)](https://travis-ci.org/sger/go-apns2)

Simple package for HTTP/2 [Apple Push Notification Service](https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html).

## Installation

 Via `go-get`:

```sh
$ go get github.com/sger/go-apns2
```

## Usage

# Simple example

```go
package main

import (
	"crypto/tls"
	"fmt"
	"log"

	"github.com/sger/go-apns2"
	"github.com/sger/go-apns2/certificate"
)

func main() {
	var deviceToken = "<KEY>"
	var filename = "../certs/PushChatKey.p12"
	var password = "<PASSWORD>"

	// Setup payload must contains an aps root label and alert message also remove the spaces from []byte decreases the payload size.
	payload := []byte(`{"aps":{"alert":"Hello world"}}`)

	cert, key, err := p12.ReadFile(filename, password)
	if err != nil {
		log.Fatal(err)
	}

	certificate := tls.Certificate{
		Certificate: [][]byte{cert.Raw},
		PrivateKey:  key,
		Leaf:        cert,
	}

	// Setup a new http client
	client, err := apns2.NewClient(certificate, apns2.Development)

	if err != nil {
		log.Fatal(err)
	}

	// Send the Push Notification
	resp, err := client.SendPush(payload, deviceToken, &apns2.Headers{})

	if err != nil {
		log.Fatal(err)
	}

	// returns apns-id if request is success
	// otherwise returns error reason
	fmt.Println(resp)
}
```

# Channels example

```go
package main

import (
	"crypto/tls"
	"fmt"
	"log"
	"time"

	"github.com/sger/go-apns2"
	"github.com/sger/go-apns2/certificate"
)

func main() {

	payloads := [][]byte{}

	for i := 0; i < 200; i++ {
		message := fmt.Sprintf("Hello World %v!", i)
		payload := []byte(`{ "aps" : { "alert" : "` + message + `" } }`)
		payloads = append(payloads, payload)
	}

	results := asyncHTTPPosts(payloads)
	for _, result := range results {
		if result != nil {
			fmt.Println(result)
		}
	}
}

func asyncHTTPPosts(payloads [][]byte) []*apns2.ApnsResponse {

	var deviceToken = "<KEY>"
	var filename = "../certs/PushChatKey.p12"
	var password = "<PASSWORD>"

	ch := make(chan *apns2.ApnsResponse)
	responses := []*apns2.ApnsResponse{}

	cert, key, err := certificate.ReadP12File(filename, password)
	if err != nil {
		log.Fatal(err)
	}

	certificate := tls.Certificate{
		Certificate: [][]byte{cert.Raw},
		PrivateKey:  key,
		Leaf:        cert,
	}

	// Setup a new http client
	client, err := apns2.NewClient(certificate, apns2.Development)

	if err != nil {
		log.Fatal(err)
	}

	for _, payload := range payloads {
		go func(payload []byte) {
			fmt.Printf("Sending %s \n", payload)
			resp, err := client.SendPush(payload, deviceToken, &apns2.Headers{})
			if err != nil {
				log.Fatal(err)
			}
			ch <- resp
		}(payload)
	}

	for {
		select {
		case resp := <-ch:
			fmt.Printf("%v was received \n", resp)
			responses = append(responses, resp)
			if len(responses) == len(payloads) {
				return responses
			}
		case <-time.After(50 * time.Millisecond):
			fmt.Printf(".")
		}
	}
	return responses
}
```

## TODO
- [ ] Error Handling
- [ ] Payload Struct

Author
-----

__<NAME>__ 

- [![](https://img.shields.io/badge/twitter-sger-brightgreen.svg)](https://twitter.com/sger) 
{'fraction_non_alphanumeric': 0.1668029435813573, 'fraction_numerical': 0.009403107113654947, 'mean_word_length': 3.6116870876531575, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2290553', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1782, 'n_words': 398}