-
-
Notifications
You must be signed in to change notification settings - Fork 850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
let apns support multi-cert #288
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,34 +11,50 @@ import ( | |
) | ||
|
||
// InitAPNSClient use for initialize APNs Client. | ||
func InitAPNSClient() error { | ||
if PushConf.Ios.Enabled { | ||
var err error | ||
ext := filepath.Ext(PushConf.Ios.KeyPath) | ||
|
||
switch ext { | ||
case ".p12": | ||
CertificatePemIos, err = certificate.FromP12File(PushConf.Ios.KeyPath, PushConf.Ios.Password) | ||
case ".pem": | ||
CertificatePemIos, err = certificate.FromPemFile(PushConf.Ios.KeyPath, PushConf.Ios.Password) | ||
default: | ||
err = errors.New("wrong certificate key extension") | ||
func InitAPNSClient(key string) (*apns.Client, error) { | ||
path, password := "", "" | ||
if len(key) > 0 { | ||
if _, ok := PushConf.Ios.KeyMap[key]; !ok { | ||
LogError.Errorf("Key %s key_map not exist", key) | ||
return nil, errors.New("APNS key_map not exists") | ||
} | ||
if _, ok := PushConf.Ios.KeyMap[key]; !ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. KeyMap -> KeyPass |
||
LogError.Errorf("Key %s key_password not exist", key) | ||
return nil, errors.New("APNS key_password not exists") | ||
} | ||
path = PushConf.Ios.KeyMap[key] | ||
password = PushConf.Ios.KeyMap[key] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. KeyPass |
||
} else { | ||
path = PushConf.Ios.KeyPath | ||
password = PushConf.Ios.Password | ||
} | ||
|
||
if err != nil { | ||
LogError.Error("Cert Error:", err.Error()) | ||
if c, ok := ApnsClients[path]; ok { | ||
return c, nil | ||
} | ||
var err error | ||
ext := filepath.Ext(path) | ||
switch ext { | ||
case ".p12": | ||
CertificatePemIos, err = certificate.FromP12File(path, password) | ||
case ".pem": | ||
CertificatePemIos, err = certificate.FromPemFile(path, password) | ||
default: | ||
err = errors.New("wrong certificate key extension") | ||
} | ||
|
||
return err | ||
} | ||
if err != nil { | ||
LogError.Error("Cert Error:", err.Error()) | ||
return nil, err | ||
} | ||
|
||
if PushConf.Ios.Production { | ||
ApnsClient = apns.NewClient(CertificatePemIos).Production() | ||
} else { | ||
ApnsClient = apns.NewClient(CertificatePemIos).Development() | ||
} | ||
if PushConf.Ios.Production { | ||
ApnsClients[path] = apns.NewClient(CertificatePemIos).Production() | ||
} else { | ||
ApnsClients[path] = apns.NewClient(CertificatePemIos).Development() | ||
} | ||
|
||
return nil | ||
return ApnsClients[path], nil | ||
} | ||
|
||
func iosAlertDictionary(payload *payload.Payload, req PushNotification) *payload.Payload { | ||
|
@@ -162,6 +178,8 @@ func PushToIOS(req PushNotification) bool { | |
} | ||
|
||
var ( | ||
err error | ||
client *apns.Client | ||
retryCount = 0 | ||
maxRetry = PushConf.Ios.MaxRetry | ||
) | ||
|
@@ -181,8 +199,19 @@ Retry: | |
for _, token := range req.Tokens { | ||
notification.DeviceToken = token | ||
|
||
if req.ApnsClient != "" { | ||
client, err = InitAPNSClient(req.ApnsClient) | ||
} else { | ||
client, err = InitAPNSClient("") | ||
} | ||
|
||
if err != nil { | ||
// APNS server error | ||
LogError.Error("APNS server error: " + err.Error()) | ||
return false | ||
} | ||
// send ios notification | ||
res, err := ApnsClient.Push(notification) | ||
res, err := client.Push(notification) | ||
|
||
if err != nil { | ||
// apns server error | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package gorush | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
|
@@ -41,6 +42,11 @@ func queueNotification(req RequestPush) (int, []LogPushEntry) { | |
if !PushConf.Ios.Enabled { | ||
continue | ||
} | ||
if len(notification.ApnsClient) > 0 { | ||
if _, ok := PushConf.Ios.KeyMap[notification.ApnsClient]; !ok { | ||
continue | ||
} | ||
} | ||
case PlatFormAndroid: | ||
if !PushConf.Android.Enabled { | ||
continue | ||
|
@@ -56,6 +62,7 @@ func queueNotification(req RequestPush) (int, []LogPushEntry) { | |
notification.log = &log | ||
notification.AddWaitCount() | ||
} | ||
fmt.Println("-----") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this line |
||
QueueNotification <- notification | ||
count += len(notification.Tokens) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if PushConf.Ios.Enabled is false?