Skip to content

Apple Push Notification service (APNs) mock server

License

Notifications You must be signed in to change notification settings

bergusman/apnsmock-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

APNs Mock Server

Server example:

key, err := mock.AuthKeyFromFile("AuthKey_XXXXXXXXXX.p8")
if err != nil {
	log.Fatal(err)
}

handler := &mock.Handler{}
handler.TokenPublicKey = func(keyID, teamID string) *ecdsa.PublicKey {
	if keyID == "XXXXXXXXXX" && teamID == "YYYYYYYYYY" {
		return &key.PublicKey
	}
	return nil
}
handler.DeviceToken = func(token string) *mock.DeviceToken {
	if token == "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" {
		return &mock.DeviceToken{
			Token:        token,
			Topic:        "com.example.app",
			Unregistered: 1625475525573,
		}
	}
	return &mock.DeviceToken{
		Token: token,
		Topic: "com.example.app",
	}
}
handler.Push = func(push *mock.Push) {
	fmt.Println(push.Status, push.Reason)
}

http.ListenAndServe(":80", handler)

Client example with apns-go:

key, err := apns.AuthKeyFromFile("AuthKey_XXXXXXXXXX.p8")
if err != nil {
	log.Fatal(err)
}

token := apns.NewToken(key, "XXXXXXXXXX", "YYYYYYYYYY")
client := apns.NewClient(token, nil)

n := &apns.Notification{
	DeviceToken: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
	Host:        "http://localhost",
	Topic:       "com.example.app",
	Payload: apns.BuildPayload(&apns.APS{
		Alert: "Hi",
	}, nil),
}

fmt.Println(client.Push(n))