apns

一个与苹果推送通知服务对接的 Go 包。「A Go package to interface with the Apple Push Notification Service」

  • 所有者: timehop/apns
  • 平台: Windows,Linux,Mac
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

apns

GoDoc
Build Status

A Go package to interface with the Apple Push Notification Service

Features

This library implements a few features that we couldn't find in any one library elsewhere:

  • Long Lived Clients - Apple's documentation say that you should hold a persistent connection open and not create new connections for every payload
  • Use of New Protocol - Apple came out with v2 of their API with support for variable length payloads. This library uses that protocol.
  • Robust Send Guarantees - APNS has asynchronous feedback on whether a push sent. That means that if you send pushes after a bad send, those pushes will be lost forever. Our library records the last N pushes, detects errors, and is able to resend the pushes that could have been lost. More reading

API Compatibility

The apns package may undergo breaking changes. A tool like godep is recommended to vendor the current release.

Install

go get github.com/timehop/apns

Checkout the develop branch for the current work in progress.

Usage

Sending a push notification (basic)

c, _ := apns.NewClient(apns.ProductionGateway, apnsCert, apnsKey)

p := apns.NewPayload()
p.APS.Alert.Body = "I am a push notification!"
p.APS.Badge.Set(5)
p.APS.Sound = "turn_down_for_what.aiff"

m := apns.NewNotification()
m.Payload = p
m.DeviceToken = "A_DEVICE_TOKEN"
m.Priority = apns.PriorityImmediate

c.Send(m)

Sending a push notification with error handling

    c, err := apns.NewClientWithFiles(apns.ProductionGateway, "cert.pem", "key.pem")
    if err != nil {
        log.Fatal("could not create new client", err.Error())
    }

    go func() {
        for f := range c.FailedNotifs {
            fmt.Println("Notif", f.Notif.ID, "failed with", f.Err.Error())
        }
    }()

    p := apns.NewPayload()
    p.APS.Alert.Body = "I am a push notification!"
    p.APS.Badge.Set(5)
    p.APS.Sound = "turn_down_for_what.aiff"
    p.APS.ContentAvailable = 1

    p.SetCustomValue("link", "zombo://dot/com")
    p.SetCustomValue("game", map[string]int{"score": 234})

    m := apns.NewNotification()
    m.Payload = p
    m.DeviceToken = "A_DEVICE_TOKEN"
    m.Priority = apns.PriorityImmediate
    m.Identifier = 12312       // Integer for APNS
    m.ID = "user_id:timestamp" // ID not sent to Apple – to identify error notifications

    c.Send(m)

Retrieving feedback

f, err := apns.NewFeedback(s.Address(), DummyCert, DummyKey)
if err != nil {
	log.Fatal("Could not create feedback", err.Error())
}

for ft := range f.Receive() {
	fmt.Println("Feedback for token:", ft.DeviceToken)
}

Note that the channel returned from Receive will close after the
feedback service
has no more data to send.

Running the tests

We use Ginkgo for our testing framework and
Gomega for our matchers. To run the tests:

go get github.com/onsi/ginkgo/ginkgo
go get github.com/onsi/gomega
ginkgo -randomizeAllSpecs

Contributing

License

MIT License

主要指標

概覽
名稱與所有者timehop/apns
主編程語言Go
編程語言Go (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2014-06-29 20:08:22
推送於2023-04-03 14:39:03
最后一次提交2023-04-03 10:38:59
發布數0
用户参与
星數185
關注者數27
派生數46
提交數57
已啟用問題?
問題數49
打開的問題數17
拉請求數17
打開的拉請求數2
關閉的拉請求數8
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?