-
Notifications
You must be signed in to change notification settings - Fork 56
/
jpush.dart
257 lines (230 loc) · 7.94 KB
/
jpush.dart
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
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_learn/utils/event.dart';
import 'package:flutter_learn/utils/push.dart';
import 'package:flutter_learn/utils/toast.dart';
class JPushPage extends StatefulWidget {
JPushPage(this.title, {Key key}) : super(key: key);
final String title;
@override
_JPushPageState createState() => _JPushPageState();
}
class _JPushPageState extends State<JPushPage> {
String _message = "";
@override
void initState() {
super.initState();
registerPushEvent();
XPush.getRegistrationID().then((vaule) {
updateMessage("RegistrationID: $vaule");
});
}
void updateMessage(String value) {
setState(() {
_message = value;
});
}
@override
void dispose() {
super.dispose();
unregisterPushEvent();
}
StreamSubscription<Map> _notification;
StreamSubscription<Map> _notificationOpen;
StreamSubscription<Map> _customMessage;
void registerPushEvent() {
_notification = XEvent.on<Map>(EVENT_NOTIFICATION_MESSAGE, (notification) {
XToast.success("收到通知");
updateMessage("收到通知, notification: $notification");
});
_notificationOpen = XEvent.on<Map>(EVENT_NOTIFICATION_OPEN, (notification) {
XToast.success("通知被点击");
updateMessage("通知被点击, notification: $notification");
});
_customMessage = XEvent.on<Map>(EVENT_CUSTOM_MESSAGE, (notification) {
XToast.success("收到自定义消息");
updateMessage("收到自定义消息, notification: $notification");
});
}
void unregisterPushEvent() {
XEvent.cancel(EVENT_NOTIFICATION_MESSAGE, _notification);
XEvent.cancel(EVENT_NOTIFICATION_OPEN, _notificationOpen);
XEvent.cancel(EVENT_NOTIFICATION_OPEN, _customMessage);
}
TextEditingController _aliasController = TextEditingController();
TextEditingController _tagsController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
padding: EdgeInsets.all(10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start, //文本是起始端对齐
children: <Widget>[
Container(
alignment: Alignment.topLeft,
height: 150,
color: Colors.grey,
padding: EdgeInsets.all(10),
child: Text(_message,
style: TextStyle(color: Colors.white, fontSize: 12)),
),
ButtonBar(
alignment:
MainAxisAlignment.start, //布局方向,默认MainAxisAlignment.end
mainAxisSize: MainAxisSize.min, //主轴大小,默认MainAxisSize.max
children: <Widget>[
RaisedButton(
child: Text('恢复推送'),
onPressed: resumePush,
),
RaisedButton(
child: Text('停止推送'),
onPressed: stopPush,
),
],
),
TextField(
controller: _aliasController,
decoration: InputDecoration(
labelStyle: TextStyle(fontSize: 12),
labelText: "别名(alias)",
hintText: "请输入需要绑定的别名",
hintStyle: TextStyle(fontSize: 12)),
),
ButtonBar(
alignment:
MainAxisAlignment.start, //布局方向,默认MainAxisAlignment.end
mainAxisSize: MainAxisSize.min, //主轴大小,默认MainAxisSize.max
children: <Widget>[
RaisedButton(
child: Text('绑定'),
onPressed: setAlias,
),
RaisedButton(
child: Text('解绑'),
onPressed: deleteAlias,
),
],
),
SizedBox(height: 10),
TextField(
controller: _tagsController,
decoration: InputDecoration(
labelStyle: TextStyle(fontSize: 12),
labelText: "标签(tags)",
hintText: "请输入标签, 以英文的\',\'隔开",
hintStyle: TextStyle(fontSize: 12)),
),
ButtonBar(
alignment:
MainAxisAlignment.start, //布局方向,默认MainAxisAlignment.end
mainAxisSize: MainAxisSize.min, //主轴大小,默认MainAxisSize.max
children: <Widget>[
RaisedButton(
child: Text('添加(增量)'),
onPressed: addTags,
),
RaisedButton(
child: Text('删除'),
onPressed: deleteTags,
),
RaisedButton(
child: Text('获取'),
onPressed: getTags,
),
],
),
ButtonBar(
alignment:
MainAxisAlignment.start, //布局方向,默认MainAxisAlignment.end
mainAxisSize: MainAxisSize.min, //主轴大小,默认MainAxisSize.max
children: <Widget>[
RaisedButton(
child: Text('设置(全量)'),
onPressed: setTags,
),
RaisedButton(
child: Text('清除'),
onPressed: cleanTags,
),
],
),
],
)));
}
void resumePush() {
XPush.jpush.resumePush();
}
void stopPush() {
XPush.jpush.stopPush();
}
void setAlias() {
if (_aliasController.text.isEmpty) {
XToast.error("别名不能为空");
return;
}
XPush.setAlias(_aliasController.text).then((value) {
updateMessage("别名绑定结果: $value");
}).catchError((error) {
updateMessage("别名绑定出错: $error");
});
}
void deleteAlias() {
XPush.deleteAlias().then((value) {
updateMessage("别名解绑结果: $value");
}).catchError((error) {
updateMessage("别名解绑出错: $error");
});
}
void addTags() {
if (_tagsController.text.isEmpty) {
XToast.error("标签不能为空");
return;
}
XPush.addTags(_tagsController.text.split(",")).then((value) {
updateMessage("标签添加结果: $value");
}).catchError((error) {
updateMessage("标签添加出错: $error");
});
}
void deleteTags() {
if (_tagsController.text.isEmpty) {
XToast.error("标签不能为空");
return;
}
XPush.deleteTags(_tagsController.text.split(",")).then((value) {
updateMessage("标签删除结果: $value");
}).catchError((error) {
updateMessage("标签删除出错: $error");
});
}
void getTags() {
XPush.jpush.getAllTags().then((value) {
updateMessage("标签获取结果: $value");
}).catchError((error) {
updateMessage("标签获取出错: $error");
});
}
void setTags() {
if (_tagsController.text.isEmpty) {
XToast.error("标签不能为空");
return;
}
XPush.setTags(_tagsController.text.split(",")).then((value) {
updateMessage("标签设置结果: $value");
}).catchError((error) {
updateMessage("标签设置出错: $error");
});
}
void cleanTags() {
XPush.cleanTags().then((value) {
updateMessage("标签清除结果: $value");
}).catchError((error) {
updateMessage("标签清除出错: $error");
});
}
}