-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapn.go
117 lines (80 loc) · 3.52 KB
/
apn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2011 Nicolas Paton. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
apn is a Go Apple Push Notification package. Heavily uses queues and goroutines.
It has a few tests and **seems** to work properly but it hasn't had thourough testing and hasn't gone into production yet.
Feedback service not supported yet. [3]
NB: This is my first Go code ever.
Installation
goinstall github.com/nicolaspaton/goapn
Usage
import "github.com/nicolaspaton/goapn"
// You can create multiple queues for different environments and apps
q, err := apn.NewQueue(apn.Sandbox, "cert.pem", "key.pem")
if err != nil {
log.Fatalln("Error loading queue", err)
}
// The payload is a nested string to interface{} map that respects Apple doc [1]
payload := make(map[string]interface{})
payload["aps"] = map[string]interface{}{
"alert": "You've got emails.",
"badge": 9,
"sound": "bingbong.aiff",
}
payload["foo"] = "bar"
payload["answer"] = 42
// Et hop, send! (0 for expiry means 1 year)
q.Send <- apn.NewNotification("3f6e...device token here", payload, 0)
// Start a loop in a goroutine somewhere to handle errors (before you start sending)
// The erronerous notification is returned with an non null Error (os.Error) attribute
// This interface handles internal validation errors and errors sent back by apple [2]
go func() {
for {
notification := <- q.Error
if notification.Error != nil {
// Do something with that notification
}
}
}()
Certificates
You need a certification and an unprotected key pem file. See http://blog.boxedice.com/2010/06/05/how-to-renew-your-apple-push-notification-push-ssl-certificate/
Reminder, after you've got your .p12 files:
openssl pkcs12 -clcerts -nokeys -out dev-cert.pem -in dev-cert.p12
openssl pkcs12 -nocerts -out dev-key.pem -in dev-key.p12
openssl rsa -in dev-key.pem -out dev-key-noenc.pem
License
BSD-style. See LICENSE file.
Apple and Apple Push Notifications are trademarks owned by Apple inc. and have nothing to do with the creator of this software.
[1] http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html%23//apple_ref/doc/uid/TP40008194-CH100-SW9
[2] http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html%23//apple_ref/doc/uid/TP40008194-CH101-SW4
[3] http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/CommunicatingWIthAPS/CommunicatingWIthAPS.html%23//apple_ref/doc/uid/TP40008194-CH101-SW4
*/
package apn
type ApnEnv int
const (
Test = iota // The Test env is not an Apple env, it doesn't connect nor send
Sandbox
Production
)
func envString(env ApnEnv) (envString string) {
if env == Test {
envString = "test"
} else if env == Sandbox {
envString = "sandbox"
} else if env == Production {
envString = "production"
}
return
}
func envObject(envString string) (env ApnEnv) {
if envString == "test" {
env = Test
} else if envString == "sandbox" {
env = Sandbox
} else if envString == "production" {
env = Production
}
return
}
var queues []Queue = make([]Queue, 10)