diff --git a/example/ios/Runner/AppDelegate.swift b/example/ios/Runner/AppDelegate.swift index 0f69fc3f4..4fdf2bb72 100644 --- a/example/ios/Runner/AppDelegate.swift +++ b/example/ios/Runner/AppDelegate.swift @@ -1,17 +1,24 @@ import UIKit import Flutter -@UIApplicationMain +@main @objc class AppDelegate: FlutterAppDelegate { - override func application( - _ application: UIApplication, - didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? - ) -> Bool { - GeneratedPluginRegistrant.register(with: self) - return super.application(application, didFinishLaunchingWithOptions: launchOptions) - } - + override func application( + _ application: UIApplication, + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? + ) -> Bool { + GeneratedPluginRegistrant.register(with: self) + return super.application(application, didFinishLaunchingWithOptions: launchOptions) + } + override func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) { NSLog("application:didFailToRegisterForRemoteNotificationsWithError was called with error: %@", error.localizedDescription) } + + /// An example of how you handle push notification if you opt-out from using Ably Pushes by adding `AblyFlutterHandlePushNotifications` equal to `NO` in your project's Info.plist file: + /// + /// (This method is deprecated, use the new one from `UNUserNotificationCenter` as recommented by Apple, it's here for the sake of simplest example) + override func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { + NSLog("Notification received: \(userInfo)") + } } diff --git a/example/ios/Runner/Info.plist b/example/ios/Runner/Info.plist index 02aeae863..ed3e8743b 100644 --- a/example/ios/Runner/Info.plist +++ b/example/ios/Runner/Info.plist @@ -2,6 +2,8 @@ + AblyFlutterHandlePushNotifications + CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleDisplayName diff --git a/ios/Classes/AblyFlutter.m b/ios/Classes/AblyFlutter.m index d02602b20..48ffa41b3 100644 --- a/ios/Classes/AblyFlutter.m +++ b/ios/Classes/AblyFlutter.m @@ -695,7 +695,11 @@ +(void)registerWithRegistrar:(NSObject*)registrar { // initializing method channel for round-trip method calls FlutterMethodChannel *const methodChannel = [FlutterMethodChannel methodChannelWithName:@"io.ably.flutter.plugin" binaryMessenger:[registrar messenger] codec:methodCodec]; - AblyFlutter *const ably = [[AblyFlutter alloc] initWithChannel:methodChannel streamsChannel: streamsChannel registrar:registrar]; + NSNumber *handleAPNs = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"AblyFlutterHandlePushNotifications"]; + AblyFlutter *const ably = [[AblyFlutter alloc] initWithChannel:methodChannel + streamsChannel:streamsChannel + registrar:registrar + handleAPNs:handleAPNs != nil ? [handleAPNs boolValue] : NO]; // registering method channel with registrar [registrar addMethodCallDelegate:ably channel:methodChannel]; @@ -708,7 +712,8 @@ +(void)registerWithRegistrar:(NSObject*)registrar { -(instancetype)initWithChannel:(FlutterMethodChannel *const)channel streamsChannel:(AblyStreamsChannel *const)streamsChannel - registrar:(NSObject*)registrar { + registrar:(NSObject*)registrar + handleAPNs:(BOOL)handleAPNs { self = [super init]; if (!self) { return nil; @@ -716,10 +721,13 @@ -(instancetype)initWithChannel:(FlutterMethodChannel *const)channel _instanceStore = [AblyInstanceStore sharedInstance]; _channel = channel; _streamsChannel = streamsChannel; - UNUserNotificationCenter *const center = UNUserNotificationCenter.currentNotificationCenter; - _pushNotificationEventHandlers = [[PushNotificationEventHandlers alloc] initWithDelegate: center.delegate andMethodChannel: channel]; - center.delegate = _pushNotificationEventHandlers; - + + if (handleAPNs) { + UNUserNotificationCenter *const center = UNUserNotificationCenter.currentNotificationCenter; + _pushNotificationEventHandlers = [[PushNotificationEventHandlers alloc] initWithDelegate: center.delegate andMethodChannel: channel]; + center.delegate = _pushNotificationEventHandlers; + } + _handlers = @{ AblyPlatformMethod_getPlatformVersion: _getPlatformVersion, AblyPlatformMethod_getVersion: _getVersion,