func main() { var deviceToken, filename, password, environment string flag.StringVar(&deviceToken, "d", "", "Device token") flag.StringVar(&filename, "c", "", "Path to p12 certificate file") flag.StringVar(&password, "p", "", "Password for p12 file.") flag.StringVar(&environment, "e", "development", "Environment") flag.Parse() cert, key, err := certificate.Load(filename, password) if err != nil { log.Fatal(err) } service := push.Service{ Client: push.NewClient(certificate.TLS(cert, key)), Host: push.Development, } if environment == "production" { service.Host = push.Production } p := payload.APS{ Alert: payload.Alert{Body: "Hello HTTP/2"}, Badge: badge.New(42), } id, err := service.Push(deviceToken, &push.Headers{}, p) if err != nil { log.Fatal(err) } log.Println("apns-id:", id) }
func send(token string, headers *push.Headers, payload interface{}, service *push.Service) { if token != "" { println("Sending push: ", token) b, err := json.Marshal(payload) if err != nil { println("Cannot parse payload") os.Exit(1) } id, err := service.Push(token, headers, b) if err != nil { log.Fatal(err, id) } else { println("Push sent successfully ", id) } } }
func TestTimestampError(t *testing.T) { deviceToken := "c2732227a1d8021cfaf781d71fb2f908c61f5861079a00954a5453f1d0281433" payload := []byte(`{ "aps" : { "alert" : "Hello HTTP/2" } }`) handler := http.NewServeMux() server := httptest.NewServer(handler) handler.HandleFunc("/3/device/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusGone) w.Write([]byte(`{"reason":"Unregistered","timestamp":12622780800}`)) }) service := push.Service{ Client: http.DefaultClient, Host: server.URL, } _, err := service.PushBytes(deviceToken, nil, payload) if err == push.ErrUnregistered { t.Error("Expected error structure, got constant.") } e, ok := err.(*push.Error) if !ok { t.Fatalf("Expected push error, got %v.", err) } if e.Err != push.ErrUnregistered { t.Errorf("Expected error %v, got %v.", push.ErrUnregistered, err) } expected := time.Unix(12622780800, 0) if e.Timestamp != expected { t.Errorf("Expected timestamp %v, got %v.", expected, e.Timestamp) } if e.DeviceToken != deviceToken { t.Errorf("Expected device token %v, got %v.", deviceToken, e.DeviceToken) } }
func TestBadPriorityPush(t *testing.T) { deviceToken := "c2732227a1d8021cfaf781d71fb2f908c61f5861079a00954a5453f1d0281433" payload := []byte(`{ "aps" : { "alert" : "Hello HTTP/2" } }`) handler := http.NewServeMux() server := httptest.NewServer(handler) handler.HandleFunc("/3/device/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusBadRequest) w.Write([]byte(`{"reason": "BadPriority"}`)) }) service := push.Service{ Client: http.DefaultClient, Host: server.URL, } _, err := service.PushBytes(deviceToken, nil, payload) if err != push.ErrBadPriority { t.Errorf("Expected error %v, got %v.", push.ErrBadPriority, err) } }
func TestPush(t *testing.T) { deviceToken := "c2732227a1d8021cfaf781d71fb2f908c61f5861079a00954a5453f1d0281433" payload := []byte(`{ "aps" : { "alert" : "Hello HTTP/2" } }`) apnsID := "922D9F1F-B82E-B337-EDC9-DB4FC8527676" handler := http.NewServeMux() server := httptest.NewServer(handler) handler.HandleFunc("/3/device/", func(w http.ResponseWriter, r *http.Request) { expectURL := fmt.Sprintf("/3/device/%s", deviceToken) if r.URL.String() != expectURL { t.Errorf("Expected url %v, got %v", expectURL, r.URL) } body, err := ioutil.ReadAll(r.Body) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(body, payload) { t.Errorf("Expected body %v, got %v", payload, body) } w.Header().Set("apns-id", apnsID) }) service := push.Service{ Client: http.DefaultClient, Host: server.URL, } id, err := service.PushBytes(deviceToken, &push.Headers{}, payload) if err != nil { t.Error(err) } if id != apnsID { t.Errorf("Expected apns-id %q, but got %q.", apnsID, id) } }