Skip to content

Commit f90bef0

Browse files
author
施凯伦
committed
fix MessageMultiTopicURL url
1 parent 2ea850b commit f90bef0

File tree

4 files changed

+59
-79
lines changed

4 files changed

+59
-79
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func main() {
2828
- [x] SendToAliasList(msg *Message, aliasList []string)
2929
- [x] SendToUserAccount(msg *Message, userAccount string)
3030
- [x] SendToUserAccountList(msg *Message, accountList []string)
31-
- [ ] Broadcast(msg *Message, topic string)
31+
- [x] Broadcast(msg *Message, topic string)
3232
- [x] BroadcastAll(msg *Message) (*SendResult, error)
3333
- [x] MultiTopicBroadcast(msg *Message, topics []string, topicOP TopicOP)
3434
- [x] CheckScheduleJobExist(msgID string)

client.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ func (m *MiPush) SendTargetMessageList(msgList []*TargetedMessage) (*SendResult,
5959
var bytes []byte
6060
var err error
6161
if msgList[0].targetType == TargetTypeRegID {
62-
bytes, err = m.doPost2(m.host+MultiMessagesRegIDURL, params)
62+
bytes, err = m.doPost(m.host+MultiMessagesRegIDURL, params)
6363
} else if msgList[0].targetType == TargetTypeReAlias {
64-
bytes, err = m.doPost2(m.host+MultiMessagesAliasURL, params)
64+
bytes, err = m.doPost(m.host+MultiMessagesAliasURL, params)
6565
} else if msgList[0].targetType == TargetTypeAccount {
66-
bytes, err = m.doPost2(m.host+MultiMessagesUserAccountURL, params)
66+
bytes, err = m.doPost(m.host+MultiMessagesUserAccountURL, params)
6767
} else {
6868
panic("bad targetType")
6969
}
@@ -434,7 +434,7 @@ func (m *MiPush) assembleSendParams(msg *Message, regID string) url.Values {
434434
return form
435435
}
436436

437-
func (m *MiPush) assembleTargetMessageListParams(msgList []*TargetedMessage) string {
437+
func (m *MiPush) assembleTargetMessageListParams(msgList []*TargetedMessage) url.Values {
438438
form := url.Values{}
439439
type OneMsg struct {
440440
Target string `json:"target"`
@@ -455,7 +455,7 @@ func (m *MiPush) assembleTargetMessageListParams(msgList []*TargetedMessage) str
455455
panic(err)
456456
}
457457
form.Add("messages", string(bytes))
458-
return string(bytes)
458+
return form
459459
}
460460

461461
func (m *MiPush) assembleSendToAlisaParams(msg *Message, alias string) url.Values {
@@ -630,26 +630,6 @@ func (m *MiPush) doPost(url string, form url.Values) ([]byte, error) {
630630
return result, nil
631631
}
632632

633-
func (m *MiPush) doPost2(url string, jsonStr string) ([]byte, error) {
634-
var result []byte
635-
var req *http.Request
636-
var resp *http.Response
637-
var err error
638-
req, err = http.NewRequest("POST", url, strings.NewReader(jsonStr))
639-
req.Header.Set("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8")
640-
req.Header.Set("Authorization", "key="+m.appSecret)
641-
client := &http.Client{}
642-
resp, err = client.Do(req)
643-
if err != nil {
644-
return nil, err
645-
}
646-
result, err = m.handleResponse(resp)
647-
if err != nil {
648-
return nil, err
649-
}
650-
return result, nil
651-
}
652-
653633
func (m *MiPush) doGet(url string, params string) ([]byte, error) {
654634
var result []byte
655635
var req *http.Request

constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const (
1313
MessageAlisaURL = "/v3/message/alias" // 根据alias,发送消息到指定设备上
1414
MessageUserAccountURL = "/v2/message/user_account" // 根据account,发送消息到指定account上
1515
MultiPackageNameMessageMultiTopicURL = "/v3/message/multi_topic" // 根据topic,发送消息到指定一组设备上
16-
MessageMultiTopicURL = "/v2/message/multi_topic" // 根据topic,发送消息到指定一组设备上
16+
MessageMultiTopicURL = "/v2/message/topic" // 根据topic,发送消息到指定一组设备上
1717
MultiPackageNameMessageAllURL = "/v3/message/all" // 向所有设备推送某条消息
1818
MessageAllURL = "/v2/message/all" // 向所有设备推送某条消息
1919
MultiTopicURL = "/v3/message/multi_topic" // 向多个topic广播消息

message.go

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package xiaomipush
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"strconv"
76
"time"
87
)
@@ -25,21 +24,6 @@ const (
2524
MaxTimeToLive = time.Hour * 24 * 7 * 2
2625
)
2726

28-
func NewAndroidMessage(title, description string) *Message {
29-
return &Message{
30-
UniqueID: "",
31-
Payload: "",
32-
Title: title,
33-
Description: description,
34-
PassThrough: 0,
35-
NotifyType: -1, // default notify type
36-
TimeToLive: 0,
37-
TimeToSend: 0,
38-
NotifyID: 0,
39-
Extra: make(map[string]string),
40-
}
41-
}
42-
4327
func (m *Message) SetUniqueID(uniqueID string) *Message {
4428
m.UniqueID = uniqueID
4529
return m
@@ -115,10 +99,61 @@ func (m *Message) JSON() []byte {
11599
if err != nil {
116100
panic(err)
117101
}
118-
fmt.Println("m", string(bytes))
119102
return bytes
120103
}
121104

105+
//-----------------------------------------------------------------------------------//
106+
// 发送给Android设备的Message对象
107+
func NewAndroidMessage(title, description string) *Message {
108+
return &Message{
109+
UniqueID: "",
110+
Payload: "",
111+
Title: title,
112+
Description: description,
113+
PassThrough: 0,
114+
NotifyType: -1, // default notify type
115+
TimeToLive: 0,
116+
TimeToSend: 0,
117+
NotifyID: 0,
118+
Extra: make(map[string]string),
119+
}
120+
}
121+
122+
//-----------------------------------------------------------------------------------//
123+
// 发送给IOS设备的Message对象
124+
func NewIOSMessage(description string) *Message {
125+
return &Message{
126+
UniqueID: "",
127+
Payload: "",
128+
Title: "",
129+
Description: description,
130+
PassThrough: 0,
131+
NotifyType: -1, // default notify type
132+
TimeToLive: 0,
133+
TimeToSend: 0,
134+
NotifyID: 0,
135+
Extra: make(map[string]string),
136+
}
137+
}
138+
139+
// 可选项,自定义通知数字角标。
140+
func (i *Message) SetBadge(badge int64) *Message {
141+
i.Extra["badge"] = strconv.FormatInt(badge, 10)
142+
return i
143+
}
144+
145+
// 可选项,iOS8推送消息快速回复类别。
146+
func (i *Message) SetCategory(category string) *Message {
147+
i.Extra["category"] = category
148+
return i
149+
}
150+
151+
// 可选项,自定义消息铃声。
152+
func (i *Message) SetSoundURL(soundURL string) *Message {
153+
i.Extra["sound_url"] = soundURL
154+
return i
155+
}
156+
122157
//-----------------------------------------------------------------------------------//
123158
// TargetedMessage封装了MiPush推送服务系统中的消息Message对象,和该Message对象所要发送到的目标。
124159

@@ -161,38 +196,3 @@ func (tm *TargetedMessage) JSON() []byte {
161196
}
162197
return bytes
163198
}
164-
165-
//-----------------------------------------------------------------------------------//
166-
// 发送给IOS设备的Message对象
167-
func NewIOSMessage(description string) *Message {
168-
return &Message{
169-
UniqueID: "",
170-
Payload: "",
171-
Title: "",
172-
Description: description,
173-
PassThrough: 0,
174-
NotifyType: -1, // default notify type
175-
TimeToLive: 0,
176-
TimeToSend: 0,
177-
NotifyID: 0,
178-
Extra: make(map[string]string),
179-
}
180-
}
181-
182-
// 可选项,自定义通知数字角标。
183-
func (i *Message) SetBadge(badge int64) *Message {
184-
i.Extra["badge"] = strconv.FormatInt(badge, 10)
185-
return i
186-
}
187-
188-
// 可选项,iOS8推送消息快速回复类别。
189-
func (i *Message) SetCategory(category string) *Message {
190-
i.Extra["category"] = category
191-
return i
192-
}
193-
194-
// 可选项,自定义消息铃声。
195-
func (i *Message) SetSoundURL(soundURL string) *Message {
196-
i.Extra["sound_url"] = soundURL
197-
return i
198-
}

0 commit comments

Comments
 (0)