2023-03-03 17:42:26 +08:00
|
|
|
package jpush
|
2021-11-10 15:24:59 +08:00
|
|
|
|
|
|
|
|
import (
|
2023-02-22 19:51:14 +08:00
|
|
|
"context"
|
2023-02-20 10:13:29 +08:00
|
|
|
"encoding/base64"
|
|
|
|
|
"fmt"
|
2023-03-16 10:46:06 +08:00
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/internal/push/offlinepush/jpush/body"
|
|
|
|
|
"github.com/OpenIMSDK/Open-IM-Server/pkg/common/config"
|
|
|
|
|
http2 "github.com/OpenIMSDK/Open-IM-Server/pkg/common/http"
|
2021-11-10 15:24:59 +08:00
|
|
|
)
|
|
|
|
|
|
2022-04-08 15:40:07 +08:00
|
|
|
type JPush struct{}
|
|
|
|
|
|
2023-02-22 19:51:14 +08:00
|
|
|
func NewClient() *JPush {
|
2022-04-08 15:40:07 +08:00
|
|
|
return &JPush{}
|
2021-11-10 15:24:59 +08:00
|
|
|
}
|
|
|
|
|
|
2022-04-08 15:40:07 +08:00
|
|
|
func (j *JPush) Auth(apiKey, secretKey string, timeStamp int64) (token string, err error) {
|
|
|
|
|
return token, nil
|
2022-04-05 10:27:34 +08:00
|
|
|
}
|
|
|
|
|
|
2022-04-08 15:40:07 +08:00
|
|
|
func (j *JPush) SetAlias(cid, alias string) (resp string, err error) {
|
|
|
|
|
return resp, nil
|
|
|
|
|
}
|
2021-11-10 15:24:59 +08:00
|
|
|
|
2023-02-22 19:51:14 +08:00
|
|
|
func (j *JPush) getAuthorization(appKey string, masterSecret string) string {
|
|
|
|
|
str := fmt.Sprintf("%s:%s", appKey, masterSecret)
|
2023-02-20 10:13:29 +08:00
|
|
|
buf := []byte(str)
|
|
|
|
|
Authorization := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(buf))
|
|
|
|
|
return Authorization
|
|
|
|
|
}
|
2022-06-02 20:06:14 +08:00
|
|
|
|
2023-03-03 19:59:10 +08:00
|
|
|
func (j *JPush) Push(ctx context.Context, userIDs []string, title, content string, opts *offlinepush.Opts) error {
|
2023-02-20 10:13:29 +08:00
|
|
|
var pf body.Platform
|
2022-04-13 16:20:30 +08:00
|
|
|
pf.SetAll()
|
2023-02-20 10:13:29 +08:00
|
|
|
var au body.Audience
|
2023-02-22 19:51:14 +08:00
|
|
|
au.SetAlias(userIDs)
|
2023-02-20 10:13:29 +08:00
|
|
|
var no body.Notification
|
|
|
|
|
var extras body.Extras
|
2022-06-02 18:17:11 +08:00
|
|
|
if opts.Signal.ClientMsgID != "" {
|
|
|
|
|
extras.ClientMsgID = opts.Signal.ClientMsgID
|
|
|
|
|
}
|
2022-06-02 18:36:42 +08:00
|
|
|
no.IOSEnableMutableContent()
|
2022-06-02 18:17:11 +08:00
|
|
|
no.SetExtras(extras)
|
2022-09-01 21:05:16 +08:00
|
|
|
no.SetAlert(title)
|
2023-02-22 19:51:14 +08:00
|
|
|
var msg body.Message
|
|
|
|
|
msg.SetMsgContent(content)
|
|
|
|
|
var opt body.Options
|
|
|
|
|
opt.SetApnsProduction(config.Config.IOSPush.Production)
|
|
|
|
|
var pushObj body.PushObj
|
|
|
|
|
pushObj.SetPlatform(&pf)
|
|
|
|
|
pushObj.SetAudience(&au)
|
|
|
|
|
pushObj.SetNotification(&no)
|
|
|
|
|
pushObj.SetMessage(&msg)
|
|
|
|
|
pushObj.SetOptions(&opt)
|
|
|
|
|
var resp interface{}
|
2023-06-05 17:35:41 +08:00
|
|
|
return j.request(ctx, pushObj, resp, 5)
|
2023-02-22 19:51:14 +08:00
|
|
|
}
|
2021-11-10 15:24:59 +08:00
|
|
|
|
2023-06-05 17:35:41 +08:00
|
|
|
func (j *JPush) request(ctx context.Context, po body.PushObj, resp interface{}, timeout int) error {
|
|
|
|
|
return http2.PostReturn(ctx, config.Config.Push.Jpns.PushUrl, map[string]string{"Authorization": j.getAuthorization(config.Config.Push.Jpns.AppKey, config.Config.Push.Jpns.MasterSecret)}, po, resp, timeout)
|
2021-11-10 15:24:59 +08:00
|
|
|
}
|