Removed the email warning about notifications entitlement (flutter/engine#29996)

This commit is contained in:
gaaclarke
2021-11-30 14:51:08 -08:00
committed by GitHub
parent 1324bac660
commit 5b1174940a
5 changed files with 216 additions and 20 deletions

View File

@@ -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;

View File

@@ -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 -[<UIApplicationDelegate> "

View File

@@ -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<FlutterApplicationLifeCycleDelegate>*)[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<FlutterApplicationLifeCycleDelegate>*)[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<FlutterApplicationLifeCycleDelegate>*)[delegate verify]
application:[UIApplication sharedApplication]
didFailToRegisterForRemoteNotificationsWithError:error];
}
@end

View File

@@ -4,6 +4,7 @@
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h"
#import <objc/runtime.h>
#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<FlutterApplicationLifeCycleDelegate>* 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<FlutterApplicationLifeCycleDelegate>* 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<FlutterApplicationLifeCycleDelegate>* delegate in _delegates) {

View File

@@ -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<FlutterPlugin>*)[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<FlutterPlugin>*)[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<FlutterPlugin>*)[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<FlutterPlugin>*)[plugin verify] application:[UIApplication sharedApplication]
didFailToRegisterForRemoteNotificationsWithError:error];
}
@end