Verified Commit f474a9f1 authored by Cui's avatar Cui

add appMessage

parent 3065466b
......@@ -4,5 +4,5 @@ require (
github.com/heycayc/chkcki v0.0.0-20180425093509-ad62647adf04
github.com/sirupsen/logrus v1.3.0
github.com/valyala/fasthttp v1.0.0
gitlab.qingclass.cn/library/utils v0.1.17
gitlab.qingclass.cn/library/jpush v0.0.1
)
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/heycayc/chkcki v0.0.0-20180425093509-ad62647adf04/go.mod h1:68nSgivWifFndv3X4OMS7EIuHaNxJb1PlYECLiH99jU=
github.com/klauspost/compress v1.4.0/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/cpuid v0.0.0-20180405133222-e7e905edc00e/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.0.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
gitlab.qingclass.cn/library/jpush v0.0.1 h1:zXqwo7C6+fvTccAqLyAWTyYRUXXqEPq1W7ll9oYidmY=
gitlab.qingclass.cn/library/jpush v0.0.1/go.mod h1:h4wvcwtBQSlZKJhpE2FwcXFLIZREfB7fBNYJSCWdGhE=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
package sender
import (
"encoding/json"
"github.com/sirupsen/logrus"
. "gitlab.qingclass.cn/library/utils"
Push "gitlab.qingclass.cn/library/jpush"
"gitlab.qingclass.cn/library/jpush/push"
)
// const appKey = "2eccc8d57e1bfd374e690f9d"
// const masterSecret = "30ce7445d75af8a6dd0f251b"
func AppMessageSender(am AppMessage, appKey, masterSecret string) (statusCode int, errorStr string) {
log := Logger.WithFields(logrus.Fields{
"position": "jpush.go",
"func": "AppMessageSender",
})
client := Push.NewJPushClient(appKey, masterSecret)
platform := push.NewPlatform()
platform.All() // Add("android")
audience := push.NewAudience()
audience.SetRegistrationId(am.ToUser)
notification := push.NewNotification(am.Title)
// android 平台专有的 notification,用 alert 属性初始化
androidNotification := push.NewAndroidNotification(am.SubTitle)
androidNotification.Title = am.Title
iosNotification := push.NewIosNotification(am.Title)
notification.Android = androidNotification
notification.Ios = iosNotification
// option 对象,表示推送可选项
options := push.NewOptions()
// iOS 平台,是否推送生产环境,false 表示开发环境;如果不指定,就是生产环境
options.ApnsProduction = false
// Options 的 Validate 方法会对 time_to_live 属性做范围限制,以满足 JPush 的规范
options.TimeToLive = 10000000
// Options 的 Validate 方法会对 big_push_duration 属性做范围限制,以满足 JPush 的规范
options.BigPushDuration = 1500
if am.Extra.MessageId != "" {
androidNotification.AddExtra("messageId", am.Extra.MessageId)
iosNotification.AddExtra("messageId", am.Extra.MessageId)
}
androidNotification.AddExtra("unReadCount", am.Extra.UnReadCount)
iosNotification.AddExtra("unReadCount", am.Extra.UnReadCount)
payload := push.NewPushObject()
payload.Platform = platform
payload.Audience = audience
payload.Notification = notification
payload.Options = options
// result, err := client.PushValidate(payload)
result, err := client.Push(payload)
res, _ := json.Marshal(result)
if err != nil {
log.
WithField("error", err).
Error("AppMessageSender Error ")
statusCode = -1
errorStr = "AppMessageSender Error -" + err.Error()
} else if result.StatusCode != 200 {
log.
WithField("error", result.Error.String()).
Error("AppMessageSender Error ")
statusCode = -2
errorStr = "AppMessageSender Error -" + result.Error.String()
} else {
log.
WithFields(am.ToFields()).
WithField("response", string(res)).
Info("AppMessageSender send success !")
}
return
}
......@@ -2,12 +2,13 @@ package sender
import (
"encoding/json"
"github.com/sirupsen/logrus"
)
type MessageLog struct {
MessageType string `json:"messageType" bson:"messageType"` // CUSTOMER-客服消息 TEMPLATE-模板消息 SUBSCRIBE-订阅消息 SMS-短信消息 MASS-高级群发
MessageSubType string `json:"messageSubType" bson:"messageSubType"` // TEXT IMAGE VOICE VIDEO MUSIC NEWS MPNEWS WXCARD MINIPROGRAMPAGE
MessageType string `json:"messageType" bson:"messageType"` // CUSTOMER-客服消息 TEMPLATE-模板消息 SUBSCRIBE-订阅消息 SMS-短信消息 MASS-高级群发 APP-app 消息
MessageSubType string `json:"messageSubType" bson:"messageSubType"` // TEXT IMAGE VOICE VIDEO MUSIC NEWS MPNEWS WXCARD MINIPROGRAMPAGE ANDROID IOS
RemindType string `json:"remindType" bson:"remindType"` // 开课提醒、购买成功通知、上课提醒、打卡失败提醒、复习提醒、结课提醒、到期提醒、获得补卡券提醒、提前获得奖学金提醒、现金券到期提醒、成绩单提醒、提成消息<br/>classBegin,purchaseSuccessNotice,classRemind,PUNCH_FAIL,review,classEnd,Expire,replacementCardGain,previousScholarship,CouponExpire,transcript,profit
JsonId string `json:"jsonId"` // 分组ID
UserOpenid string `json:"userOpenid" bson:"userOpenid"` // 用户openid
......@@ -52,6 +53,9 @@ type MessageStruct struct {
SmsAppId int `json:"smsAppId" bson:"smsAppId"`
SmsAppKey string `json:"smsAppKey" bson:"smsAppKey"`
JAppKey string `json:"jappKey"`
JAppSecret string `json:"jappSecret"`
Retry int `json:"retry"`
PushCount int `json:"pushCount"`
......@@ -361,3 +365,36 @@ func (sm *SmsMessage) ToFields() (fields logrus.Fields) {
fields["time"] = sm.Time
return
}
// App 消息
type AppMessage struct {
ToUser []string `json:"toUser"`
Title string `json:"title"`
SubTitle string `json:"subTitle"`
// MsgContent string `json:"msg_content"`
// JumpEnable bool `json:"jumpEnable"`
// JumpUrl string `json:"jumpUrl"`
// JumpType string `json:"jumpType"` // enum: [ 'internal', 'external' ],
// JumpParams map[string]interface{} `json:"jumpParams"`
Extra struct {
UnReadCount int `json:"unReadCount"`
MessageId string `json:"messageId"`
} `json:"extra"`
}
func (am *AppMessage) ToFields() (fields logrus.Fields) {
fields = make(logrus.Fields)
fields["title"] = am.Title
fields["subTitle"] = am.SubTitle
// fields["msg_content"] = am.MsgContent
// fields["jumpEnable"] = am.JumpEnable
// fields["jumpUrl"] = am.JumpUrl
// fields["jumpType"] = am.JumpType
fields["unReadCount"] = am.Extra.UnReadCount
fields["messageId"] = am.Extra.MessageId
return
}
......@@ -2,7 +2,9 @@ package sender
import (
"encoding/json"
"github.com/sirupsen/logrus"
. "gitlab.qingclass.cn/library/utils"
)
......@@ -197,6 +199,22 @@ func Sender(accessToken string, message MessageStruct) (code int, msg string) {
code, msg = SmsMessageSender(sm, message.SmsAppId, message.SmsAppKey)
break
case "APP":
var am AppMessage
err := json.Unmarshal(message.MessageData, &am)
if err != nil {
msg = "Unmarshal AppMessage failed"
log.
WithField("error", err).
Error(msg)
code = -10005
break
}
code, msg = AppMessageSender(am, message.JAppKey, message.JAppSecret)
break
default:
msg = "MessageType is not match"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment