2021-07-23 17:56:33 +00:00
|
|
|
package notify
|
2017-07-17 03:22:48 +00:00
|
|
|
|
|
|
|
import (
|
2017-07-25 08:41:30 +00:00
|
|
|
"errors"
|
2017-07-17 03:22:48 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
"github.com/appleboy/gorush/config"
|
2021-07-13 08:32:39 +00:00
|
|
|
"github.com/appleboy/gorush/core"
|
|
|
|
"github.com/appleboy/gorush/logx"
|
2021-07-13 15:58:47 +00:00
|
|
|
"github.com/appleboy/gorush/status"
|
2021-07-16 04:10:34 +00:00
|
|
|
|
|
|
|
"github.com/appleboy/go-fcm"
|
2017-07-17 03:22:48 +00:00
|
|
|
)
|
|
|
|
|
2017-07-25 08:41:30 +00:00
|
|
|
// InitFCMClient use for initialize FCM Client.
|
2021-08-02 06:07:30 +00:00
|
|
|
func InitFCMClient(cfg *config.ConfYaml, key string) (*fcm.Client, error) {
|
2017-07-25 08:41:30 +00:00
|
|
|
var err error
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
if key == "" && cfg.Android.APIKey == "" {
|
2022-12-18 02:06:43 +00:00
|
|
|
return nil, errors.New("missing android api key")
|
2017-07-25 08:41:30 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
if key != "" && key != cfg.Android.APIKey {
|
2017-07-25 08:41:30 +00:00
|
|
|
return fcm.NewClient(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if FCMClient == nil {
|
2021-07-16 04:10:34 +00:00
|
|
|
FCMClient, err = fcm.NewClient(cfg.Android.APIKey)
|
2017-07-25 08:41:30 +00:00
|
|
|
return FCMClient, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return FCMClient, nil
|
|
|
|
}
|
|
|
|
|
2017-07-17 03:22:48 +00:00
|
|
|
// GetAndroidNotification use for define Android notification.
|
|
|
|
// HTTP Connection Server Reference for Android
|
|
|
|
// https://firebase.google.com/docs/cloud-messaging/http-server-ref
|
2021-08-02 06:07:30 +00:00
|
|
|
func GetAndroidNotification(req *PushNotification) *fcm.Message {
|
2017-07-17 03:22:48 +00:00
|
|
|
notification := &fcm.Message{
|
|
|
|
To: req.To,
|
2017-10-24 09:00:08 +00:00
|
|
|
Condition: req.Condition,
|
2017-07-17 03:22:48 +00:00
|
|
|
CollapseKey: req.CollapseKey,
|
|
|
|
ContentAvailable: req.ContentAvailable,
|
2018-08-15 03:47:15 +00:00
|
|
|
MutableContent: req.MutableContent,
|
2017-07-17 03:22:48 +00:00
|
|
|
DelayWhileIdle: req.DelayWhileIdle,
|
|
|
|
TimeToLive: req.TimeToLive,
|
|
|
|
RestrictedPackageName: req.RestrictedPackageName,
|
|
|
|
DryRun: req.DryRun,
|
|
|
|
}
|
|
|
|
|
2017-10-25 02:37:53 +00:00
|
|
|
if len(req.Tokens) > 0 {
|
|
|
|
notification.RegistrationIDs = req.Tokens
|
|
|
|
}
|
2017-07-17 03:22:48 +00:00
|
|
|
|
2022-12-20 14:03:29 +00:00
|
|
|
if req.Priority == HIGH || req.Priority == "normal" {
|
2020-07-31 09:26:58 +00:00
|
|
|
notification.Priority = req.Priority
|
2017-07-17 03:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add another field
|
|
|
|
if len(req.Data) > 0 {
|
|
|
|
notification.Data = make(map[string]interface{})
|
|
|
|
for k, v := range req.Data {
|
|
|
|
notification.Data[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 05:40:21 +00:00
|
|
|
n := &fcm.Notification{}
|
|
|
|
isNotificationSet := false
|
2020-01-20 15:14:07 +00:00
|
|
|
if req.Notification != nil {
|
2020-02-27 05:40:21 +00:00
|
|
|
isNotificationSet = true
|
|
|
|
n = req.Notification
|
|
|
|
}
|
2017-07-17 03:22:48 +00:00
|
|
|
|
2020-02-27 05:40:21 +00:00
|
|
|
if len(req.Message) > 0 {
|
|
|
|
isNotificationSet = true
|
|
|
|
n.Body = req.Message
|
|
|
|
}
|
2017-07-17 03:22:48 +00:00
|
|
|
|
2020-02-27 05:40:21 +00:00
|
|
|
if len(req.Title) > 0 {
|
|
|
|
isNotificationSet = true
|
|
|
|
n.Title = req.Title
|
|
|
|
}
|
2020-02-07 03:39:44 +00:00
|
|
|
|
2020-02-27 05:40:21 +00:00
|
|
|
if len(req.Image) > 0 {
|
|
|
|
isNotificationSet = true
|
|
|
|
n.Image = req.Image
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := req.Sound.(string); ok && len(v) > 0 {
|
|
|
|
isNotificationSet = true
|
|
|
|
n.Sound = v
|
|
|
|
}
|
|
|
|
|
|
|
|
if isNotificationSet {
|
|
|
|
notification.Notification = n
|
2017-07-17 03:22:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 03:39:44 +00:00
|
|
|
// handle iOS apns in fcm
|
|
|
|
|
|
|
|
if len(req.Apns) > 0 {
|
|
|
|
notification.Apns = req.Apns
|
|
|
|
}
|
|
|
|
|
2017-07-17 03:22:48 +00:00
|
|
|
return notification
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushToAndroid provide send notification to Android server.
|
2021-08-02 06:07:30 +00:00
|
|
|
func PushToAndroid(req *PushNotification, cfg *config.ConfYaml) (resp *ResponsePush, err error) {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug("Start push notification for Android")
|
2017-07-17 03:22:48 +00:00
|
|
|
|
|
|
|
var (
|
2017-07-25 08:41:30 +00:00
|
|
|
client *fcm.Client
|
2017-07-17 03:22:48 +00:00
|
|
|
retryCount = 0
|
2021-08-01 11:48:57 +00:00
|
|
|
maxRetry = cfg.Android.MaxRetry
|
2017-07-17 03:22:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if req.Retry > 0 && req.Retry < maxRetry {
|
|
|
|
maxRetry = req.Retry
|
|
|
|
}
|
|
|
|
|
|
|
|
// check message
|
2021-08-01 09:12:16 +00:00
|
|
|
err = CheckMessage(req)
|
2017-07-17 03:22:48 +00:00
|
|
|
if err != nil {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Error("request error: " + err.Error())
|
2020-07-14 15:57:23 +00:00
|
|
|
return
|
2017-07-17 03:22:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 09:12:16 +00:00
|
|
|
resp = &ResponsePush{}
|
|
|
|
|
2017-07-17 03:22:48 +00:00
|
|
|
Retry:
|
|
|
|
notification := GetAndroidNotification(req)
|
|
|
|
|
2017-07-25 08:41:30 +00:00
|
|
|
if req.APIKey != "" {
|
2021-08-01 11:48:57 +00:00
|
|
|
client, err = InitFCMClient(cfg, req.APIKey)
|
2017-07-25 08:41:30 +00:00
|
|
|
} else {
|
2021-08-01 11:48:57 +00:00
|
|
|
client, err = InitFCMClient(cfg, cfg.Android.APIKey)
|
2017-07-17 03:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// FCM server error
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Error("FCM server error: " + err.Error())
|
2020-07-14 15:57:23 +00:00
|
|
|
return
|
2017-07-17 03:22:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := client.Send(notification)
|
|
|
|
if err != nil {
|
2017-07-25 08:41:30 +00:00
|
|
|
// Send Message error
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogError.Error("FCM server send message error: " + err.Error())
|
2020-11-08 00:56:11 +00:00
|
|
|
|
|
|
|
if req.IsTopic() {
|
2021-08-01 11:48:57 +00:00
|
|
|
errLog := logPush(cfg, core.FailedPush, req.To, req, err)
|
2021-08-02 04:37:38 +00:00
|
|
|
resp.Logs = append(resp.Logs, errLog)
|
2021-07-13 15:58:47 +00:00
|
|
|
status.StatStorage.AddAndroidError(1)
|
2020-11-08 00:56:11 +00:00
|
|
|
} else {
|
|
|
|
for _, token := range req.Tokens {
|
2021-08-01 11:48:57 +00:00
|
|
|
errLog := logPush(cfg, core.FailedPush, token, req, err)
|
2021-08-02 04:37:38 +00:00
|
|
|
resp.Logs = append(resp.Logs, errLog)
|
2020-11-08 00:56:11 +00:00
|
|
|
}
|
2021-07-13 15:58:47 +00:00
|
|
|
status.StatStorage.AddAndroidError(int64(len(req.Tokens)))
|
2020-11-08 00:56:11 +00:00
|
|
|
}
|
2020-07-14 15:57:23 +00:00
|
|
|
return
|
2017-07-17 03:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 09:00:08 +00:00
|
|
|
if !req.IsTopic() {
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug(fmt.Sprintf("Android Success count: %d, Failure count: %d", res.Success, res.Failure))
|
2017-10-24 09:00:08 +00:00
|
|
|
}
|
|
|
|
|
2021-07-13 15:58:47 +00:00
|
|
|
status.StatStorage.AddAndroidSuccess(int64(res.Success))
|
|
|
|
status.StatStorage.AddAndroidError(int64(res.Failure))
|
2017-07-17 03:22:48 +00:00
|
|
|
|
|
|
|
var newTokens []string
|
2017-10-24 09:00:08 +00:00
|
|
|
// result from Send messages to specific devices
|
2017-07-17 03:22:48 +00:00
|
|
|
for k, result := range res.Results {
|
2017-10-25 02:37:53 +00:00
|
|
|
to := ""
|
|
|
|
if k < len(req.Tokens) {
|
|
|
|
to = req.Tokens[k]
|
|
|
|
} else {
|
|
|
|
to = req.To
|
|
|
|
}
|
|
|
|
|
2017-07-17 03:22:48 +00:00
|
|
|
if result.Error != nil {
|
2020-05-06 11:37:06 +00:00
|
|
|
// We should retry only "retryable" statuses. More info about response:
|
|
|
|
// https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-plain-text
|
|
|
|
if !result.Unregistered() {
|
|
|
|
newTokens = append(newTokens, to)
|
|
|
|
}
|
|
|
|
|
2021-08-01 11:48:57 +00:00
|
|
|
errLog := logPush(cfg, core.FailedPush, to, req, result.Error)
|
2021-08-02 04:37:38 +00:00
|
|
|
resp.Logs = append(resp.Logs, errLog)
|
2017-07-17 03:22:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-01 11:48:57 +00:00
|
|
|
logPush(cfg, core.SucceededPush, to, req, nil)
|
2017-07-17 03:22:48 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 09:00:08 +00:00
|
|
|
// result from Send messages to topics
|
|
|
|
if req.IsTopic() {
|
|
|
|
to := ""
|
|
|
|
if req.To != "" {
|
|
|
|
to = req.To
|
|
|
|
} else {
|
|
|
|
to = req.Condition
|
|
|
|
}
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug("Send Topic Message: ", to)
|
2017-10-24 09:00:08 +00:00
|
|
|
// Success
|
|
|
|
if res.MessageID != 0 {
|
2021-08-01 11:48:57 +00:00
|
|
|
logPush(cfg, core.SucceededPush, to, req, nil)
|
2017-10-24 09:00:08 +00:00
|
|
|
} else {
|
|
|
|
// failure
|
2021-08-01 11:48:57 +00:00
|
|
|
errLog := logPush(cfg, core.FailedPush, to, req, res.Error)
|
2021-08-02 04:37:38 +00:00
|
|
|
resp.Logs = append(resp.Logs, errLog)
|
2017-10-24 09:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 02:37:53 +00:00
|
|
|
// Device Group HTTP Response
|
|
|
|
if len(res.FailedRegistrationIDs) > 0 {
|
2019-10-17 15:49:21 +00:00
|
|
|
newTokens = append(newTokens, res.FailedRegistrationIDs...)
|
2017-10-25 02:37:53 +00:00
|
|
|
|
2022-12-18 02:06:43 +00:00
|
|
|
//nolint
|
2021-08-01 11:48:57 +00:00
|
|
|
errLog := logPush(cfg, core.FailedPush, notification.To, req, errors.New("device group: partial success or all fails"))
|
2021-08-02 04:37:38 +00:00
|
|
|
resp.Logs = append(resp.Logs, errLog)
|
2017-10-25 02:37:53 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 15:57:23 +00:00
|
|
|
if len(newTokens) > 0 && retryCount < maxRetry {
|
2017-07-17 03:22:48 +00:00
|
|
|
retryCount++
|
|
|
|
|
|
|
|
// resend fail token
|
|
|
|
req.Tokens = newTokens
|
|
|
|
goto Retry
|
|
|
|
}
|
2021-07-13 08:32:39 +00:00
|
|
|
|
2021-08-04 01:00:04 +00:00
|
|
|
return resp, nil
|
2021-07-13 08:32:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-02 06:07:30 +00:00
|
|
|
func logPush(cfg *config.ConfYaml, status, token string, req *PushNotification, err error) logx.LogPushEntry {
|
2021-08-01 09:12:16 +00:00
|
|
|
return logx.LogPush(&logx.InputLog{
|
2021-07-13 08:32:39 +00:00
|
|
|
ID: req.ID,
|
|
|
|
Status: status,
|
|
|
|
Token: token,
|
|
|
|
Message: req.Message,
|
|
|
|
Platform: req.Platform,
|
|
|
|
Error: err,
|
2021-07-16 04:10:34 +00:00
|
|
|
HideToken: cfg.Log.HideToken,
|
|
|
|
Format: cfg.Log.Format,
|
2021-07-13 08:32:39 +00:00
|
|
|
})
|
|
|
|
}
|