|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
//import 'package:fake_push/fake_push.dart' as fake;
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
|
import 'package:jpush_flutter/jpush_flutter.dart';
|
|
|
|
import 'package:one_key/models/message.dart';
|
|
|
|
import 'package:one_key/pages/message/message_page.dart';
|
|
|
|
import 'package:one_key/pages/my/my_page.dart';
|
|
|
|
import 'package:one_key/pages/notebook/notebook_page.dart';
|
|
...
|
...
|
@@ -19,11 +15,10 @@ import 'package:one_key/provider/update_model.dart'; |
|
|
|
import 'package:one_key/provider/user_model.dart';
|
|
|
|
import 'package:one_key/res/colors.dart';
|
|
|
|
import 'package:one_key/utils/log_util.dart';
|
|
|
|
import 'package:one_key/utils/navigator_util.dart';
|
|
|
|
import 'package:one_key/utils/utils.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:tpns_flutter_plugin/tpns_flutter_plugin.dart';
|
|
|
|
|
|
|
|
class HomePage extends StatefulWidget{
|
|
|
|
|
|
...
|
...
|
@@ -40,22 +35,14 @@ class HomePage extends StatefulWidget{ |
|
|
|
|
|
|
|
class HomePageState extends State<HomePage>{
|
|
|
|
|
|
|
|
// 信鸽推送
|
|
|
|
// fake.Push _push = fake.Push();
|
|
|
|
// StreamSubscription<String> _receiveDeviceToken;
|
|
|
|
// StreamSubscription<fake.Message> _receiveMessage;
|
|
|
|
// StreamSubscription<fake.Message> _receiveNotification;
|
|
|
|
// StreamSubscription<String> _launchNotification;
|
|
|
|
// StreamSubscription<String> _resumeNotification;
|
|
|
|
|
|
|
|
// 极光推送
|
|
|
|
final JPush jpush = new JPush();
|
|
|
|
|
|
|
|
List<Widget> pages = List<Widget>();
|
|
|
|
int _currIndex = 1;
|
|
|
|
Color _activeColor = MyColours.color_2bb5b7;
|
|
|
|
Color _defaultColor = MyColours.color_bfbfbf;
|
|
|
|
|
|
|
|
Timer timer;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
// TODO: implement initState
|
|
...
|
...
|
@@ -75,155 +62,92 @@ class HomePageState extends State<HomePage>{ |
|
|
|
create: (_)=>MyModel(),
|
|
|
|
child: MyPage(),
|
|
|
|
));
|
|
|
|
initJPush();
|
|
|
|
// initXinge();
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_){
|
|
|
|
if(widget.initialIndex==0)return;
|
|
|
|
ContactModel.getInstance().uploadLocalContact(context);
|
|
|
|
// getUntreatedMessage();
|
|
|
|
// UpdateModel updateModel=new UpdateModel();
|
|
|
|
// updateModel.checkUpdate(context);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Future<void> initJPush() async {
|
|
|
|
try {
|
|
|
|
jpush.addEventHandler(
|
|
|
|
onReceiveNotification: (Map<String, dynamic> message) async {
|
|
|
|
LogUtil.v("flutter onReceiveNotification: $message");
|
|
|
|
Map<String,dynamic> messageMap=(json.decode(message["extras"]["cn.jpush.android.EXTRA"]) as Map<String,dynamic>)["extras"];
|
|
|
|
Provider.of<MessageDialogModel>(context,listen: false).addMessage(context,Message.fromJson(messageMap));
|
|
|
|
}, onOpenNotification: (Map<String, dynamic> message) async {
|
|
|
|
LogUtil.v("flutter onOpenNotification: $message");
|
|
|
|
NavigatorUtil.goHomePage(context,initialIndex: 0);
|
|
|
|
}, onReceiveMessage: (Map<String, dynamic> message) async {
|
|
|
|
LogUtil.v("flutter onReceiveMessage: $message");
|
|
|
|
}, onReceiveNotificationAuthorization:
|
|
|
|
(Map<String, dynamic> message) async {
|
|
|
|
LogUtil.v("flutter onReceiveNotificationAuthorization: $message");
|
|
|
|
getUntreatedMessage();
|
|
|
|
timer=Timer.periodic(Duration(minutes: 3), (_){
|
|
|
|
getUntreatedMessage();
|
|
|
|
});
|
|
|
|
} on PlatformException {}
|
|
|
|
|
|
|
|
jpush.setup(
|
|
|
|
appKey: "27ceb79c44af41ad99202f7a", //你自己应用的 AppKey
|
|
|
|
channel: "theChannel",
|
|
|
|
production: false,
|
|
|
|
debug: true,
|
|
|
|
);
|
|
|
|
|
|
|
|
jpush.applyPushAuthority(
|
|
|
|
new NotificationSettingsIOS(sound: true, alert: true, badge: false));
|
|
|
|
|
|
|
|
// Platform messages may fail, so we use a try/catch PlatformException.
|
|
|
|
jpush.getRegistrationID().then((rid) {
|
|
|
|
LogUtil.v("flutter get registration id : $rid");
|
|
|
|
userTokenInit(rid);
|
|
|
|
});
|
|
|
|
jpush.isNotificationEnabled().then((bool value) {
|
|
|
|
if (!value) {
|
|
|
|
//没有打开通知
|
|
|
|
jpush.openSettingsForNotification();
|
|
|
|
}
|
|
|
|
UpdateModel updateModel=new UpdateModel();
|
|
|
|
updateModel.checkUpdate(context);
|
|
|
|
});
|
|
|
|
initPlatformState();
|
|
|
|
}
|
|
|
|
|
|
|
|
//初始化信鸽推送
|
|
|
|
void initXinge(){
|
|
|
|
// _receiveDeviceToken =
|
|
|
|
// _push.receiveDeviceToken().listen(_handleReceiveDeviceToken);
|
|
|
|
// _receiveMessage = _push.receiveMessage().listen(_handleReceiveMessage);
|
|
|
|
// _receiveNotification =
|
|
|
|
// _push.receiveNotification().listen(_handleReceiveNotification);
|
|
|
|
// _launchNotification =
|
|
|
|
// _push.launchNotification().listen(_handleLaunchNotification);
|
|
|
|
// _resumeNotification =
|
|
|
|
// _push.resumeNotification().listen(_handleResumeNotification);
|
|
|
|
//
|
|
|
|
// _push.startWork(enableDebug: !_isReleaseMode());
|
|
|
|
//
|
|
|
|
// // 第二次打开应用再调用
|
|
|
|
// SharedPreferences.getInstance().then((SharedPreferences sharedPref) {
|
|
|
|
// if (sharedPref.getBool('firstStart') ?? true) {
|
|
|
|
// sharedPref.setBool('firstStart', false);
|
|
|
|
// } else {
|
|
|
|
// _push.areNotificationsEnabled().then((bool isEnabled) {
|
|
|
|
// if (!isEnabled) {
|
|
|
|
// showCupertinoDialog(
|
|
|
|
// context: context,
|
|
|
|
// builder: (BuildContext context) {
|
|
|
|
// return CupertinoAlertDialog(
|
|
|
|
// title: const Text('提示'),
|
|
|
|
// content: const Text('开启通知权限可收到更多优质内容'),
|
|
|
|
// actions: <Widget>[
|
|
|
|
// CupertinoDialogAction(
|
|
|
|
// child: const Text('取消'),
|
|
|
|
// onPressed: () {
|
|
|
|
// Navigator.of(context).pop();
|
|
|
|
// },
|
|
|
|
// ),
|
|
|
|
// CupertinoDialogAction(
|
|
|
|
// child: const Text('设置'),
|
|
|
|
// onPressed: () {
|
|
|
|
// _push.openNotificationsSettings();
|
|
|
|
// Navigator.of(context).pop();
|
|
|
|
// },
|
|
|
|
// ),
|
|
|
|
// ],
|
|
|
|
// );
|
|
|
|
// },
|
|
|
|
// );
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
Future<void> initPlatformState() async {
|
|
|
|
// If the widget was removed from the tree while the asynchronous platform
|
|
|
|
// message was in flight, we want to discard the reply rather than calling
|
|
|
|
// setState to update our non-existent appearance.
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
|
|
bool _isReleaseMode() {
|
|
|
|
return const bool.fromEnvironment('dart.vm.product');
|
|
|
|
}
|
|
|
|
/// 开启DEBUG
|
|
|
|
XgFlutterPlugin().setEnableDebug(true);
|
|
|
|
|
|
|
|
void _handleReceiveDeviceToken(String deviceToken) async {
|
|
|
|
// String token=await _push.getDeviceToken();
|
|
|
|
// print('receiveDeviceToken: $deviceToken - $token');
|
|
|
|
// userTokenInit2(token);
|
|
|
|
}
|
|
|
|
/// 添加回调事件
|
|
|
|
XgFlutterPlugin().addEventHandler(
|
|
|
|
onRegisteredDeviceToken: (String msg) async {
|
|
|
|
print("flutter onRegisteredDeviceToken: $msg");
|
|
|
|
},
|
|
|
|
onRegisteredDone: (String msg) async {
|
|
|
|
print("flutter onRegisteredDone: $msg");
|
|
|
|
LogUtil.v("注册成功");
|
|
|
|
userTokenInit(msg);
|
|
|
|
},
|
|
|
|
unRegistered: (String msg) async {
|
|
|
|
print("flutter unRegistered: $msg");
|
|
|
|
},
|
|
|
|
onReceiveNotificationResponse: (Map<String, dynamic> msg) async {
|
|
|
|
LogUtil.v("flutter onReceiveNotificationResponse $msg");
|
|
|
|
},
|
|
|
|
onReceiveMessage: (Map<String, dynamic> msg) async {
|
|
|
|
print("flutter onReceiveMessage $msg");
|
|
|
|
},
|
|
|
|
xgPushDidSetBadge: (String msg) async {
|
|
|
|
print("flutter xgPushDidSetBadge: $msg");
|
|
|
|
|
|
|
|
// void _handleReceiveMessage(fake.Message message) {
|
|
|
|
// print(
|
|
|
|
// 'receiveMessage: ${message.title} - ${message.content} - ${message.customContent}');
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// void _handleReceiveNotification(fake.Message notification) {
|
|
|
|
// print(
|
|
|
|
// 'receiveNotification: ${notification.title} - ${notification.content} - ${notification.customContent}');
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// void _handleLaunchNotification(String customContent) {
|
|
|
|
// print('launchNotification: $customContent');
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// void _handleResumeNotification(String customContent) {
|
|
|
|
// print('resumeNotification: $customContent');
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
/// 在此可设置应用角标
|
|
|
|
/// XgFlutterPlugin().setAppBadge(0);;
|
|
|
|
},
|
|
|
|
xgPushDidBindWithIdentifier: (String msg) async {
|
|
|
|
print("flutter xgPushDidBindWithIdentifier: $msg");
|
|
|
|
},
|
|
|
|
xgPushDidUnbindWithIdentifier: (String msg) async {
|
|
|
|
print("flutter xgPushDidUnbindWithIdentifier: $msg");
|
|
|
|
},
|
|
|
|
xgPushDidUpdatedBindedIdentifier: (String msg) async {
|
|
|
|
print("flutter xgPushDidUpdatedBindedIdentifier: $msg");
|
|
|
|
},
|
|
|
|
xgPushDidClearAllIdentifiers: (String msg) async {
|
|
|
|
print("flutter xgPushDidClearAllIdentifiers: $msg");
|
|
|
|
},
|
|
|
|
xgPushClickAction: (Map<String, dynamic> msg) async {
|
|
|
|
print("flutter xgPushClickAction $msg");
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
//初始化推送token
|
|
|
|
userTokenInit(String registrationId){
|
|
|
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
|
|
|
userModel.registrationId=registrationId;
|
|
|
|
Map<String,String> params={
|
|
|
|
"sid":userModel.user.sellerid,
|
|
|
|
"account":userModel.user.account,
|
|
|
|
"registrationId":registrationId
|
|
|
|
};
|
|
|
|
userModel.userTokenInit(context, params);
|
|
|
|
/// 如果您的应用非广州集群则需要在startXG之前调用此函数
|
|
|
|
/// 香港:tpns.hk.tencent.com
|
|
|
|
/// 新加坡:tpns.sgp.tencent.com
|
|
|
|
/// 上海:tpns.sh.tencent.com
|
|
|
|
// XgFlutterPlugin().configureClusterDomainName("tpns.hk.tencent.com");
|
|
|
|
|
|
|
|
/// 设置OPPO AppKey 和 AppSecret
|
|
|
|
XgFlutterPlugin().getXgAndroidApi().enableOppoNotification();
|
|
|
|
XgFlutterPlugin().getXgAndroidApi().setOppoPushAppId(appId:"30260281");
|
|
|
|
XgFlutterPlugin().getXgAndroidApi().setOppoPushAppKey(appKey: "9b7e372f93794593953f93f118619570");
|
|
|
|
// 设置小米 AppID 和 AppKey。
|
|
|
|
XgFlutterPlugin().getXgAndroidApi().setMiPushAppId(appId:"2882303761518639456");
|
|
|
|
XgFlutterPlugin().getXgAndroidApi().setMiPushAppKey(appKey: "5231863961456");
|
|
|
|
XgFlutterPlugin().getXgAndroidApi().enableOtherPush();
|
|
|
|
/// 启动TPNS服务
|
|
|
|
XgFlutterPlugin().startXg("1600012876", "INUIMFK21WGH");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//初始化推送token
|
|
|
|
userTokenInit2(String token){
|
|
|
|
userTokenInit(String token){
|
|
|
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
|
|
|
userModel.token=token;
|
|
|
|
Map<String,String> params={
|
|
...
|
...
|
@@ -239,8 +163,8 @@ class HomePageState extends State<HomePage>{ |
|
|
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
|
|
|
Map<String,String> params={
|
|
|
|
"sid":userModel.user.sellerid,
|
|
|
|
"type_id":"10",
|
|
|
|
"status":"0",
|
|
|
|
"type_id":"10",//10 在线预订
|
|
|
|
"status":"0",//未处理
|
|
|
|
"offset":"0",
|
|
|
|
"limit":"100",
|
|
|
|
};
|
|
...
|
...
|
@@ -332,22 +256,11 @@ class HomePageState extends State<HomePage>{ |
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
// TODO: implement dispose
|
|
|
|
// if (_receiveDeviceToken != null) {
|
|
|
|
// _receiveDeviceToken.cancel();
|
|
|
|
// }
|
|
|
|
// if (_receiveMessage != null) {
|
|
|
|
// _receiveMessage.cancel();
|
|
|
|
// }
|
|
|
|
// if (_receiveNotification != null) {
|
|
|
|
// _receiveNotification.cancel();
|
|
|
|
// }
|
|
|
|
// if (_launchNotification != null) {
|
|
|
|
// _launchNotification.cancel();
|
|
|
|
// }
|
|
|
|
// if (_resumeNotification != null) {
|
|
|
|
// _resumeNotification.cancel();
|
|
|
|
// }
|
|
|
|
super.dispose();
|
|
|
|
LogUtil.v("home_dispose");
|
|
|
|
if(timer!=null){
|
|
|
|
timer.cancel();
|
|
|
|
}
|
|
|
|
XgFlutterPlugin().stopXg();
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|