|
1
|
import 'dart:async';
|
1
|
import 'dart:async';
|
|
2
|
import 'dart:convert';
|
2
|
import 'dart:convert';
|
|
3
|
|
3
|
|
|
|
|
4
|
+import 'package:fake_push/fake_push.dart' as fake;
|
|
|
|
5
|
+import 'package:flutter/cupertino.dart';
|
|
4
|
import 'package:flutter/material.dart';
|
6
|
import 'package:flutter/material.dart';
|
|
5
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
7
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
6
|
import 'package:jpush_flutter/jpush_flutter.dart';
|
8
|
import 'package:jpush_flutter/jpush_flutter.dart';
|
|
7
|
-import 'package:one_key/app.dart';
|
|
|
|
8
|
import 'package:one_key/models/message.dart';
|
9
|
import 'package:one_key/models/message.dart';
|
|
9
|
import 'package:one_key/pages/message/message_page.dart';
|
10
|
import 'package:one_key/pages/message/message_page.dart';
|
|
10
|
import 'package:one_key/pages/my/my_page.dart';
|
11
|
import 'package:one_key/pages/my/my_page.dart';
|
|
@@ -15,12 +16,12 @@ import 'package:one_key/provider/my_model.dart'; |
|
@@ -15,12 +16,12 @@ import 'package:one_key/provider/my_model.dart'; |
|
15
|
import 'package:one_key/provider/notebook_model.dart';
|
16
|
import 'package:one_key/provider/notebook_model.dart';
|
|
16
|
import 'package:one_key/provider/user_model.dart';
|
17
|
import 'package:one_key/provider/user_model.dart';
|
|
17
|
import 'package:one_key/res/colors.dart';
|
18
|
import 'package:one_key/res/colors.dart';
|
|
18
|
-import 'package:one_key/route/navigate_service.dart';
|
|
|
|
19
|
import 'package:one_key/utils/log_util.dart';
|
19
|
import 'package:one_key/utils/log_util.dart';
|
|
20
|
import 'package:one_key/utils/navigator_util.dart';
|
20
|
import 'package:one_key/utils/navigator_util.dart';
|
|
21
|
import 'package:one_key/utils/utils.dart';
|
21
|
import 'package:one_key/utils/utils.dart';
|
|
22
|
import 'package:provider/provider.dart';
|
22
|
import 'package:provider/provider.dart';
|
|
23
|
import 'package:flutter/services.dart';
|
23
|
import 'package:flutter/services.dart';
|
|
|
|
24
|
+import 'package:shared_preferences/shared_preferences.dart';
|
|
24
|
|
25
|
|
|
25
|
class HomePage extends StatefulWidget{
|
26
|
class HomePage extends StatefulWidget{
|
|
26
|
|
27
|
|
|
@@ -37,6 +38,15 @@ class HomePage extends StatefulWidget{ |
|
@@ -37,6 +38,15 @@ class HomePage extends StatefulWidget{ |
|
37
|
|
38
|
|
|
38
|
class HomePageState extends State<HomePage>{
|
39
|
class HomePageState extends State<HomePage>{
|
|
39
|
|
40
|
|
|
|
|
41
|
+// 信鸽推送
|
|
|
|
42
|
+ fake.Push _push = fake.Push();
|
|
|
|
43
|
+ StreamSubscription<String> _receiveDeviceToken;
|
|
|
|
44
|
+ StreamSubscription<fake.Message> _receiveMessage;
|
|
|
|
45
|
+ StreamSubscription<fake.Message> _receiveNotification;
|
|
|
|
46
|
+ StreamSubscription<String> _launchNotification;
|
|
|
|
47
|
+ StreamSubscription<String> _resumeNotification;
|
|
|
|
48
|
+
|
|
|
|
49
|
+// 极光推送
|
|
40
|
final JPush jpush = new JPush();
|
50
|
final JPush jpush = new JPush();
|
|
41
|
|
51
|
|
|
42
|
List<Widget> pages = List<Widget>();
|
52
|
List<Widget> pages = List<Widget>();
|
|
@@ -64,6 +74,7 @@ class HomePageState extends State<HomePage>{ |
|
@@ -64,6 +74,7 @@ class HomePageState extends State<HomePage>{ |
|
64
|
child: MyPage(),
|
74
|
child: MyPage(),
|
|
65
|
));
|
75
|
));
|
|
66
|
initJPush();
|
76
|
initJPush();
|
|
|
|
77
|
+// initXinge();
|
|
67
|
WidgetsBinding.instance.addPostFrameCallback((_){
|
78
|
WidgetsBinding.instance.addPostFrameCallback((_){
|
|
68
|
if(widget.initialIndex==0)return;
|
79
|
if(widget.initialIndex==0)return;
|
|
69
|
getUntreatedMessage();
|
80
|
getUntreatedMessage();
|
|
@@ -111,6 +122,88 @@ class HomePageState extends State<HomePage>{ |
|
@@ -111,6 +122,88 @@ class HomePageState extends State<HomePage>{ |
|
111
|
});
|
122
|
});
|
|
112
|
}
|
123
|
}
|
|
113
|
|
124
|
|
|
|
|
125
|
+ //初始化信鸽推送
|
|
|
|
126
|
+ void initXinge(){
|
|
|
|
127
|
+ _receiveDeviceToken =
|
|
|
|
128
|
+ _push.receiveDeviceToken().listen(_handleReceiveDeviceToken);
|
|
|
|
129
|
+ _receiveMessage = _push.receiveMessage().listen(_handleReceiveMessage);
|
|
|
|
130
|
+ _receiveNotification =
|
|
|
|
131
|
+ _push.receiveNotification().listen(_handleReceiveNotification);
|
|
|
|
132
|
+ _launchNotification =
|
|
|
|
133
|
+ _push.launchNotification().listen(_handleLaunchNotification);
|
|
|
|
134
|
+ _resumeNotification =
|
|
|
|
135
|
+ _push.resumeNotification().listen(_handleResumeNotification);
|
|
|
|
136
|
+
|
|
|
|
137
|
+ _push.startWork(enableDebug: !_isReleaseMode());
|
|
|
|
138
|
+
|
|
|
|
139
|
+ // 第二次打开应用再调用
|
|
|
|
140
|
+ SharedPreferences.getInstance().then((SharedPreferences sharedPref) {
|
|
|
|
141
|
+ if (sharedPref.getBool('firstStart') ?? true) {
|
|
|
|
142
|
+ sharedPref.setBool('firstStart', false);
|
|
|
|
143
|
+ } else {
|
|
|
|
144
|
+ _push.areNotificationsEnabled().then((bool isEnabled) {
|
|
|
|
145
|
+ if (!isEnabled) {
|
|
|
|
146
|
+ showCupertinoDialog(
|
|
|
|
147
|
+ context: context,
|
|
|
|
148
|
+ builder: (BuildContext context) {
|
|
|
|
149
|
+ return CupertinoAlertDialog(
|
|
|
|
150
|
+ title: const Text('提示'),
|
|
|
|
151
|
+ content: const Text('开启通知权限可收到更多优质内容'),
|
|
|
|
152
|
+ actions: <Widget>[
|
|
|
|
153
|
+ CupertinoDialogAction(
|
|
|
|
154
|
+ child: const Text('取消'),
|
|
|
|
155
|
+ onPressed: () {
|
|
|
|
156
|
+ Navigator.of(context).pop();
|
|
|
|
157
|
+ },
|
|
|
|
158
|
+ ),
|
|
|
|
159
|
+ CupertinoDialogAction(
|
|
|
|
160
|
+ child: const Text('设置'),
|
|
|
|
161
|
+ onPressed: () {
|
|
|
|
162
|
+ _push.openNotificationsSettings();
|
|
|
|
163
|
+ Navigator.of(context).pop();
|
|
|
|
164
|
+ },
|
|
|
|
165
|
+ ),
|
|
|
|
166
|
+ ],
|
|
|
|
167
|
+ );
|
|
|
|
168
|
+ },
|
|
|
|
169
|
+ );
|
|
|
|
170
|
+ }
|
|
|
|
171
|
+ });
|
|
|
|
172
|
+ }
|
|
|
|
173
|
+ });
|
|
|
|
174
|
+ }
|
|
|
|
175
|
+
|
|
|
|
176
|
+ bool _isReleaseMode() {
|
|
|
|
177
|
+ return const bool.fromEnvironment('dart.vm.product');
|
|
|
|
178
|
+ }
|
|
|
|
179
|
+
|
|
|
|
180
|
+ void _handleReceiveDeviceToken(String deviceToken) async {
|
|
|
|
181
|
+ String token=await _push.getDeviceToken();
|
|
|
|
182
|
+ print('receiveDeviceToken: $deviceToken - $token');
|
|
|
|
183
|
+ userTokenInit2(token);
|
|
|
|
184
|
+ }
|
|
|
|
185
|
+
|
|
|
|
186
|
+ void _handleReceiveMessage(fake.Message message) {
|
|
|
|
187
|
+ print(
|
|
|
|
188
|
+ 'receiveMessage: ${message.title} - ${message.content} - ${message.customContent}');
|
|
|
|
189
|
+ }
|
|
|
|
190
|
+
|
|
|
|
191
|
+ void _handleReceiveNotification(fake.Message notification) {
|
|
|
|
192
|
+ print(
|
|
|
|
193
|
+ 'receiveNotification: ${notification.title} - ${notification.content} - ${notification.customContent}');
|
|
|
|
194
|
+
|
|
|
|
195
|
+ }
|
|
|
|
196
|
+
|
|
|
|
197
|
+ void _handleLaunchNotification(String customContent) {
|
|
|
|
198
|
+ print('launchNotification: $customContent');
|
|
|
|
199
|
+
|
|
|
|
200
|
+ }
|
|
|
|
201
|
+
|
|
|
|
202
|
+ void _handleResumeNotification(String customContent) {
|
|
|
|
203
|
+ print('resumeNotification: $customContent');
|
|
|
|
204
|
+
|
|
|
|
205
|
+ }
|
|
|
|
206
|
+
|
|
114
|
//初始化推送token
|
207
|
//初始化推送token
|
|
115
|
userTokenInit(String registrationId){
|
208
|
userTokenInit(String registrationId){
|
|
116
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
209
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
|
@@ -123,6 +216,18 @@ class HomePageState extends State<HomePage>{ |
|
@@ -123,6 +216,18 @@ class HomePageState extends State<HomePage>{ |
|
123
|
userModel.userTokenInit(context, params);
|
216
|
userModel.userTokenInit(context, params);
|
|
124
|
}
|
217
|
}
|
|
125
|
|
218
|
|
|
|
|
219
|
+ //初始化推送token
|
|
|
|
220
|
+ userTokenInit2(String token){
|
|
|
|
221
|
+ UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
|
|
|
222
|
+ userModel.token=token;
|
|
|
|
223
|
+ Map<String,String> params={
|
|
|
|
224
|
+ "sid":userModel.user.sellerid,
|
|
|
|
225
|
+ "account":userModel.user.account,
|
|
|
|
226
|
+ "user_token":token
|
|
|
|
227
|
+ };
|
|
|
|
228
|
+ userModel.userTokenInit(context, params);
|
|
|
|
229
|
+ }
|
|
|
|
230
|
+
|
|
126
|
//获取未处理在线预订消息
|
231
|
//获取未处理在线预订消息
|
|
127
|
getUntreatedMessage(){
|
232
|
getUntreatedMessage(){
|
|
128
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
233
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
|
@@ -221,6 +326,21 @@ class HomePageState extends State<HomePage>{ |
|
@@ -221,6 +326,21 @@ class HomePageState extends State<HomePage>{ |
|
221
|
@override
|
326
|
@override
|
|
222
|
void dispose() {
|
327
|
void dispose() {
|
|
223
|
// TODO: implement dispose
|
328
|
// TODO: implement dispose
|
|
|
|
329
|
+ if (_receiveDeviceToken != null) {
|
|
|
|
330
|
+ _receiveDeviceToken.cancel();
|
|
|
|
331
|
+ }
|
|
|
|
332
|
+ if (_receiveMessage != null) {
|
|
|
|
333
|
+ _receiveMessage.cancel();
|
|
|
|
334
|
+ }
|
|
|
|
335
|
+ if (_receiveNotification != null) {
|
|
|
|
336
|
+ _receiveNotification.cancel();
|
|
|
|
337
|
+ }
|
|
|
|
338
|
+ if (_launchNotification != null) {
|
|
|
|
339
|
+ _launchNotification.cancel();
|
|
|
|
340
|
+ }
|
|
|
|
341
|
+ if (_resumeNotification != null) {
|
|
|
|
342
|
+ _resumeNotification.cancel();
|
|
|
|
343
|
+ }
|
|
224
|
super.dispose();
|
344
|
super.dispose();
|
|
225
|
LogUtil.v("home_dispose");
|
345
|
LogUtil.v("home_dispose");
|
|
226
|
}
|
346
|
}
|