复制项目
This commit is contained in:
64
internal/push/offlinepush/jpush/body/audience.go
Normal file
64
internal/push/offlinepush/jpush/body/audience.go
Normal file
@@ -0,0 +1,64 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package body
|
||||
|
||||
const (
|
||||
TAG = "tag"
|
||||
TAGAND = "tag_and"
|
||||
TAGNOT = "tag_not"
|
||||
ALIAS = "alias"
|
||||
REGISTRATIONID = "registration_id"
|
||||
)
|
||||
|
||||
type Audience struct {
|
||||
Object any
|
||||
audience map[string][]string
|
||||
}
|
||||
|
||||
func (a *Audience) set(key string, v []string) {
|
||||
if a.audience == nil {
|
||||
a.audience = make(map[string][]string)
|
||||
a.Object = a.audience
|
||||
}
|
||||
// v, ok = this.audience[key]
|
||||
// if ok {
|
||||
// return
|
||||
//}
|
||||
a.audience[key] = v
|
||||
}
|
||||
|
||||
func (a *Audience) SetTag(tags []string) {
|
||||
a.set(TAG, tags)
|
||||
}
|
||||
|
||||
func (a *Audience) SetTagAnd(tags []string) {
|
||||
a.set(TAGAND, tags)
|
||||
}
|
||||
|
||||
func (a *Audience) SetTagNot(tags []string) {
|
||||
a.set(TAGNOT, tags)
|
||||
}
|
||||
|
||||
func (a *Audience) SetAlias(alias []string) {
|
||||
a.set(ALIAS, alias)
|
||||
}
|
||||
|
||||
func (a *Audience) SetRegistrationId(ids []string) {
|
||||
a.set(REGISTRATIONID, ids)
|
||||
}
|
||||
|
||||
func (a *Audience) SetAll() {
|
||||
a.Object = "all"
|
||||
}
|
||||
41
internal/push/offlinepush/jpush/body/message.go
Normal file
41
internal/push/offlinepush/jpush/body/message.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package body
|
||||
|
||||
type Message struct {
|
||||
MsgContent string `json:"msg_content"`
|
||||
Title string `json:"title,omitempty"`
|
||||
ContentType string `json:"content_type,omitempty"`
|
||||
Extras map[string]any `json:"extras,omitempty"`
|
||||
}
|
||||
|
||||
func (m *Message) SetMsgContent(c string) {
|
||||
m.MsgContent = c
|
||||
}
|
||||
|
||||
func (m *Message) SetTitle(t string) {
|
||||
m.Title = t
|
||||
}
|
||||
|
||||
func (m *Message) SetContentType(c string) {
|
||||
m.ContentType = c
|
||||
}
|
||||
|
||||
func (m *Message) SetExtras(key string, value any) {
|
||||
if m.Extras == nil {
|
||||
m.Extras = make(map[string]any)
|
||||
}
|
||||
m.Extras[key] = value
|
||||
}
|
||||
72
internal/push/offlinepush/jpush/body/notification.go
Normal file
72
internal/push/offlinepush/jpush/body/notification.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package body
|
||||
|
||||
import (
|
||||
"git.imall.cloud/openim/open-im-server-deploy/internal/push/offlinepush/options"
|
||||
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/config"
|
||||
)
|
||||
|
||||
type Notification struct {
|
||||
Alert string `json:"alert,omitempty"`
|
||||
Android Android `json:"android,omitempty"`
|
||||
IOS Ios `json:"ios,omitempty"`
|
||||
}
|
||||
|
||||
type Android struct {
|
||||
Alert string `json:"alert,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
Intent struct {
|
||||
URL string `json:"url,omitempty"`
|
||||
} `json:"intent,omitempty"`
|
||||
Extras map[string]string `json:"extras,omitempty"`
|
||||
}
|
||||
type Ios struct {
|
||||
Alert IosAlert `json:"alert,omitempty"`
|
||||
Sound string `json:"sound,omitempty"`
|
||||
Badge string `json:"badge,omitempty"`
|
||||
Extras map[string]string `json:"extras,omitempty"`
|
||||
MutableContent bool `json:"mutable-content"`
|
||||
}
|
||||
|
||||
type IosAlert struct {
|
||||
Title string `json:"title,omitempty"`
|
||||
Body string `json:"body,omitempty"`
|
||||
}
|
||||
|
||||
func (n *Notification) SetAlert(alert string, title string, opts *options.Opts) {
|
||||
n.Alert = alert
|
||||
n.Android.Alert = alert
|
||||
n.Android.Title = title
|
||||
n.IOS.Alert.Body = alert
|
||||
n.IOS.Alert.Title = title
|
||||
n.IOS.Sound = opts.IOSPushSound
|
||||
if opts.IOSBadgeCount {
|
||||
n.IOS.Badge = "+1"
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notification) SetExtras(extras map[string]string) {
|
||||
n.IOS.Extras = extras
|
||||
n.Android.Extras = extras
|
||||
}
|
||||
|
||||
func (n *Notification) SetAndroidIntent(pushConf *config.Push) {
|
||||
n.Android.Intent.URL = pushConf.JPush.PushIntent
|
||||
}
|
||||
|
||||
func (n *Notification) IOSEnableMutableContent() {
|
||||
n.IOS.MutableContent = true
|
||||
}
|
||||
23
internal/push/offlinepush/jpush/body/options.go
Normal file
23
internal/push/offlinepush/jpush/body/options.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package body
|
||||
|
||||
type Options struct {
|
||||
ApnsProduction bool `json:"apns_production"`
|
||||
}
|
||||
|
||||
func (o *Options) SetApnsProduction(c bool) {
|
||||
o.ApnsProduction = c
|
||||
}
|
||||
99
internal/push/offlinepush/jpush/body/platform.go
Normal file
99
internal/push/offlinepush/jpush/body/platform.go
Normal file
@@ -0,0 +1,99 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package body
|
||||
|
||||
import (
|
||||
"github.com/openimsdk/tools/errs"
|
||||
|
||||
"git.imall.cloud/openim/protocol/constant"
|
||||
)
|
||||
|
||||
const (
|
||||
ANDROID = "android"
|
||||
IOS = "ios"
|
||||
QUICKAPP = "quickapp"
|
||||
WINDOWSPHONE = "winphone"
|
||||
ALL = "all"
|
||||
)
|
||||
|
||||
type Platform struct {
|
||||
Os any
|
||||
osArry []string
|
||||
}
|
||||
|
||||
func (p *Platform) Set(os string) error {
|
||||
if p.Os == nil {
|
||||
p.osArry = make([]string, 0, 4)
|
||||
} else {
|
||||
switch p.Os.(type) {
|
||||
case string:
|
||||
return errs.New("platform is all")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
for _, value := range p.osArry {
|
||||
if os == value {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
switch os {
|
||||
case IOS:
|
||||
fallthrough
|
||||
case ANDROID:
|
||||
fallthrough
|
||||
case QUICKAPP:
|
||||
fallthrough
|
||||
case WINDOWSPHONE:
|
||||
p.osArry = append(p.osArry, os)
|
||||
p.Os = p.osArry
|
||||
default:
|
||||
return errs.New("unknow platform")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Platform) SetPlatform(platform string) error {
|
||||
switch platform {
|
||||
case constant.AndroidPlatformStr:
|
||||
return p.SetAndroid()
|
||||
case constant.IOSPlatformStr:
|
||||
return p.SetIOS()
|
||||
default:
|
||||
return errs.New("platform err")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Platform) SetIOS() error {
|
||||
return p.Set(IOS)
|
||||
}
|
||||
|
||||
func (p *Platform) SetAndroid() error {
|
||||
return p.Set(ANDROID)
|
||||
}
|
||||
|
||||
func (p *Platform) SetQuickApp() error {
|
||||
return p.Set(QUICKAPP)
|
||||
}
|
||||
|
||||
func (p *Platform) SetWindowsPhone() error {
|
||||
return p.Set(WINDOWSPHONE)
|
||||
}
|
||||
|
||||
func (p *Platform) SetAll() {
|
||||
p.Os = ALL
|
||||
}
|
||||
43
internal/push/offlinepush/jpush/body/pushobj.go
Normal file
43
internal/push/offlinepush/jpush/body/pushobj.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package body
|
||||
|
||||
type PushObj struct {
|
||||
Platform any `json:"platform"`
|
||||
Audience any `json:"audience"`
|
||||
Notification any `json:"notification,omitempty"`
|
||||
Message any `json:"message,omitempty"`
|
||||
Options any `json:"options,omitempty"`
|
||||
}
|
||||
|
||||
func (p *PushObj) SetPlatform(pf *Platform) {
|
||||
p.Platform = pf.Os
|
||||
}
|
||||
|
||||
func (p *PushObj) SetAudience(ad *Audience) {
|
||||
p.Audience = ad.Object
|
||||
}
|
||||
|
||||
func (p *PushObj) SetNotification(no *Notification) {
|
||||
p.Notification = no
|
||||
}
|
||||
|
||||
func (p *PushObj) SetMessage(m *Message) {
|
||||
p.Message = m
|
||||
}
|
||||
|
||||
func (p *PushObj) SetOptions(o *Options) {
|
||||
p.Options = o
|
||||
}
|
||||
107
internal/push/offlinepush/jpush/push.go
Normal file
107
internal/push/offlinepush/jpush/push.go
Normal file
@@ -0,0 +1,107 @@
|
||||
// Copyright © 2023 OpenIM. All rights reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package jpush
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
||||
"git.imall.cloud/openim/open-im-server-deploy/internal/push/offlinepush/jpush/body"
|
||||
"git.imall.cloud/openim/open-im-server-deploy/internal/push/offlinepush/options"
|
||||
"git.imall.cloud/openim/open-im-server-deploy/pkg/common/config"
|
||||
"github.com/openimsdk/tools/utils/httputil"
|
||||
)
|
||||
|
||||
type JPush struct {
|
||||
pushConf *config.Push
|
||||
httpClient *httputil.HTTPClient
|
||||
}
|
||||
|
||||
func NewClient(pushConf *config.Push) *JPush {
|
||||
return &JPush{pushConf: pushConf,
|
||||
httpClient: httputil.NewHTTPClient(httputil.NewClientConfig()),
|
||||
}
|
||||
}
|
||||
|
||||
func (j *JPush) Auth(apiKey, secretKey string, timeStamp int64) (token string, err error) {
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func (j *JPush) SetAlias(cid, alias string) (resp string, err error) {
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (j *JPush) getAuthorization(appKey string, masterSecret string) string {
|
||||
str := fmt.Sprintf("%s:%s", appKey, masterSecret)
|
||||
buf := []byte(str)
|
||||
Authorization := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(buf))
|
||||
return Authorization
|
||||
}
|
||||
|
||||
func (j *JPush) Push(ctx context.Context, userIDs []string, title, content string, opts *options.Opts) error {
|
||||
var pf body.Platform
|
||||
pf.SetAll()
|
||||
var au body.Audience
|
||||
au.SetAlias(userIDs)
|
||||
var no body.Notification
|
||||
extras := make(map[string]string)
|
||||
extras["ex"] = opts.Ex
|
||||
if opts.Signal.ClientMsgID != "" {
|
||||
extras["ClientMsgID"] = opts.Signal.ClientMsgID
|
||||
}
|
||||
no.IOSEnableMutableContent()
|
||||
no.SetExtras(extras)
|
||||
no.SetAlert(content, title, opts)
|
||||
no.SetAndroidIntent(j.pushConf)
|
||||
|
||||
var msg body.Message
|
||||
msg.SetMsgContent(content)
|
||||
msg.SetTitle(title)
|
||||
if opts.Signal.ClientMsgID != "" {
|
||||
msg.SetExtras("ClientMsgID", opts.Signal.ClientMsgID)
|
||||
}
|
||||
msg.SetExtras("ex", opts.Ex)
|
||||
var opt body.Options
|
||||
opt.SetApnsProduction(j.pushConf.IOSPush.Production)
|
||||
var pushObj body.PushObj
|
||||
pushObj.SetPlatform(&pf)
|
||||
pushObj.SetAudience(&au)
|
||||
pushObj.SetNotification(&no)
|
||||
pushObj.SetMessage(&msg)
|
||||
pushObj.SetOptions(&opt)
|
||||
var resp map[string]any
|
||||
return j.request(ctx, pushObj, &resp, 5)
|
||||
}
|
||||
|
||||
func (j *JPush) request(ctx context.Context, po body.PushObj, resp *map[string]any, timeout int) error {
|
||||
err := j.httpClient.PostReturn(
|
||||
ctx,
|
||||
j.pushConf.JPush.PushURL,
|
||||
map[string]string{
|
||||
"Authorization": j.getAuthorization(j.pushConf.JPush.AppKey, j.pushConf.JPush.MasterSecret),
|
||||
},
|
||||
po,
|
||||
resp,
|
||||
timeout,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if (*resp)["sendno"] != "0" {
|
||||
return fmt.Errorf("jpush push failed %v", resp)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user