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 init(AliyunPush aliyunPush) async { _aliyunPush = aliyunPush; if (Platform.isAndroid) { _aliyunPush.createAndroidChannel('1', '测试通道A', 3, '测试创建通知通道'); _aliyunPush.setAndroidLogLevel(kAliyunPushLogLevelError); } _addPushCallback(); } Future _onNotification(Map message) async { // print('onNotification: $message'); } Future _onAndroidNotificationReceivedInApp( Map message) async { // print('onAndroidNotificationReceivedInApp: $message'); } Future _onMessage(Map message) async { // print('onMessage: $message'); } Future _onNotificationOpened(Map message) async { // print('onNotificationOpened: $message'); } Future _onNotificationRemoved(Map message) async { // print('onNotificationRemoved: $message'); } Future _onAndroidNotificationClickedWithNoAction( Map message) async { // print('onAndroidNotificationClickedWithNoAction: $message'); } Future _onIOSChannelOpened(Map message) async {} Future _onIOSRegisterDeviceTokenSuccess( Map message) async { // Toast.show(msg: 'APNs注册成功, $message'); } Future _onIOSRegisterDeviceTokenFailed( Map 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 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 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) {} } }