diff --git a/push/buford/buford.go b/push/buford/buford.go index ad6fdd2..7a6955a 100644 --- a/push/buford/buford.go +++ b/push/buford/buford.go @@ -19,7 +19,7 @@ type NewClient func(*tls.Certificate) (*http.Client, error) // bufordFactory instantiates new buford Services to satisfy the PushProviderFactory interface. type bufordFactory struct { workers uint - expiration time.Time + expiration time.Duration newClientCallback NewClient } @@ -34,7 +34,7 @@ func WithWorkers(workers uint) Option { } // WithExpiration sets the APNs expiration time for the push notifications. -func WithExpiration(expiration time.Time) Option { +func WithExpiration(expiration time.Duration) Option { return func(f *bufordFactory) { f.expiration = expiration } @@ -72,26 +72,33 @@ func (f *bufordFactory) NewPushProvider(cert *tls.Certificate) (push.PushProvide return nil, err } prov := &bufordPushProvider{ - service: bufordpush.NewService(client, bufordpush.Production), - workers: f.workers, - } - if !f.expiration.IsZero() { - prov.headers = &bufordpush.Headers{Expiration: f.expiration} + service: bufordpush.NewService(client, bufordpush.Production), + expiration: f.expiration, + workers: f.workers, } return prov, err } // bufordPushProvider wraps a buford Service to satisfy the PushProvider interface. type bufordPushProvider struct { - service *bufordpush.Service - headers *bufordpush.Headers - workers uint + service *bufordpush.Service + expiration time.Duration + workers uint +} + +func assemblePushData(magic string, exp time.Duration) (payload []byte, hdr *bufordpush.Headers) { + payload = []byte(`{"mdm":"` + magic + `"}`) + if exp > 0 { + expiration := time.Now().Add(exp) + hdr = &bufordpush.Headers{Expiration: expiration} + } + return } func (c *bufordPushProvider) pushSingle(pushInfo *mdm.Push) *push.Response { resp := new(push.Response) - payload := []byte(`{"mdm":"` + pushInfo.PushMagic + `"}`) - resp.Id, resp.Err = c.service.Push(pushInfo.Token.String(), c.headers, payload) + payload, headers := assemblePushData(pushInfo.PushMagic, c.expiration) + resp.Id, resp.Err = c.service.Push(pushInfo.Token.String(), headers, payload) return resp } @@ -103,8 +110,8 @@ func (c *bufordPushProvider) pushMulti(pushInfos []*mdm.Push) map[string]*push.R queue := bufordpush.NewQueue(c.service, workers) defer queue.Close() for _, push := range pushInfos { - payload := []byte(`{"mdm":"` + push.PushMagic + `"}`) - go queue.Push(push.Token.String(), c.headers, payload) + payload, headers := assemblePushData(push.PushMagic, c.expiration) + go queue.Push(push.Token.String(), headers, payload) } responses := make(map[string]*push.Response) for range pushInfos {