Flutter Plugin iOS Method Channel

ENGAGE® Flutter Plugin iOS Method Channel

In iOS AppDelegate.swift contains the functionality to establish the connection with the Flutter and ENGAGE SDK through the Flutter Method Channel. Register the plugin method channel in AppDelegate so that it can communicate with flutter engage plugin and pass the addition data to plugin so that it can handle iOS functionality.


import UIKit
import Flutter
import EngageKit
 
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    
    private var deviceTokenMethodChannel: FlutterMethodChannel?
    private var pushDataMethodChannel: FlutterMethodChannel?
    
    override func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
        
				//Set ENGAGE Logging Level
				Engage.logVerbosity = .minimal
        
        GeneratedPluginRegistrant.register(with: self)
        // Set up the method channel
        if let controller = window?.rootViewController as? FlutterViewController {
            deviceTokenMethodChannel = FlutterMethodChannel(
                name: "ios_device_token_channel",
                binaryMessenger: controller.binaryMessenger
            )
            pushDataMethodChannel = FlutterMethodChannel(
                name: "push_data_channel",
                binaryMessenger: controller.binaryMessenger
            )
        }
        UIApplication.shared.registerForRemoteNotifications()
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
    
    override func application(
        _ application: UIApplication,
        didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
    ) {
        let tokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
        deviceTokenMethodChannel?.invokeMethod("providePushTokenToSDK", arguments: tokenString)
    }
 
    override func application(
        _ application: UIApplication,
        didReceiveRemoteNotification userInfo: [AnyHashable : Any],
        fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void
    ) {
        pushDataMethodChannel?.invokeMethod("handlePushNotification", arguments: userInfo)
    }
    
    override func application(
        _ application: UIApplication,
        didReceiveRemoteNotification userInfo: [AnyHashable : Any]
    ) {
        pushDataMethodChannel?.invokeMethod("handlePushNotification", arguments: userInfo)
    }
}


  • Private Method Channel Variables: These variables hold references to Flutter method channels for communication between native iOS code and Flutter Dart code.
  • application (_:didFinishLaunchingWithOptions:): This method is overridden to perform setup tasks when the application finishes launching. It includes registering Flutter plugins and setting up method channels.
  • Setting up Method Channels: Method channels are created with specific names to facilitate communication between Flutter and native iOS code. One channel is for passing device tokens (ios_device_token_channel) and the other is for handling push notification data (push_data_channel).
  • Registering for Remote Notifications: The application is registered for remote notifications, enabling it to receive push notifications from a server.
  • application (_:didRegisterForRemoteNotificationsWithDeviceToken:): This method is called when the device successfully registers for remote notifications. It converts the device token to a string and invokes a Flutter method to provide the token to the SDK.
  • application (_:didReceiveRemoteNotification:fetchCompletionHandler:): This method is called when the device receives a remote notification and the app is in the foreground or background. It invokes a Flutter method to handle the received push notification data.
  • application(_:didReceiveRemoteNotification:): This method is called when the device receives a remote notification and the app is in the foreground. It also invokes a Flutter method to handle the received push notification data.