Skip to content

Commit 31a7890

Browse files
committed
add harmony push
1 parent 251041e commit 31a7890

3 files changed

Lines changed: 138 additions & 92 deletions

File tree

constname.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,28 @@ var honorMessageTypeImportance = map[MessageType]string{
9999
InstantMsg: "NORMAL",
100100
}
101101

102+
/*
103+
harmony category意义:
104+
105+
ACCOUNT(帐号动态):
106+
用户帐号和帐号下资源资产的动态信息。
107+
帐号:帐号上下线、帐号状态变化、帐号信息认证等。
108+
资产:会员到期/过期、续费提醒、余额变动(余额必须为真实的资产变动,且需排除积分变动、金币变动,排名更新等)。
109+
110+
MARKETING(资讯营销类):
111+
包含 内容资讯 和 营销活动 两大类
112+
*/
113+
114+
// harmony category 定义在华为开发者后台
115+
var harmonyMessageTypeCategory = map[MessageType]string{
116+
ArticleMsg: "MARKETING",
117+
AlgorithmReCommendMsg: "MARKETING",
118+
AttendRecommendMsg: "MARKETING",
119+
PlatformActionMsg: "MARKETING", //由于小米的分类比较详细,后台需要分为四类,对harmony来说,都归到资讯营销类
120+
UserAccountMsg: "ACCOUNT", //账号动态
121+
InstantMsg: "IM", //即时聊天
122+
}
123+
102124
/*
103125
VIVO category意义:
104126
@@ -228,6 +250,15 @@ func (m MessageType) GetOPPOChannelId() string {
228250
return channelId
229251
}
230252

253+
// GetHarmonyCategory 鸿蒙: 不同消息类型对应的category
254+
func (m MessageType) GetHarmonyCategory() string {
255+
category := "MARKETING"
256+
if harmonyMessageTypeCategory[m] != "" {
257+
category = harmonyMessageTypeCategory[m]
258+
}
259+
return category
260+
}
261+
231262
// ClientType APP客户端类型
232263
type ClientType int
233264

getui.go

Lines changed: 59 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,27 @@ type PushStore struct {
5656
Key string //存储key名称
5757
}
5858

59+
// HarmonyConfig 鸿蒙包名等配置
60+
type HarmonyConfig struct {
61+
BundleName string //包名
62+
AbilityName string //鸿蒙配置
63+
Action string //鸿蒙配置
64+
Uri string //鸿蒙配置
65+
}
66+
67+
type AppConfig struct {
68+
Harmony *HarmonyConfig
69+
}
70+
5971
// PushClient 个推 push client
6072
type PushClient struct {
6173
*PushConfig
6274
*PushStore
75+
*AppConfig
6376
}
6477

6578
// NewPushClient 返回个推实例并初始化redis信息
66-
func NewPushClient(conf *PushConfig, store *PushStore, toDebug bool) (client *PushClient, err error) {
79+
func NewPushClient(conf *PushConfig, store *PushStore, app *AppConfig, toDebug bool) (client *PushClient, err error) {
6780
if conf == nil {
6881
err = errors.New("配置为空")
6982
return
@@ -86,6 +99,7 @@ func NewPushClient(conf *PushConfig, store *PushStore, toDebug bool) (client *Pu
8699
client = &PushClient{
87100
PushConfig: conf,
88101
PushStore: store,
102+
AppConfig: app,
89103
}
90104
err = goredis.InitDefaultDB(&goredis.RedisConfig{
91105
Host: store.Host,
@@ -400,7 +414,7 @@ func (g *PushClient) PushAll(msgType, scheduleTime int, payload *models.CustomMe
400414
if err != nil {
401415
return
402416
}
403-
pushMessage, pushChannel, setting, err := getPushMessageAndChannel(msgType, scheduleTime, payload)
417+
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
404418
if err != nil {
405419
return
406420
}
@@ -435,7 +449,7 @@ func (g *PushClient) PushAllByClient(msgType, scheduleTime int, clientType Clien
435449
if err != nil {
436450
return
437451
}
438-
pushMessage, pushChannel, setting, err := getPushMessageAndChannel(msgType, scheduleTime, payload)
452+
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
439453
if err != nil {
440454
return
441455
}
@@ -492,7 +506,7 @@ func (g *PushClient) PushSingleByCid(msgType int, cid string, payload *models.Cu
492506
if err != nil {
493507
return
494508
}
495-
pushMessage, pushChannel, setting, err := getPushMessageAndChannel(msgType, 0, payload)
509+
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, 0, payload)
496510
if err != nil {
497511
return
498512
}
@@ -530,7 +544,7 @@ func (g *PushClient) PushSingleByAlias(msgType int, alias string, payload *model
530544
if err != nil {
531545
return
532546
}
533-
pushMessage, pushChannel, setting, err := getPushMessageAndChannel(msgType, 0, payload)
547+
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, 0, payload)
534548
if err != nil {
535549
return
536550
}
@@ -572,7 +586,7 @@ func (g *PushClient) PushListByCid(msgType int, cid []string, payload *models.Cu
572586
if err != nil {
573587
return
574588
}
575-
pushMessage, pushChannel, setting, err := getPushMessageAndChannel(msgType, 0, payload)
589+
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, 0, payload)
576590
if err != nil {
577591
return
578592
}
@@ -638,7 +652,7 @@ func (g *PushClient) PushAllByCustomTag(msgType, scheduleTime int, customTag []s
638652
if err != nil {
639653
return
640654
}
641-
pushMessage, pushChannel, setting, err := getPushMessageAndChannel(msgType, scheduleTime, payload)
655+
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
642656
if err != nil {
643657
return
644658
}
@@ -686,7 +700,7 @@ func (g *PushClient) PushAllByLogicTags(msgType, scheduleTime int, tags []*model
686700
if err != nil {
687701
return
688702
}
689-
pushMessage, pushChannel, setting, err := getPushMessageAndChannel(msgType, scheduleTime, payload)
703+
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
690704
if err != nil {
691705
return
692706
}
@@ -731,7 +745,7 @@ func (g *PushClient) PushAppByFastCustomTag(msgType, scheduleTime int, tag strin
731745
if err != nil {
732746
return
733747
}
734-
pushMessage, pushChannel, setting, err := getPushMessageAndChannel(msgType, scheduleTime, payload)
748+
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
735749
if err != nil {
736750
return
737751
}
@@ -787,7 +801,7 @@ private
787801
// msgType 消息类型
788802
// scheduleTime 定时任务的时间戳
789803
// payload 消息结构体
790-
func getPushMessageAndChannel(msgType int, scheduleTime int, payload *models.CustomMessage) (pushMessage *models.PushMessage, pushChannel *models.PushChannel, setting *models.Setting, err error) {
804+
func (m *PushClient) getPushMessageAndChannel(msgType int, scheduleTime int, payload *models.CustomMessage) (pushMessage *models.PushMessage, pushChannel *models.PushChannel, setting *models.Setting, err error) {
791805
payload.Title = strings.TrimSpace(payload.Title)
792806
pushInfo, err := json.Marshal(payload)
793807
if err != nil {
@@ -801,6 +815,8 @@ func getPushMessageAndChannel(msgType int, scheduleTime int, payload *models.Cus
801815
setting.Strategy.IOS = 2
802816
setting.Strategy.Default = 1
803817
setting.Strategy.HW = 1
818+
setting.Strategy.HO = 1
819+
setting.Strategy.HOSHW = 1
804820
setting.Strategy.OP = 1
805821
setting.Strategy.VV = 1
806822
setting.Strategy.XM = 1
@@ -848,82 +864,14 @@ func getPushMessageAndChannel(msgType int, scheduleTime int, payload *models.Cus
848864
android.Ups.Notification = &models.UPSNotification{
849865
Title: payload.Title,
850866
Body: payload.Content,
851-
ClickType: "intent",
867+
ClickType: "intent", //打开应用内特定页面(厂商都支持)
852868
Intent: getIntent(payload.Url),
853869
NotifyId: uint(time.Now().Unix()),
854870
}
855871

856872
// android 离线推送通道
857873
// 以下为厂商配置
858874

859-
// 营销/全推类消息
860-
//if channelType == PublicChannel {
861-
// android.Ups.Options.All.Channel = "yuanmeng_push"
862-
//
863-
// //华为 OK except p10
864-
// android.Ups.Options.Hw = map[string]interface{}{
865-
// "/message/android/notification/default_sound": true,
866-
// "/message/android/notification/channel_id": "yuanmeng_push",
867-
// "/message/android/notification/visibility": "PUBLIC",
868-
// "/message/android/notification/importance": "LOW",
869-
// }
870-
//
871-
// //荣耀
872-
// android.Ups.Options.Ho = map[string]interface{}{
873-
// "/android/notification/importance": "LOW",
874-
// }
875-
//
876-
// // oppo
877-
// android.Ups.Options.Op = map[string]interface{}{
878-
// "/channel_id": "yuanmeng_push",
879-
// }
880-
//
881-
// // 小米公共
882-
// android.Ups.Options.Xm = map[string]interface{}{
883-
// "/extra.channel_id": "pre213",
884-
// "notifyType": -1,
885-
// }
886-
//
887-
// logx.Errorf("小米新的channel")
888-
//
889-
// //vivo
890-
// android.Ups.Options.Vv.Classification = 0
891-
// android.Ups.Options.Vv.NotifyType = 4
892-
//}
893-
//
894-
////聊天、即时类消息
895-
//if channelType == PrivateChannel {
896-
// android.Ups.Options.All.Channel = "yuanmeng_push_im"
897-
//
898-
// //华为 OK except p10
899-
// android.Ups.Options.Hw = map[string]interface{}{
900-
// "/message/android/notification/default_sound": true,
901-
// "/message/android/notification/channel_id": "yuanmeng_push_im",
902-
// "/message/android/notification/visibility": "PUBLIC",
903-
// "/message/android/notification/importance": "NORMAL",
904-
// }
905-
//
906-
// //荣耀
907-
// android.Ups.Options.Ho = map[string]interface{}{
908-
// "/android/notification/importance": "NORMAL",
909-
// }
910-
//
911-
// // oppo
912-
// android.Ups.Options.Op = map[string]interface{}{
913-
// "/channel_id": "yuanmeng_push_im",
914-
// }
915-
//
916-
// // 小米聊天
917-
// android.Ups.Options.Xm = map[string]interface{}{
918-
// "/extra.channel_id": "high_system",
919-
// "notifyType": -1,
920-
// }
921-
//
922-
// //vivo
923-
// android.Ups.Options.Vv.Classification = 1
924-
// android.Ups.Options.Vv.NotifyType = 4
925-
//}
926-
927875
//根据最新的消息推送规定,需要按照指定的消息类型推送,不再仅分为 公用消息 和 聊天消息
928876
if msgType > 0 {
929877
android.Ups.Options.All.Channel = "yuanmeng_push"
@@ -937,7 +885,6 @@ func getPushMessageAndChannel(msgType int, scheduleTime int, payload *models.Cus
937885
"/extra.channel_id": ximiChannelId,
938886
"notifyType": -1,
939887
}
940-
//logx.Errorf("【小米推送】channelID:%v", ximiChannelId)
941888

942889
//华为
943890
huaweiChannelId, huaweiCategory, importance := MessageType(msgType).GetHuaweiInfo()
@@ -948,14 +895,12 @@ func getPushMessageAndChannel(msgType int, scheduleTime int, payload *models.Cus
948895
"/message/android/notification/visibility": "PUBLIC", //最新接口已没有此参数
949896
"/message/android/notification/importance": importance,
950897
}
951-
//logx.Errorf("【华为】channelId:%v, category:%v, importance:%v", huaweiChannelId, huaweiCategory, importance)
952898

953899
//荣耀
954900
honorImportance := MessageType(msgType).GetHonorImportance()
955901
android.Ups.Options.Ho = map[string]interface{}{
956902
"/android/notification/importance": honorImportance,
957903
}
958-
//logx.Errorf("【荣耀】importance:%v", honorImportance)
959904

960905
//vivo
961906
vvClassification := MessageType(msgType).GetViVoClassification()
@@ -965,19 +910,49 @@ func getPushMessageAndChannel(msgType int, scheduleTime int, payload *models.Cus
965910
"/notifyType": 4,
966911
"/category": vvCategory,
967912
}
968-
//logx.Errorf("【vivo】classification:%v, category:%v", vvClassification, vvCategory)
969913

970914
// oppo
971915
oppoChannelId := MessageType(msgType).GetOPPOChannelId()
972916
android.Ups.Options.Op = map[string]interface{}{
973917
"/channel_id": oppoChannelId,
974918
}
975-
//logx.Errorf("【oppo】channelId:%v", oppoChannelId)
976919
}
977920
pushChannel = &models.PushChannel{
978921
Android: android,
979922
IOS: ios,
980923
}
924+
925+
// harmony 厂商通知 配置
926+
if m.AppConfig != nil && m.AppConfig.Harmony != nil {
927+
harmony := &models.HarmonyChannel{}
928+
harmony.Notification = &models.HarmonyNotification{
929+
Title: payload.Title,
930+
Body: payload.Content,
931+
Category: "",
932+
ClickType: "want",
933+
Payload: "",
934+
NotifyId: uint(time.Now().Unix()),
935+
}
936+
wantData := &models.WantData{
937+
DeviceId: "",
938+
BundleName: m.AppConfig.Harmony.BundleName,
939+
AbilityName: m.AppConfig.Harmony.AbilityName,
940+
Action: m.AppConfig.Harmony.Action,
941+
Uri: "",
942+
Parameters: nil,
943+
}
944+
//parameters中添加"gttask":""参数后,个推会自动在 [want] 里拼接 taskid 和 actionid,app 端接收到参数可以用于上报点击埋点
945+
param := make(map[string]interface{})
946+
param["gttask"] = ""
947+
param["data"] = payload
948+
wantData.Parameters = param
949+
b, _ := json.Marshal(wantData)
950+
harmony.Notification.Want = string(b)
951+
952+
//消息分类
953+
harmony.Notification.Category = MessageType(msgType).GetHarmonyCategory()
954+
pushChannel.Harmony = harmony
955+
}
981956
return
982957
}
983958

0 commit comments

Comments
 (0)