Skip to content

Commit 3e598e8

Browse files
committed
Add support for sending silent push notifications.
1 parent 5c6f6a6 commit 3e598e8

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

apns/apns.go

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ type PushMessage struct {
2828
ExtParams map[string]interface{} `form:"ext_params,omitempty" json:"ext_params,omitempty" xml:"ext_params,omitempty" query:"ext_params,omitempty"`
2929
}
3030

31+
// Check if it's an empty message, empty messages might be silent push notifications
32+
func (p PushMessage) IsEmptyAlert() bool {
33+
return p.Title == "" && p.Body == "" && p.Subtitle == ""
34+
}
35+
36+
// Check if it's an encrypted push notification
37+
func (p PushMessage) IsEncrypted() bool {
38+
return p.ExtParams["ciphertext"] != nil
39+
}
40+
3141
const (
3242
topic = "me.fin.bark"
3343
keyID = "LH4T9V5U4R"
@@ -37,7 +47,7 @@ const (
3747

3848
var clients = make(chan *apns2.Client, 1)
3949

40-
// 初始化 APNS 客户端池
50+
// Initialize APNS client pool
4151
func init() {
4252
ReCreateAPNS(1)
4353
}
@@ -84,7 +94,7 @@ func ReCreateAPNS(maxClientCount int) error {
8494
},
8595
Timeout: apns2.HTTPClientTimeout,
8696
},
87-
Host: apns2.HostProduction,
97+
Host: apns2.HostDevelopment,
8898
}
8999
logger.Infof("create apns client: %d", i)
90100
clients <- client
@@ -95,16 +105,24 @@ func ReCreateAPNS(maxClientCount int) error {
95105
}
96106

97107
func Push(msg *PushMessage) error {
98-
pl := payload.NewPayload().
99-
AlertTitle(msg.Title).
100-
AlertSubtitle(msg.Subtitle).
101-
AlertBody(msg.Body).
102-
Sound(msg.Sound).
103-
Category("myNotificationCategory")
104-
105-
group, exist := msg.ExtParams["group"]
106-
if exist {
107-
pl = pl.ThreadID(group.(string))
108+
pl := payload.NewPayload().MutableContent()
109+
pushType := apns2.PushTypeAlert
110+
111+
if msg.IsEmptyAlert() {
112+
// Silent push notification
113+
pl = pl.ContentAvailable()
114+
pushType = apns2.PushTypeBackground
115+
} else {
116+
// Regular push notification
117+
pl = pl.AlertTitle(msg.Title).
118+
AlertSubtitle(msg.Subtitle).
119+
AlertBody(msg.Body).
120+
Sound(msg.Sound).
121+
Category("myNotificationCategory")
122+
group, exist := msg.ExtParams["group"]
123+
if exist {
124+
pl = pl.ThreadID(group.(string))
125+
}
108126
}
109127

110128
for k, v := range msg.ExtParams {
@@ -119,8 +137,9 @@ func Push(msg *PushMessage) error {
119137
CollapseID: msg.Id,
120138
DeviceToken: msg.DeviceToken,
121139
Topic: topic,
122-
Payload: pl.MutableContent(),
140+
Payload: pl,
123141
Expiration: time.Now().Add(24 * time.Hour),
142+
PushType: pushType,
124143
})
125144
if err != nil {
126145
return err

route_push.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ func push(params map[string]interface{}) (int, error) {
219219
switch strings.ToLower(string(key)) {
220220
case "id":
221221
msg.Id = val
222+
msg.ExtParams["id"] = val
222223
case "device_key":
223224
msg.DeviceKey = val
224225
case "subtitle":
@@ -250,8 +251,9 @@ func push(params map[string]interface{}) (int, error) {
250251
return 400, fmt.Errorf("device key is empty")
251252
}
252253

253-
if msg.Body == "" && msg.Title == "" && msg.Subtitle == "" {
254-
msg.Body = "Empty message"
254+
if msg.IsEmptyAlert() && msg.IsEncrypted() {
255+
// For encrypted push notifications, a Body is required; otherwise, APNs will discard the notification
256+
msg.Body = "Encrypted Message"
255257
}
256258

257259
deviceToken, err := db.DeviceTokenByKey(msg.DeviceKey)

0 commit comments

Comments
 (0)