|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:fake_push/fake_push.dart' as fake;
|
|
|
|
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/app.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';
|
|
...
|
...
|
@@ -15,12 +16,12 @@ import 'package:one_key/provider/my_model.dart'; |
|
|
|
import 'package:one_key/provider/notebook_model.dart';
|
|
|
|
import 'package:one_key/provider/user_model.dart';
|
|
|
|
import 'package:one_key/res/colors.dart';
|
|
|
|
import 'package:one_key/route/navigate_service.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';
|
|
|
|
|
|
|
|
class HomePage extends StatefulWidget{
|
|
|
|
|
|
...
|
...
|
@@ -37,6 +38,15 @@ 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>();
|
|
...
|
...
|
@@ -64,6 +74,7 @@ class HomePageState extends State<HomePage>{ |
|
|
|
child: MyPage(),
|
|
|
|
));
|
|
|
|
initJPush();
|
|
|
|
// initXinge();
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_){
|
|
|
|
if(widget.initialIndex==0)return;
|
|
|
|
getUntreatedMessage();
|
|
...
|
...
|
@@ -111,6 +122,88 @@ class HomePageState extends State<HomePage>{ |
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//初始化信鸽推送
|
|
|
|
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();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
bool _isReleaseMode() {
|
|
|
|
return const bool.fromEnvironment('dart.vm.product');
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleReceiveDeviceToken(String deviceToken) async {
|
|
|
|
String token=await _push.getDeviceToken();
|
|
|
|
print('receiveDeviceToken: $deviceToken - $token');
|
|
|
|
userTokenInit2(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//初始化推送token
|
|
|
|
userTokenInit(String registrationId){
|
|
|
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
|
...
|
...
|
@@ -123,6 +216,18 @@ class HomePageState extends State<HomePage>{ |
|
|
|
userModel.userTokenInit(context, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
//初始化推送token
|
|
|
|
userTokenInit2(String token){
|
|
|
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
|
|
|
userModel.token=token;
|
|
|
|
Map<String,String> params={
|
|
|
|
"sid":userModel.user.sellerid,
|
|
|
|
"account":userModel.user.account,
|
|
|
|
"user_token":token
|
|
|
|
};
|
|
|
|
userModel.userTokenInit(context, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取未处理在线预订消息
|
|
|
|
getUntreatedMessage(){
|
|
|
|
UserModel userModel=Provider.of<UserModel>(context,listen: false);
|
|
...
|
...
|
@@ -221,6 +326,21 @@ 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");
|
|
|
|
}
|
...
|
...
|
|