|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:fake_push/fake_push.dart' as fake;
|
|
|
|
//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';
|
|
...
|
...
|
@@ -39,12 +39,12 @@ 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;
|
|
|
|
// 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();
|
|
...
|
...
|
@@ -124,53 +124,53 @@ 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();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// _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() {
|
|
...
|
...
|
@@ -178,31 +178,31 @@ class HomePageState extends State<HomePage>{ |
|
|
|
}
|
|
|
|
|
|
|
|
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}');
|
|
|
|
// String token=await _push.getDeviceToken();
|
|
|
|
// print('receiveDeviceToken: $deviceToken - $token');
|
|
|
|
// userTokenInit2(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
}
|
|
|
|
// 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){
|
|
...
|
...
|
@@ -326,21 +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();
|
|
|
|
}
|
|
|
|
// 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");
|
|
|
|
}
|
...
|
...
|
|