@@ -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+
3141const (
3242 topic = "me.fin.bark"
3343 keyID = "LH4T9V5U4R"
@@ -37,7 +47,7 @@ const (
3747
3848var clients = make (chan * apns2.Client , 1 )
3949
40- // 初始化 APNS 客户端池
50+ // Initialize APNS client pool
4151func 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
97107func 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
0 commit comments