124 lines
4.3 KiB
Dart
124 lines
4.3 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:aliyun_push/aliyun_push.dart';
|
|
import 'package:star_lock/network/api_repository.dart';
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
|
|
class XSAliyunPushProvider {
|
|
late AliyunPush _aliyunPush = AliyunPush();
|
|
|
|
Future<void> init(AliyunPush aliyunPush) async {
|
|
_aliyunPush = aliyunPush;
|
|
if (Platform.isAndroid) {
|
|
_aliyunPush.createAndroidChannel('1', '测试通道A', 3, '测试创建通知通道');
|
|
_aliyunPush.setAndroidLogLevel(kAliyunPushLogLevelError);
|
|
}
|
|
_addPushCallback();
|
|
}
|
|
|
|
Future<void> _onNotification(Map<dynamic, dynamic> message) async {
|
|
// print('onNotification: $message');
|
|
}
|
|
|
|
Future<void> _onAndroidNotificationReceivedInApp(
|
|
Map<dynamic, dynamic> message) async {
|
|
// print('onAndroidNotificationReceivedInApp: $message');
|
|
}
|
|
|
|
Future<void> _onMessage(Map<dynamic, dynamic> message) async {
|
|
// print('onMessage: $message');
|
|
}
|
|
|
|
Future<void> _onNotificationOpened(Map<dynamic, dynamic> message) async {
|
|
// print('onNotificationOpened: $message');
|
|
}
|
|
|
|
Future<void> _onNotificationRemoved(Map<dynamic, dynamic> message) async {
|
|
// print('onNotificationRemoved: $message');
|
|
}
|
|
|
|
Future<void> _onAndroidNotificationClickedWithNoAction(
|
|
Map<dynamic, dynamic> message) async {
|
|
// print('onAndroidNotificationClickedWithNoAction: $message');
|
|
}
|
|
|
|
Future<void> _onIOSChannelOpened(Map<dynamic, dynamic> message) async {}
|
|
|
|
Future<void> _onIOSRegisterDeviceTokenSuccess(
|
|
Map<dynamic, dynamic> message) async {
|
|
// Toast.show(msg: 'APNs注册成功, $message');
|
|
}
|
|
|
|
Future<void> _onIOSRegisterDeviceTokenFailed(
|
|
Map<dynamic, dynamic> message) async {
|
|
// Toast.show(msg: '注册APNs失败, errorMsg: $message');
|
|
}
|
|
|
|
_addPushCallback() {
|
|
_aliyunPush.addMessageReceiver(
|
|
onNotification: _onNotification,
|
|
onNotificationOpened: _onNotificationOpened,
|
|
onNotificationRemoved: _onNotificationRemoved,
|
|
onMessage: _onMessage,
|
|
onAndroidNotificationReceivedInApp: _onAndroidNotificationReceivedInApp,
|
|
onAndroidNotificationClickedWithNoAction:
|
|
_onAndroidNotificationClickedWithNoAction,
|
|
onIOSChannelOpened: _onIOSChannelOpened,
|
|
onIOSRegisterDeviceTokenSuccess: _onIOSRegisterDeviceTokenSuccess,
|
|
onIOSRegisterDeviceTokenFailed: _onIOSRegisterDeviceTokenFailed);
|
|
}
|
|
|
|
Future<void> initAliyunPush() async {
|
|
String appKey;
|
|
String appSecret;
|
|
if (Platform.isIOS) {
|
|
appKey = "333904046";
|
|
appSecret = "3eead09a7fc7416cb4082319aa6f48c6";
|
|
} else {
|
|
appKey = "333904040";
|
|
appSecret = "c316965fe0a74fc9a481a5c44a535dc2";
|
|
}
|
|
|
|
_aliyunPush
|
|
.initPush(appKey: appKey, appSecret: appSecret)
|
|
.then((initResult) {
|
|
var code = initResult['code'];
|
|
if (code == kAliyunPushSuccessCode) {
|
|
// Toast.show(msg: "初始化推送成功");
|
|
} else {
|
|
String errorMsg = initResult['errorMsg'];
|
|
// print('初始化推送失败,原因为:$errorMsg');
|
|
// Toast.show(msg: '初始化推送失败, errorMsg: $errorMsg.}');
|
|
}
|
|
});
|
|
|
|
// 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.
|
|
}
|
|
|
|
Future<void> initAliyunThirdPush() async {
|
|
// Platform messages may fail, so we use a try/catch PlatformException.
|
|
// We also handle the message potentially returning null.
|
|
_aliyunPush.initAndroidThirdPush().then((initResult) {
|
|
var code = initResult['code'];
|
|
if (code == kAliyunPushSuccessCode) {
|
|
// Toast.show(msg: "初始化辅助通道成功");
|
|
} else {
|
|
String errorMsg = initResult['errorMsg'];
|
|
// Toast.show(msg: '初始化辅助通道成功, errorMsg: $errorMsg');
|
|
// print("初始化辅助通道失败,原因为:$errorMsg");
|
|
}
|
|
});
|
|
|
|
// 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.
|
|
}
|
|
|
|
void pushBindDeviceID(String deviceID, int deviceType) async {
|
|
var entity = await ApiRepository.to.pushBindAppId(deviceID, deviceType);
|
|
if (entity.errorCode!.codeIsSuccessful) {}
|
|
}
|
|
}
|