import 'dart:convert'; import 'package:flutter/services.dart'; import 'package:flutter/material.dart'; class ReceiveIntentPlugin { static const MethodChannel _methodChannel = MethodChannel('receive_intent'); static const EventChannel _eventChannel = EventChannel('receive_intent/event'); static Map? initialIntentMap; static Map? latestIntentMap; static bool initialIntent = true; static void handleIntent(Map intent, String? fromPackageName) { final intentMap = { 'fromPackageName': fromPackageName, 'fromSignatures': fromPackageName != null ? getApplicationSignature(fromPackageName) : null, 'action': intent['action'], 'data': intent['data'], 'categories': intent['categories'], 'extra': intent['extras'] != null ? bundleToJson(intent['extras']) : null, }; if (initialIntent) { initialIntentMap = intentMap; initialIntent = false; } latestIntentMap = intentMap; _eventChannel.receiveBroadcastStream().add(latestIntentMap); } static Future setResult(int? resultCode, String? data, bool? shouldFinish) async { if (resultCode != null) { final json = data != null ? jsonDecode(data) : null; await _methodChannel.invokeMethod('setResult', { 'resultCode': resultCode, 'data': json, 'shouldFinish': shouldFinish, }); } else { throw ArgumentError('resultCode can not be null'); } } static Future?> getApplicationSignature(String packageName) async { // Implement platform channel to get application signatures. } static Map bundleToJson(Map bundle) { // Convert bundle to JSON-like structure. return bundle; } }