12.2 C
London
Friday, April 26, 2024

I am making an attempt to name a operate from Flutter primarily based on an occasion within the ios native, however flutterMethodChannel?.invokeMethod , not referred to as


I am making an attempt to name a operate from Flutter primarily based on an occasion within the ios native, however flutterMethodChannel?.invokeMethod , not referred to as

That is My AppDelegate class ..


import UIKit
import Flutter
import Firebase
import FirebaseMessaging
import UserNotifications


@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    
    var flutterMethodChannel: FlutterMethodChannel?
    let channelName = "notification_channel"
    var notificationData : Any? = nil
    
    override func utility(
        _ utility: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        if #obtainable(iOS 10.0, *) {
            UNUserNotificationCenter.present().delegate = self
        }
        
        configureFlutterEngine()
        GeneratedPluginRegistrant.register(with: self)
        return tremendous.utility(utility, didFinishLaunchingWithOptions: launchOptions)
    }
    
    
    
    override public func utility(_ utility: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                                     fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        
        if utility.applicationState == .energetic {
            showLocalNotification(with: userInfo)
        } else {
            Messaging.messaging().appDidReceiveMessage(userInfo)
            showLocalNotification(with: userInfo)
        }
    }
    
    non-public func showLocalNotification(with userInfo: [AnyHashable : Any]) {
        if let information = userInfo as? [String: Any],
           let notificationID = information["id"] as? String,
           let title = information["title"] as? String,
           let physique = information["body"] as? String
        {
            let content material = UNMutableNotificationContent()
            content material.title =  title
            content material.physique = physique
            content material.sound = UNNotificationSound.default
            content material.userInfo = information
            notificationData = information
            let request = UNNotificationRequest(identifier: notificationID, content material: content material, set off: nil)
            
            UNUserNotificationCenter.present().add(request) { (error) in
                if let error = error {
                    print("Error displaying native notification: (error.localizedDescription)")
                }
            }
            
        }
    }
    
    override public func  userNotificationCenter(_ heart: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("fcm_token Notification Clicked Person Information = (response.notification.request.content material.userInfo)")
        flutterMethodChannel?.invokeMethod("onNotificationClick", arguments: notificationData){ (end result) in
            if let resultString = end result as? String {
                print(resultString)
            }
            completionHandler()
        }
        completionHandler()
    }
    
    override func userNotificationCenter(_ heart: UNUserNotificationCenter,
                                         willPresent: UNNotification,
                                         withCompletionHandler: @escaping (UNNotificationPresentationOptions)->()) {
        withCompletionHandler([.alert, .sound, .badge])
    }
    
    non-public func getNotificationData(end result: FlutterResult){
        end result(notificationData)
    }
    
    @objc func configureFlutterEngine() {
        let flutterViewController = window?.rootViewController as! FlutterViewController
        let flutterEngine = flutterViewController.engine!
        let binaryMessenger = flutterEngine.binaryMessenger
        
        let flutterMethodChannel = FlutterMethodChannel(identify: channelName, binaryMessenger: binaryMessenger)
        flutterMethodChannel.setMethodCallHandler { (name: FlutterMethodCall, end result: @escaping FlutterResult) in
            print("fcm_token Methodology Name Handler")
            if name.methodology == "getNotificationData" {
                print("fcm_token getNotificationData")
                self.getNotificationData(end result: end result)
            } else {
                end result(FlutterMethodNotImplemented)
            }
        }
    }
    
    override func applicationDidBecomeActive(_ utility: UIApplication) {
        print("fcm_token applicationDidBecomeActive")
        flutterMethodChannel?.invokeMethod("onNotificationClick", arguments: "ios string")
    }
    
}

And That is My Flutter Code 

import 'bundle:flutter/providers.dart';
import 'bundle:logger/logger.dart';

class NativeNotificationConnector {
  static ultimate NativeNotificationConnector occasion =
      NativeNotificationConnector._init();
  NativeNotificationConnector._init();
  MethodChannel? _channel;

  void configureChannel() {
    _channel = const MethodChannel('notification_channel');
    _channel?.setMethodCallHandler(methodHandler);
  }

  Future getNotificationData() async {
    attempt {
      Logger().e('fcm_token getNotificationData');
      dynamic information = await _channel?.invokeMethod('getNotificationData');
      Logger().e('fcm_token notification information: $information');
      return await _channel?.invokeMethod('getNotificationData');
    } on PlatformException catch (e) {
      // Deal with platform exceptions (e.g., methodology not discovered)
      Logger().e(e.message);
    }
  }

  // create a onNotificationClick methodology to obtain the notification information from ios
  Future<void> methodHandler(MethodCall name) async {
    ultimate String concept = name.arguments;
    Logger().e('fcm_token notification information: $concept');

    swap (name.methodology) {
      case 'onNotificationClick':
        onNotificationClick(concept);
        break;
      case 'selectNotification' :
        selectNotification(concept);
        break;
      default:
        Logger().e('fcm_token no methodology handler for methodology ${name.methodology}');
    }
  }

  // create onNotificationClick methodology to obtain the notification information from ios
  void onNotificationClick(String information) {
    Logger().e('fcm_token notification information: $information');
  }

  void selectNotification(String information) {
    Logger().e('fcm_token notification information: $information');
  }
}

How To Remedy This Drawback ..

Notice: I Want To Name Flutter Code From Native On Person Faucet Notification , I Dont Want To Use flutter_local_notification plugin ..

I Tried To Reverse the method (Name Native Code From Flutter) , This Labored With out Issues ..

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here