-
Notifications
You must be signed in to change notification settings - Fork 1
/
push.go
301 lines (264 loc) · 7.4 KB
/
push.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package model
import (
"encoding/json"
"errors"
"fmt"
"github.com/33cn/chat33/app"
"github.com/33cn/chat33/orm"
push "github.com/33cn/chat33/pkg/u-push"
android_push "github.com/33cn/chat33/pkg/u-push/android"
ios_push "github.com/33cn/chat33/pkg/u-push/ios"
"github.com/33cn/chat33/proto"
"github.com/33cn/chat33/router"
"github.com/33cn/chat33/types"
"github.com/33cn/chat33/utility"
"github.com/inconshreveable/log15"
)
var pushLog = log15.New("module", "push")
//
type IPusher interface {
singlePush(title, text, targetId string, channelType int) error
}
type androidPusher struct {
AppKey string
AppMasterSecret string
MiActivity string
DeviceToken string
}
type iOSPusher struct {
AppKey string
AppMasterSecret string
MiActivity string
DeviceToken string
}
func checkDeviceToken(app *types.App, userId, deviceToken string) (IPusher, error) {
deviceType, nowUser, err := orm.FindUserIdByDeviceToken(deviceToken)
if err != nil {
return nil, errors.New("check device token failed :" + err.Error())
}
if nowUser != userId {
return nil, nil
}
var pusher IPusher
switch deviceType {
case types.DeviceAndroid:
pusher = &androidPusher{
AppKey: app.PushAppKey.Android,
AppMasterSecret: app.PushAppMasterSecret.Android,
MiActivity: app.PushMiActive.Android,
DeviceToken: deviceToken,
}
case types.DeviceIOS:
pusher = &iOSPusher{
AppKey: app.PushAppKey.IOS,
AppMasterSecret: app.PushAppMasterSecret.IOS,
MiActivity: app.PushMiActive.IOS,
DeviceToken: deviceToken,
}
default:
return nil, errors.New(fmt.Sprintf("checkDeviceToken device : %s type illegal", deviceType))
}
return pusher, nil
}
func (t *androidPusher) singlePush(title, text, targetId string, channelType int) error {
var client push.PushClient
unicast := android_push.NewAndroidUnicast(t.AppKey, t.AppMasterSecret)
fmt.Println(t.AppKey, t.AppMasterSecret, t.DeviceToken, title, text)
unicast.SetDeviceToken(t.DeviceToken)
unicast.SetTitle(title)
unicast.SetText(text)
unicast.GoCustomAfterOpen("")
unicast.SetDisplayType(push.NOTIFICATION)
unicast.SetMipush(true, t.MiActivity)
// 线上模式
unicast.SetReleaseMode()
// Set customized fields
unicast.SetExtraField("targetId", targetId)
unicast.SetExtraField("channelType", utility.ToString(channelType))
return client.Send(unicast)
}
func (t *iOSPusher) singlePush(title, text, targetId string, channelType int) error {
var client push.PushClient
unicast := ios_push.NewIOSUnicast(t.AppKey, t.AppMasterSecret)
fmt.Println(t.AppKey, t.AppMasterSecret, t.DeviceToken, title, text)
unicast.SetDeviceToken(t.DeviceToken)
//unicast.SetAlert("IOS 单播测试")
unicast.SetAlertJson(push.IOSAlert{
Title: title,
Body: text,
})
//unicast.SetBadge(0)
unicast.SetSound("default")
//// 测试模式
//unicast.SetTestMode()
// 线上模式
unicast.SetReleaseMode()
// Set customized fields
unicast.SetCustomizedField("targetId", targetId)
unicast.SetCustomizedField("channelType", utility.ToString(channelType))
return client.Send(unicast)
}
func PushToFriend(appId, userId, friendId, text string, msg *proto.Proto) error {
app := app.GetApp(appId)
if app == nil {
return types.ERR_APPNOTFIND
}
user, err := orm.FindFriendById(friendId, userId)
if err != nil {
return err
}
//如果不是红包消息 需要判断消息免打扰
if msg.GetMsgType() != types.RedPack {
//查询对方是否对你设置了消息免打扰
if user == nil || user.DND == types.NoDisturbingOn {
//消息免打扰 直接推送websocket
return nil
}
}
//如果是焚毁通知 则不推送
if msg.GetMsgType() == types.Alert {
content := msg.GetMsg()
alertType := utility.ToInt(content["type"])
if alertType == types.AlertHadBurntMsg {
return nil
}
}
friend, err := orm.GetUserInfoById(friendId)
if err != nil {
return err
}
//非透传消息 title:自己名称
var userName string
if user != nil {
if user.Remark != "" {
userName = user.Remark
} else {
userName = user.Username
}
}
title := userName
var pusher IPusher
if pusher, err = checkDeviceToken(app, friendId, friend.DeviceToken); pusher == nil {
return err
}
if cfg.Log.Level == "debug" {
b, err := json.Marshal(pusher)
if err != nil {
}
pushLog.Debug("push to room", "friendId", friendId, "userId", userId, "pusher", string(b))
}
return pusher.singlePush(title, text, userId, types.ToUser)
}
func PushToMember(appId, userId, roomId string, members map[string]string, msg *proto.Proto) error {
app := app.GetApp(appId)
if app == nil {
return types.ERR_APPNOTFIND
}
//如果是焚毁通知
if msg.GetMsgType() == types.Alert {
content := msg.GetMsg()
alertType := utility.ToInt(content["type"])
if alertType == types.AlertHadBurntMsg {
return nil
}
}
//筛选群中未设置消息免打扰的 红包消息除外
if msg.GetChannelType() == types.ToRoom {
if msg.GetMsgType() != types.RedPack {
//查找设置消息免打扰的
dndUsers, err := orm.FindSetNoDisturbingMembers(msg.GetTargetId())
if err != nil {
return err
}
//移除设置了消息免打扰的
for _, v := range dndUsers {
key := v.UserId
if _, ok := members[key]; ok {
delete(members, key)
}
}
}
}
var roomName string
info, err := orm.FindRoomById(msg.GetTargetId(), types.RoomNotDeleted)
if err == nil && info != nil {
roomName = info.Name
}
title := roomName
text := msg.GetGTMsg(userId)
for k, v := range members {
var pusher IPusher
if pusher, err = checkDeviceToken(app, k, v); pusher == nil {
if err != nil {
//TODO 输出错误日志
}
continue
}
err = pusher.singlePush(title, text, roomId, types.ToRoom)
}
return nil
}
func PushToRoom(appId, userId, roomId string, msg *proto.Proto) error {
app := app.GetApp(appId)
if app == nil {
return types.ERR_APPNOTFIND
}
//如果是焚毁通知
if msg.GetMsgType() == types.Alert {
content := msg.GetMsg()
alertType := utility.ToInt(content["type"])
if alertType == types.AlertHadBurntMsg {
return nil
}
}
//查询离线成员
var members = make(map[string]string)
users, _ := orm.FindNotDelMembers(roomId, types.SearchAll)
for _, v := range users {
if user, ok := router.GetUser(v.RoomMember.UserId); !ok || len(user.GetClients()) == 0 {
members[v.RoomMember.UserId] = v.DeviceToken
}
}
//筛选群中未设置消息免打扰的 红包消息除外
if msg.GetChannelType() == types.ToRoom {
if msg.GetMsgType() != types.RedPack {
//查找设置消息免打扰的
dndUsers, err := orm.FindSetNoDisturbingMembers(msg.GetTargetId())
if err != nil {
return err
}
//移除设置了消息免打扰的
for _, v := range dndUsers {
key := v.UserId
delete(members, key)
}
}
}
var roomName string
info, err := orm.FindRoomById(msg.GetTargetId(), types.RoomNotDeleted)
if err == nil && info != nil {
roomName = info.Name
}
title := roomName
text := msg.GetGTMsg(userId)
for k, v := range members {
var pusher IPusher
if pusher, err = checkDeviceToken(app, k, v); pusher == nil {
if err != nil {
//TODO 输出错误日志
}
continue
}
if cfg.Log.Level == "debug" {
b, err := json.Marshal(pusher)
if err != nil {
}
pushLog.Debug("push to room", "roomId", roomId, "userId", userId, "pusher", string(b))
}
err = pusher.singlePush(title, text, roomId, types.ToRoom)
if err != nil {
pushLog.Error(err.Error())
}
}
return nil
}