import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:get/get.dart'; import 'package:get/get_core/src/get_main.dart'; import '../data/repositories/track_repository.dart'; import '../handler/error_handler.dart'; import '../module/track/track_dialog/track_choose_friend_dialog.dart'; import '../module/track/track_dialog/track_daily_report_dialog.dart'; class IosPushNotificationService { static const MethodChannel _channel = MethodChannel('com.example.app/push_notification'); // 回调函数 static Function(String)? onTokenReceived; static Function(String)? onTokenError; static Function(Map)? onNotificationReceived; static Function(Map)? onNotificationTapped; // 初始化推送服务 static void initialize() { _channel.setMethodCallHandler((call) async { switch (call.method) { case 'onTokenReceived': onTokenReceived?.call(call.arguments); break; case 'onTokenError': onTokenError?.call(call.arguments); break; case 'onNotificationReceived': if (call.arguments is Map) { onNotificationReceived?.call(Map.from(call.arguments)); } break; case 'onNotificationTapped': if (call.arguments is Map) { onNotificationTapped?.call(Map.from(call.arguments)); } break; default: throw PlatformException( code: 'UNIMPLEMENTED', message: 'Method not implemented', details: null, ); } }); } // 请求推送权限 static Future requestPermission() async { try { final bool? granted = await _channel.invokeMethod('requestPermission'); return granted ?? false; } on PlatformException catch (e) { print('Failed to request permission: ${e.message}'); return false; } } // 获取设备令牌 static Future getDeviceToken() async { try { final String? token = await _channel.invokeMethod('getDeviceToken'); print("Strdefdjifjd---${token}"); return token; } on PlatformException catch (e) { print('Failed to get device token: ${e.message}'); return null; } } ///处理推送各种点击 skipType 0-首页 1-好友申请页面 2-好友消息页面 static void handleNotificationPushChick(Map pushDict) { int skipType = pushDict["skipType"]; TrackRepository trackRepository = TrackRepository.getInstance(); trackRepository.locationTrackDailyDialogs().then((trackResponse) { if ((trackResponse.trackDailyList ?? []).isNotEmpty) { if ((trackResponse.trackDailyList ?? []).length == 1) { } else { TrackChooseFriendDialog.show(trackDailyList: trackResponse.trackDailyList,onSelectItem: (selectInfo) { }); } } }).catchError((error) { ErrorHandler.toastError(error); }); // skipType = 0; // if (skipType == 0) { // Get.until((route) => route.isFirst); // // 在其他页面或组件中 // final mainController = Get.find(); // // 调用方法 // mainController.onTabClick(0); // 切换到第一个Tab // } else if (skipType == 1) { // MessagePage.start(initialIndex: 0); // } else if (skipType == 2) { // MessagePage.start(initialIndex: 1); // } } }