@@ -4,6 +4,7 @@ import UIKit
44@UIApplicationMain
55@objc class AppDelegate : FlutterAppDelegate {
66 private var deviceToken : String ?
7+ private let notificationChannelName : String = " im.phnx.prototype/channel "
78
89 override func application(
910 _ application: UIApplication ,
@@ -20,15 +21,11 @@ import UIKit
2021
2122 // Set up the method channel to retrieve the token from Flutter
2223 let controller = window? . rootViewController as! FlutterViewController
23- let deviceTokenChannel = FlutterMethodChannel ( name: " im.phnx.prototype/channel " ,
24+ let methodChannel = FlutterMethodChannel ( name: notificationChannelName ,
2425 binaryMessenger: controller. binaryMessenger)
25- deviceTokenChannel. setMethodCallHandler { [ weak self] ( call, result) in
26- if call. method == " devicetoken " {
27- self ? . getDeviceToken ( result: result)
28- } else {
29- result ( FlutterMethodNotImplemented)
30- }
31- }
26+
27+ // Set the handler function for the method channel
28+ methodChannel. setMethodCallHandler ( handleMethodCall)
3229
3330 return super. application ( application, didFinishLaunchingWithOptions: launchOptions)
3431 }
@@ -43,9 +40,47 @@ import UIKit
4340 }
4441
4542 override func application( _ application: UIApplication , didFailToRegisterForRemoteNotificationsWithError error: Error ) {
46- print ( " Failed to register: \( error) " )
43+ NSLog ( " Failed to register: \( error) " )
4744 }
4845
46+ // This method will be called when app received push notifications in foreground
47+ override func userNotificationCenter( _ center: UNUserNotificationCenter , willPresent notification: UNNotification , withCompletionHandler completionHandler: @escaping ( UNNotificationPresentationOptions ) -> Void ) {
48+ NSLog ( " Foreground notification received " )
49+ let userInfo = notification. request. content. userInfo
50+ if let customData = userInfo [ " customData " ] as? String {
51+ notifyFlutter ( customData: customData, method: " receivedNotification " )
52+ }
53+ completionHandler ( [ . alert, . sound] )
54+ }
55+
56+ // This method will be called when the user taps on the notification
57+ override func userNotificationCenter( _ center: UNUserNotificationCenter , didReceive response: UNNotificationResponse , withCompletionHandler completionHandler: @escaping ( ) -> Void ) {
58+ NSLog ( " User opened notification " )
59+ let userInfo = response. notification. request. content. userInfo
60+ if let customData = userInfo [ " customData " ] as? String {
61+ notifyFlutter ( customData: customData, method: " openedNotification " )
62+ }
63+ completionHandler ( )
64+ }
65+
66+ private func notifyFlutter( customData: String , method: String ) {
67+ let controller = window? . rootViewController as! FlutterViewController
68+ let channel = FlutterMethodChannel ( name: notificationChannelName, binaryMessenger: controller. binaryMessenger)
69+ let arguments : [ String : String ] = [ " customData " : customData]
70+ channel. invokeMethod ( method, arguments: arguments)
71+ }
72+
73+ // Define the handler function
74+ private func handleMethodCall( call: FlutterMethodCall , result: @escaping FlutterResult ) {
75+ if call. method == " devicetoken " {
76+ self . getDeviceToken ( result: result)
77+ } else {
78+ NSLog ( " Unknown method called: \( call. method) " )
79+ result ( FlutterMethodNotImplemented)
80+ }
81+ }
82+
83+ // Get device token
4984 private func getDeviceToken( result: FlutterResult ) {
5085 if let token = deviceToken {
5186 result ( token)
0 commit comments