2021-07-23 17:56:33 +00:00
|
|
|
package notify
|
2016-03-24 16:46:37 +00:00
|
|
|
|
2016-03-24 16:53:45 +00:00
|
|
|
import (
|
2016-04-02 23:27:10 +00:00
|
|
|
"errors"
|
2016-07-29 00:48:24 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-04-06 02:52:57 +00:00
|
|
|
"os"
|
2017-10-24 09:00:08 +00:00
|
|
|
"strings"
|
2017-04-06 07:00:49 +00:00
|
|
|
"sync"
|
2017-01-19 09:08:12 +00:00
|
|
|
|
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-17 04:54:13 +00:00
|
|
|
|
2021-07-23 17:56:33 +00:00
|
|
|
"github.com/appleboy/go-fcm"
|
2021-07-24 09:18:35 +00:00
|
|
|
"github.com/appleboy/queue"
|
2021-07-17 04:54:13 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2020-09-04 03:01:21 +00:00
|
|
|
"github.com/msalihkarakasli/go-hms-push/push/model"
|
2016-03-24 16:46:37 +00:00
|
|
|
)
|
|
|
|
|
2021-07-17 04:54:13 +00:00
|
|
|
var json = jsoniter.ConfigCompatibleWithStandardLibrary
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// D provide string array
|
2016-04-13 03:15:24 +00:00
|
|
|
type D map[string]interface{}
|
2016-03-24 16:46:37 +00:00
|
|
|
|
2016-04-01 07:45:24 +00:00
|
|
|
const (
|
2016-04-13 06:59:28 +00:00
|
|
|
// ApnsPriorityLow will tell APNs to send the push message at a time that takes
|
2016-04-01 07:45:24 +00:00
|
|
|
// into account power considerations for the device. Notifications with this
|
|
|
|
// priority might be grouped and delivered in bursts. They are throttled, and
|
|
|
|
// in some cases are not delivered.
|
|
|
|
ApnsPriorityLow = 5
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// ApnsPriorityHigh will tell APNs to send the push message immediately.
|
2016-04-01 07:45:24 +00:00
|
|
|
// Notifications with this priority must trigger an alert, sound, or badge on
|
|
|
|
// the target device. It is an error to use this priority for a push
|
|
|
|
// notification that contains only the content-available key.
|
|
|
|
ApnsPriorityHigh = 10
|
|
|
|
)
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// Alert is APNs payload
|
2016-04-01 07:45:24 +00:00
|
|
|
type Alert struct {
|
2018-11-20 03:02:26 +00:00
|
|
|
Action string `json:"action,omitempty"`
|
|
|
|
ActionLocKey string `json:"action-loc-key,omitempty"`
|
|
|
|
Body string `json:"body,omitempty"`
|
|
|
|
LaunchImage string `json:"launch-image,omitempty"`
|
|
|
|
LocArgs []string `json:"loc-args,omitempty"`
|
|
|
|
LocKey string `json:"loc-key,omitempty"`
|
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
Subtitle string `json:"subtitle,omitempty"`
|
|
|
|
TitleLocArgs []string `json:"title-loc-args,omitempty"`
|
|
|
|
TitleLocKey string `json:"title-loc-key,omitempty"`
|
|
|
|
SummaryArg string `json:"summary-arg,omitempty"`
|
|
|
|
SummaryArgCount int `json:"summary-arg-count,omitempty"`
|
2016-03-25 02:39:59 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// RequestPush support multiple notification request.
|
2016-04-10 03:36:49 +00:00
|
|
|
type RequestPush struct {
|
2016-04-10 04:02:44 +00:00
|
|
|
Notifications []PushNotification `json:"notifications" binding:"required"`
|
2016-04-10 03:36:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// PushNotification is single notification request
|
2016-04-10 03:36:49 +00:00
|
|
|
type PushNotification struct {
|
2021-07-16 04:10:34 +00:00
|
|
|
Wg *sync.WaitGroup
|
|
|
|
Log *[]logx.LogPushEntry
|
2021-07-16 08:30:01 +00:00
|
|
|
Cfg config.ConfYaml
|
2019-10-17 15:19:25 +00:00
|
|
|
|
2016-03-24 16:46:37 +00:00
|
|
|
// Common
|
2019-09-06 07:48:42 +00:00
|
|
|
ID string `json:"notif_id,omitempty"`
|
2018-08-28 03:02:13 +00:00
|
|
|
Tokens []string `json:"tokens" binding:"required"`
|
|
|
|
Platform int `json:"platform" binding:"required"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
Title string `json:"title,omitempty"`
|
2020-02-07 03:39:44 +00:00
|
|
|
Image string `json:"image,omitempty"`
|
2018-08-28 03:02:13 +00:00
|
|
|
Priority string `json:"priority,omitempty"`
|
|
|
|
ContentAvailable bool `json:"content_available,omitempty"`
|
|
|
|
MutableContent bool `json:"mutable_content,omitempty"`
|
|
|
|
Sound interface{} `json:"sound,omitempty"`
|
|
|
|
Data D `json:"data,omitempty"`
|
|
|
|
Retry int `json:"retry,omitempty"`
|
2016-03-25 01:34:22 +00:00
|
|
|
|
2016-03-24 16:46:37 +00:00
|
|
|
// Android
|
2020-01-20 15:14:07 +00:00
|
|
|
APIKey string `json:"api_key,omitempty"`
|
|
|
|
To string `json:"to,omitempty"`
|
|
|
|
CollapseKey string `json:"collapse_key,omitempty"`
|
|
|
|
DelayWhileIdle bool `json:"delay_while_idle,omitempty"`
|
|
|
|
TimeToLive *uint `json:"time_to_live,omitempty"`
|
|
|
|
RestrictedPackageName string `json:"restricted_package_name,omitempty"`
|
|
|
|
DryRun bool `json:"dry_run,omitempty"`
|
|
|
|
Condition string `json:"condition,omitempty"`
|
|
|
|
Notification *fcm.Notification `json:"notification,omitempty"`
|
2016-03-25 01:34:22 +00:00
|
|
|
|
2020-09-04 03:01:21 +00:00
|
|
|
// Huawei
|
2021-03-28 14:13:02 +00:00
|
|
|
AppID string `json:"app_id,omitempty"`
|
|
|
|
AppSecret string `json:"app_secret,omitempty"`
|
2020-09-04 03:01:21 +00:00
|
|
|
HuaweiNotification *model.AndroidNotification `json:"huawei_notification,omitempty"`
|
|
|
|
HuaweiData string `json:"huawei_data,omitempty"`
|
|
|
|
HuaweiCollapseKey int `json:"huawei_collapse_key,omitempty"`
|
|
|
|
HuaweiTTL string `json:"huawei_ttl,omitempty"`
|
|
|
|
BiTag string `json:"bi_tag,omitempty"`
|
|
|
|
FastAppTarget int `json:"fast_app_target,omitempty"`
|
|
|
|
|
2016-03-24 16:46:37 +00:00
|
|
|
// iOS
|
2019-11-02 06:27:22 +00:00
|
|
|
Expiration *int64 `json:"expiration,omitempty"`
|
2018-08-15 03:47:15 +00:00
|
|
|
ApnsID string `json:"apns_id,omitempty"`
|
|
|
|
CollapseID string `json:"collapse_id,omitempty"`
|
|
|
|
Topic string `json:"topic,omitempty"`
|
2019-09-23 15:36:38 +00:00
|
|
|
PushType string `json:"push_type,omitempty"`
|
2018-08-15 03:47:15 +00:00
|
|
|
Badge *int `json:"badge,omitempty"`
|
|
|
|
Category string `json:"category,omitempty"`
|
|
|
|
ThreadID string `json:"thread-id,omitempty"`
|
|
|
|
URLArgs []string `json:"url-args,omitempty"`
|
|
|
|
Alert Alert `json:"alert,omitempty"`
|
|
|
|
Production bool `json:"production,omitempty"`
|
|
|
|
Development bool `json:"development,omitempty"`
|
2018-08-28 03:02:13 +00:00
|
|
|
SoundName string `json:"name,omitempty"`
|
|
|
|
SoundVolume float32 `json:"volume,omitempty"`
|
2020-02-07 03:39:44 +00:00
|
|
|
Apns D `json:"apns,omitempty"`
|
2016-03-24 16:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-04-10 03:46:48 +00:00
|
|
|
// WaitDone decrements the WaitGroup counter.
|
|
|
|
func (p *PushNotification) WaitDone() {
|
2021-07-16 04:10:34 +00:00
|
|
|
if p.Wg != nil {
|
|
|
|
p.Wg.Done()
|
2017-04-06 07:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 03:46:48 +00:00
|
|
|
// AddWaitCount increments the WaitGroup counter.
|
|
|
|
func (p *PushNotification) AddWaitCount() {
|
2021-07-16 04:10:34 +00:00
|
|
|
if p.Wg != nil {
|
|
|
|
p.Wg.Add(1)
|
2017-04-10 03:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddLog record fail log of notification
|
2021-07-13 08:32:39 +00:00
|
|
|
func (p *PushNotification) AddLog(log logx.LogPushEntry) {
|
2021-07-16 04:10:34 +00:00
|
|
|
if p.Log != nil {
|
|
|
|
*p.Log = append(*p.Log, log)
|
2017-04-10 03:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 04:54:13 +00:00
|
|
|
// Bytes for queue message
|
|
|
|
func (p *PushNotification) Bytes() []byte {
|
|
|
|
b, err := json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2017-10-24 09:00:08 +00:00
|
|
|
// IsTopic check if message format is topic for FCM
|
|
|
|
// ref: https://firebase.google.com/docs/cloud-messaging/send-message#topic-http-post-request
|
|
|
|
func (p *PushNotification) IsTopic() bool {
|
2021-07-13 08:32:39 +00:00
|
|
|
if p.Platform == core.PlatFormAndroid {
|
2020-09-04 03:01:21 +00:00
|
|
|
return p.To != "" && strings.HasPrefix(p.To, "/topics/") || p.Condition != ""
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if p.Platform == core.PlatFormHuawei {
|
2020-09-04 03:01:21 +00:00
|
|
|
return p.Topic != "" || p.Condition != ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2017-10-24 09:00:08 +00:00
|
|
|
}
|
|
|
|
|
2016-04-24 07:57:38 +00:00
|
|
|
// CheckMessage for check request message
|
2016-04-24 07:54:41 +00:00
|
|
|
func CheckMessage(req PushNotification) error {
|
2016-04-24 06:30:17 +00:00
|
|
|
var msg string
|
|
|
|
|
2017-10-24 09:00:08 +00:00
|
|
|
// ignore send topic mesaage from FCM
|
2021-01-23 01:48:58 +00:00
|
|
|
if !req.IsTopic() && len(req.Tokens) == 0 && req.To == "" {
|
2016-04-24 06:30:17 +00:00
|
|
|
msg = "the message must specify at least one registration ID"
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug(msg)
|
2016-04-24 06:30:17 +00:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if len(req.Tokens) == core.PlatFormIos && req.Tokens[0] == "" {
|
2016-04-24 07:54:41 +00:00
|
|
|
msg = "the token must not be empty"
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug(msg)
|
2016-04-24 07:54:41 +00:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if req.Platform == core.PlatFormAndroid && len(req.Tokens) > 1000 {
|
2016-04-24 06:30:17 +00:00
|
|
|
msg = "the message may specify at most 1000 registration IDs"
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug(msg)
|
2016-04-24 06:30:17 +00:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
|
2021-07-13 08:32:39 +00:00
|
|
|
if req.Platform == core.PlatFormHuawei && len(req.Tokens) > 500 {
|
2020-09-04 03:01:21 +00:00
|
|
|
msg = "the message may specify at most 500 registration IDs for Huawei"
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug(msg)
|
2020-09-04 03:01:21 +00:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
|
2017-06-01 07:52:01 +00:00
|
|
|
// ref: https://firebase.google.com/docs/cloud-messaging/http-server-ref
|
2021-07-13 08:32:39 +00:00
|
|
|
if req.Platform == core.PlatFormAndroid && req.TimeToLive != nil && *req.TimeToLive > uint(2419200) {
|
2016-04-24 06:30:17 +00:00
|
|
|
msg = "the message's TimeToLive field must be an integer " +
|
|
|
|
"between 0 and 2419200 (4 weeks)"
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug(msg)
|
2016-04-24 06:30:17 +00:00
|
|
|
return errors.New(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-01 03:56:10 +00:00
|
|
|
// SetProxy only working for FCM server.
|
2016-07-29 00:48:24 +00:00
|
|
|
func SetProxy(proxy string) error {
|
2016-07-31 12:58:13 +00:00
|
|
|
proxyURL, err := url.ParseRequestURI(proxy)
|
2016-07-29 00:48:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-31 12:58:13 +00:00
|
|
|
http.DefaultTransport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
|
2021-07-13 08:32:39 +00:00
|
|
|
logx.LogAccess.Debug("Set http proxy as " + proxy)
|
2016-07-29 00:48:24 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-13 06:59:28 +00:00
|
|
|
// CheckPushConf provide check your yml config.
|
2021-07-16 04:10:34 +00:00
|
|
|
func CheckPushConf(cfg config.ConfYaml) error {
|
|
|
|
if !cfg.Ios.Enabled && !cfg.Android.Enabled && !cfg.Huawei.Enabled {
|
2020-09-04 03:01:21 +00:00
|
|
|
return errors.New("Please enable iOS, Android or Huawei config in yml config")
|
2016-04-02 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
if cfg.Ios.Enabled {
|
|
|
|
if cfg.Ios.KeyPath == "" && cfg.Ios.KeyBase64 == "" {
|
2018-02-18 09:12:51 +00:00
|
|
|
return errors.New("Missing iOS certificate key")
|
2016-04-02 15:51:42 +00:00
|
|
|
}
|
2017-04-06 02:52:57 +00:00
|
|
|
|
|
|
|
// check certificate file exist
|
2021-07-16 04:10:34 +00:00
|
|
|
if cfg.Ios.KeyPath != "" {
|
|
|
|
if _, err := os.Stat(cfg.Ios.KeyPath); os.IsNotExist(err) {
|
2018-02-18 09:12:51 +00:00
|
|
|
return errors.New("certificate file does not exist")
|
|
|
|
}
|
2017-04-06 02:52:57 +00:00
|
|
|
}
|
2016-04-02 15:51:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
if cfg.Android.Enabled {
|
|
|
|
if cfg.Android.APIKey == "" {
|
2016-04-02 15:51:42 +00:00
|
|
|
return errors.New("Missing Android API Key")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
if cfg.Huawei.Enabled {
|
|
|
|
if cfg.Huawei.AppSecret == "" {
|
2021-03-28 14:13:02 +00:00
|
|
|
return errors.New("Missing Huawei App Secret")
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
if cfg.Huawei.AppID == "" {
|
2021-03-28 14:13:02 +00:00
|
|
|
return errors.New("Missing Huawei App ID")
|
2020-09-04 03:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-02 15:51:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-07-16 04:10:34 +00:00
|
|
|
|
|
|
|
// SendNotification send notification
|
2021-07-17 17:58:46 +00:00
|
|
|
func SendNotification(req queue.QueuedMessage) {
|
2021-07-25 00:35:49 +00:00
|
|
|
v, ok := req.(*PushNotification)
|
|
|
|
if !ok {
|
|
|
|
if err := json.Unmarshal(req.Bytes(), v); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 17:58:46 +00:00
|
|
|
|
2021-07-16 04:10:34 +00:00
|
|
|
defer func() {
|
2021-07-17 17:58:46 +00:00
|
|
|
v.WaitDone()
|
2021-07-16 04:10:34 +00:00
|
|
|
}()
|
|
|
|
|
2021-07-17 17:58:46 +00:00
|
|
|
switch v.Platform {
|
2021-07-16 04:10:34 +00:00
|
|
|
case core.PlatFormIos:
|
2021-07-17 17:58:46 +00:00
|
|
|
PushToIOS(*v)
|
2021-07-16 04:10:34 +00:00
|
|
|
case core.PlatFormAndroid:
|
2021-07-17 17:58:46 +00:00
|
|
|
PushToAndroid(*v)
|
2021-07-16 04:10:34 +00:00
|
|
|
case core.PlatFormHuawei:
|
2021-07-17 17:58:46 +00:00
|
|
|
PushToHuawei(*v)
|
2021-07-16 04:10:34 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-25 00:35:49 +00:00
|
|
|
|
|
|
|
// Run send notification
|
|
|
|
var Run = func(msg queue.QueuedMessage) error {
|
|
|
|
SendNotification(msg)
|
|
|
|
return nil
|
|
|
|
}
|