diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h index dd6489c6f6..17a18a7353 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h @@ -53,20 +53,28 @@ FLUTTER_DARWIN_EXPORT /** * Calls all plugins registered for `UIApplicationDelegate` callbacks. + * + * At runtime this is also mapped to + * `application:didRegisterForRemoteNotificationsWithDeviceToken:`. */ -- (void)application:(UIApplication*)application +- (void)performApplication:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken; /** * Calls all plugins registered for `UIApplicationDelegate` callbacks. + * At runtime this is also mapped to + * `application:didFailToRegisterForRemoteNotificationsWithError:`. */ -- (void)application:(UIApplication*)application +- (void)performApplication:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error; /** * Calls all plugins registered for `UIApplicationDelegate` callbacks. + * + * At runtime this is also mapped to + * `application:didReceiveRemoteNotification:fetchCompletionHandler:`. */ -- (void)application:(UIApplication*)application +- (void)performApplication:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm index a50924e166..bd2eafe49a 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm @@ -15,6 +15,12 @@ static NSString* const kUIBackgroundMode = @"UIBackgroundModes"; static NSString* const kRemoteNotificationCapabitiliy = @"remote-notification"; static NSString* const kBackgroundFetchCapatibility = @"fetch"; static NSString* const kRestorationStateAppModificationKey = @"mod-date"; +static NSString* const kApplicationDidReceiveRemoteNotificationFetchCompletionHandler = + @"application:didReceiveRemoteNotification:fetchCompletionHandler:"; + +static SEL ApplicationDidReceiveRemoteNotificationFetchCompletionHandlerSelector() { + return NSSelectorFromString(kApplicationDidReceiveRemoteNotificationFetchCompletionHandler); +} @interface FlutterAppDelegate () @property(nonatomic, copy) FlutterViewController* (^rootFlutterViewControllerGetter)(void); @@ -90,18 +96,6 @@ static NSString* const kRestorationStateAppModificationKey = @"mod-date"; } #pragma GCC diagnostic pop -- (void)application:(UIApplication*)application - didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { - [_lifeCycleDelegate application:application - didRegisterForRemoteNotificationsWithDeviceToken:deviceToken]; -} - -- (void)application:(UIApplication*)application - didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { - [_lifeCycleDelegate application:application - didFailToRegisterForRemoteNotificationsWithError:error]; -} - #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" - (void)application:(UIApplication*)application @@ -296,7 +290,7 @@ static NSString* const kRestorationStateAppModificationKey = @"mod-date"; NSArray* backgroundModesArray = [[NSBundle mainBundle] objectForInfoDictionaryKey:kUIBackgroundMode]; NSSet* backgroundModesSet = [[[NSSet alloc] initWithArray:backgroundModesArray] autorelease]; - if (selector == @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)) { + if (selector == ApplicationDidReceiveRemoteNotificationFetchCompletionHandlerSelector()) { if (![backgroundModesSet containsObject:kRemoteNotificationCapabitiliy]) { NSLog( @"You've implemented -[ " diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm index 33b4e423ce..4c37499e98 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm @@ -13,6 +13,16 @@ FLUTTER_ASSERT_ARC +@interface FlutterAppDelegate () +- (void)application:(UIApplication*)application + didReceiveRemoteNotification:(NSDictionary*)userInfo + fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler; +- (void)application:(UIApplication*)application + didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken; +- (void)application:(UIApplication*)application + didFailToRegisterForRemoteNotificationsWithError:(NSError*)error; +@end + @interface FlutterAppDelegateTest : XCTestCase @property(strong) FlutterAppDelegate* appDelegate; @@ -155,4 +165,59 @@ FLUTTER_ASSERT_ARC arguments:@"/custom/route?query=test"]); } +- (void)testDidReceiveRemoteNotificationTrue { + id delegate = OCMProtocolMock(@protocol(FlutterApplicationLifeCycleDelegate)); + [self.appDelegate addApplicationLifeCycleDelegate:delegate]; + XCTAssertTrue([self.appDelegate + respondsToSelector:@selector(application: + didReceiveRemoteNotification:fetchCompletionHandler:)]); + NSDictionary* info = @{}; + void (^handler)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result) { + }; + [self.appDelegate application:[UIApplication sharedApplication] + didReceiveRemoteNotification:info + fetchCompletionHandler:handler]; + [(NSObject*)[delegate verify] + application:[UIApplication sharedApplication] + didReceiveRemoteNotification:info + fetchCompletionHandler:handler]; +} + +- (void)testDidRegisterForRemoteNotificationsWithDeviceTokenTrue { + id delegate = OCMProtocolMock(@protocol(FlutterApplicationLifeCycleDelegate)); + [self.appDelegate addApplicationLifeCycleDelegate:delegate]; + XCTAssertTrue([self.appDelegate + respondsToSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]); + NSData* token = [[NSData alloc] init]; + [self.appDelegate application:[UIApplication sharedApplication] + didRegisterForRemoteNotificationsWithDeviceToken:token]; + [(NSObject*)[delegate verify] + application:[UIApplication sharedApplication] + didRegisterForRemoteNotificationsWithDeviceToken:token]; +} + +- (void)testDidReceiveRemoteNotificationFalse { + XCTAssertFalse([self.appDelegate + respondsToSelector:@selector(application: + didReceiveRemoteNotification:fetchCompletionHandler:)]); +} + +- (void)testDidRegisterForRemoteNotificationsWithDeviceTokenFalse { + XCTAssertFalse([self.appDelegate + respondsToSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]); +} + +- (void)testDidFailToRegisterForRemoteNotificationsWithError { + id delegate = OCMProtocolMock(@protocol(FlutterApplicationLifeCycleDelegate)); + [self.appDelegate addApplicationLifeCycleDelegate:delegate]; + XCTAssertTrue([self.appDelegate + respondsToSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:)]); + NSError* error = [[NSError alloc] init]; + [self.appDelegate application:[UIApplication sharedApplication] + didFailToRegisterForRemoteNotificationsWithError:error]; + [(NSObject*)[delegate verify] + application:[UIApplication sharedApplication] + didFailToRegisterForRemoteNotificationsWithError:error]; +} + @end diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm index 2ea4fe2afe..cab7697667 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm @@ -4,6 +4,7 @@ #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h" +#import #include "flutter/fml/logging.h" #include "flutter/fml/paths.h" #include "flutter/lib/ui/plugins/callback_cache.h" @@ -11,9 +12,17 @@ #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterCallbackCache_Internal.h" static const char* kCallbackCacheSubDir = "Library/Caches/"; +static NSString* const kApplicationDidReceiveRemoteNotificationFetchCompletionHandler = + @"application:didReceiveRemoteNotification:fetchCompletionHandler:"; +static NSString* const kApplicationDidRegisterForRemoteNotificationsWithDeviceToken = + @"application:didRegisterForRemoteNotificationsWithDeviceToken:"; +static NSString* const kApplicationDidFailToRegisterForRemoteNotificationsWithError = + @"application:didFailToRegisterForRemoteNotificationsWithError:"; static const SEL selectorsHandledByPlugins[] = { - @selector(application:didReceiveRemoteNotification:fetchCompletionHandler:), + NSSelectorFromString(kApplicationDidReceiveRemoteNotificationFetchCompletionHandler), + NSSelectorFromString(kApplicationDidRegisterForRemoteNotificationsWithDeviceToken), + NSSelectorFromString(kApplicationDidFailToRegisterForRemoteNotificationsWithError), @selector(application:performFetchWithCompletionHandler:)}; @interface FlutterPluginAppLifeCycleDelegate () @@ -32,6 +41,35 @@ static const SEL selectorsHandledByPlugins[] = { NSPointerArray* _delegates; } +static void RemapMethod(Class thisClass, SEL originalSelector, SEL addedSelector) { + Method originalMethod = class_getInstanceMethod(thisClass, originalSelector); + IMP originalImp = method_getImplementation(originalMethod); + class_addMethod(thisClass, addedSelector, originalImp, method_getTypeEncoding(originalMethod)); +} + ++ (void)load { + // Swap out + // `performApplication:didReceiveRemoteNotification:fetchCompletionHandler:` + // for `application:didReceiveRemoteNotification:fetchCompletionHandler:`. + // This has to be done to avoid a potential false positive "Missing Push + // Notification Entitlement" validation failure when uploading apps to the App + // Store. Linked NSPlugin implementations that have + // `application:didReceiveRemoteNotification:fetchCompletionHandler:` defined + // will cause the failure to happen correctly if the entitlements are missing. + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + RemapMethod( + self, @selector(performApplication:didReceiveRemoteNotification:fetchCompletionHandler:), + NSSelectorFromString(kApplicationDidReceiveRemoteNotificationFetchCompletionHandler)); + RemapMethod(self, + @selector(performApplication:didRegisterForRemoteNotificationsWithDeviceToken:), + NSSelectorFromString(kApplicationDidRegisterForRemoteNotificationsWithDeviceToken)); + RemapMethod(self, + @selector(performApplication:didFailToRegisterForRemoteNotificationsWithError:), + NSSelectorFromString(kApplicationDidFailToRegisterForRemoteNotificationsWithError)); + }); +} + - (void)addObserverFor:(NSString*)name selector:(SEL)selector { [[NSNotificationCenter defaultCenter] addObserver:self selector:selector name:name object:nil]; __block NSObject* blockSelf = self; @@ -235,7 +273,7 @@ static BOOL IsPowerOfTwo(NSUInteger x) { } #pragma GCC diagnostic pop -- (void)application:(UIApplication*)application +- (void)performApplication:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken { for (NSObject* delegate in _delegates) { if (!delegate) { @@ -248,7 +286,7 @@ static BOOL IsPowerOfTwo(NSUInteger x) { } } -- (void)application:(UIApplication*)application +- (void)performApplication:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error { for (NSObject* delegate in _delegates) { if (!delegate) { @@ -260,7 +298,9 @@ static BOOL IsPowerOfTwo(NSUInteger x) { } } -- (void)application:(UIApplication*)application +// This will get swapped into `application:didReceiveRemoteNotification:fetchCompletionHandler:` +// in +[FlutterPluginAppLifeCycleDelegate load]. +- (void)performApplication:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { for (NSObject* delegate in _delegates) { diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm index 9891f52cf8..0022533d1e 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm @@ -10,6 +10,31 @@ FLUTTER_ASSERT_ARC +@interface FlutterPluginAppLifeCycleDelegate () +- (void)application:(UIApplication*)application + didReceiveRemoteNotification:(NSDictionary*)userInfo + fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler; +- (void)application:(UIApplication*)application + didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken; +- (void)application:(UIApplication*)application + didFailToRegisterForRemoteNotificationsWithError:(NSError*)error; +@end + +@interface FlutterPluginAppLifeCycleDelegateSubclass : FlutterPluginAppLifeCycleDelegate +@property(nonatomic, strong) XCTestExpectation* didReceiveRemoteNotificationExpectation; +@end + +@implementation FlutterPluginAppLifeCycleDelegateSubclass +- (void)performApplication:(UIApplication*)application + didReceiveRemoteNotification:(NSDictionary*)userInfo + fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler { + [self.didReceiveRemoteNotificationExpectation fulfill]; + [super performApplication:application + didReceiveRemoteNotification:userInfo + fetchCompletionHandler:completionHandler]; +} +@end + @interface FlutterPluginAppLifeCycleDelegateTest : XCTestCase @end @@ -89,4 +114,68 @@ FLUTTER_ASSERT_ARC OCMVerify([plugin applicationWillTerminate:[UIApplication sharedApplication]]); } +- (void)testDidReceiveRemoteNotification { + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); + [delegate addDelegate:plugin]; + NSDictionary* info = @{}; + void (^handler)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result) { + }; + XCTAssertTrue([delegate respondsToSelector:@selector + (application:didReceiveRemoteNotification:fetchCompletionHandler:)]); + [delegate application:[UIApplication sharedApplication] + didReceiveRemoteNotification:info + fetchCompletionHandler:handler]; + [(NSObject*)[plugin verify] application:[UIApplication sharedApplication] + didReceiveRemoteNotification:info + fetchCompletionHandler:handler]; +} + +- (void)flutterPluginAppLifeCycleDelegateSubclass { + FlutterPluginAppLifeCycleDelegateSubclass* delegate = + [[FlutterPluginAppLifeCycleDelegateSubclass alloc] init]; + XCTestExpectation* expecation = [self expectationWithDescription:@"subclass called"]; + delegate.didReceiveRemoteNotificationExpectation = expecation; + id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); + [delegate addDelegate:plugin]; + NSDictionary* info = @{}; + void (^handler)(UIBackgroundFetchResult) = ^(UIBackgroundFetchResult result) { + }; + XCTAssertTrue([delegate respondsToSelector:@selector + (application:didReceiveRemoteNotification:fetchCompletionHandler:)]); + [delegate application:[UIApplication sharedApplication] + didReceiveRemoteNotification:info + fetchCompletionHandler:handler]; + [(NSObject*)[plugin verify] application:[UIApplication sharedApplication] + didReceiveRemoteNotification:info + fetchCompletionHandler:handler]; + [self waitForExpectationsWithTimeout:1.0 handler:nil]; +} + +- (void)testDidRegisterForRemoteNotificationsWithDeviceToken { + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); + [delegate addDelegate:plugin]; + NSData* token = [[NSData alloc] init]; + XCTAssertTrue([delegate + respondsToSelector:@selector(application:didRegisterForRemoteNotificationsWithDeviceToken:)]); + [delegate application:[UIApplication sharedApplication] + didRegisterForRemoteNotificationsWithDeviceToken:token]; + [(NSObject*)[plugin verify] application:[UIApplication sharedApplication] + didRegisterForRemoteNotificationsWithDeviceToken:token]; +} + +- (void)testDidFailToRegisterForRemoteNotificationsWithError { + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + id plugin = OCMProtocolMock(@protocol(FlutterPlugin)); + [delegate addDelegate:plugin]; + NSError* error = [[NSError alloc] init]; + XCTAssertTrue([delegate + respondsToSelector:@selector(application:didFailToRegisterForRemoteNotificationsWithError:)]); + [delegate application:[UIApplication sharedApplication] + didFailToRegisterForRemoteNotificationsWithError:error]; + [(NSObject*)[plugin verify] application:[UIApplication sharedApplication] + didFailToRegisterForRemoteNotificationsWithError:error]; +} + @end