Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b2eabf2
FIRAuthAppDelegateProxy: implementation updated to use GULAppDelegate…
maksymmalyhin Mar 26, 2019
4ab86de
GULAppDelegateSwizzler - make sure subclass name is unique.
maksymmalyhin Mar 27, 2019
1f1560a
GoogleUtilities: enable code coverage
maksymmalyhin Mar 27, 2019
f4a4638
Merge branch 'master'
maksymmalyhin Mar 27, 2019
9fcedce
GULAppDelegateSwizzler: Add support of remote notification methods
maksymmalyhin Mar 27, 2019
27c8b5b
GULAppDelegateSwizzler - tests for remote notification methods
maksymmalyhin Mar 27, 2019
dee28d3
Revert "FIRAuthAppDelegateProxy: implementation updated to use GULApp…
maksymmalyhin Mar 27, 2019
5821591
FIRMessaging: prepare to use GULAppDelegateSwizzler [WIP]
maksymmalyhin Mar 27, 2019
908b6ea
FIRMessagingRemoteNotificationsProxy - use GULAppDelegateSwizzler [WIP]
maksymmalyhin Mar 28, 2019
9e377d2
FIRMessagingRemoteNotificationsProxy - AppDelegate proxy - test only …
maksymmalyhin Mar 28, 2019
7861cde
FIRMessagingRemoteNotificationsProxy test only public API with no imp…
maksymmalyhin Mar 28, 2019
b7703ac
Cleanup
maksymmalyhin Mar 28, 2019
ad1da54
Cleanup
maksymmalyhin Mar 28, 2019
2bdf353
Merge branch 'master'
maksymmalyhin Mar 28, 2019
8eafece
Merge branch 'mm/messaging-tests'
maksymmalyhin Mar 28, 2019
e5d2ce8
Merge branch 'master'
maksymmalyhin Mar 29, 2019
86ff257
Cocoapods 1.6.1: Podfile supports only single post_install hook. It i…
maksymmalyhin Mar 29, 2019
79b7613
FIRMessagingRemoteNotificationsProxyTest: [GULAppDelegateSwizzler res…
maksymmalyhin Mar 29, 2019
98ffccb
FIRMessagingRemoteNotificationsProxy: app delegate missing methods te…
maksymmalyhin Mar 29, 2019
cd44395
GULAppDelegateSwizzler: don't swizzle invalid application delegate
maksymmalyhin Mar 29, 2019
3df4140
Cleanup
maksymmalyhin Mar 29, 2019
dcd0e10
Run ./scripts/check.sh
maksymmalyhin Mar 29, 2019
18381dd
Merge branch 'master'
maksymmalyhin Apr 1, 2019
dae75d9
GoogleUtilities/GULAppDelegateSwizzler - tvOS support
maksymmalyhin Apr 1, 2019
6dee8d3
style.sh generated changes
maksymmalyhin Apr 1, 2019
2726e7c
FirebaseMessaging - missing dependencies added
maksymmalyhin Apr 1, 2019
63fd1f5
FCM data channel messages - pass data to [FIRMessaging appDidReceiveM…
maksymmalyhin Apr 1, 2019
4a3fbb1
FIRMessagingRemoteNotificationsProxyTest - remove GULLoggerForceDebug()
maksymmalyhin Apr 2, 2019
b390f4f
GULAppDelegateSwizzler - add deprecated `application:didReceiveRemote…
maksymmalyhin Apr 2, 2019
236877d
Merge branch 'master'
maksymmalyhin Apr 3, 2019
3641d35
Merge branch 'master'
maksymmalyhin Apr 3, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
FIRMessagingRemoteNotificationsProxy: app delegate missing methods te…
…sts added. Cleanup
  • Loading branch information
maksymmalyhin committed Mar 29, 2019
commit 98ffccbbd08514b499f5c4f4d9cf0faff29ce65e
39 changes: 37 additions & 2 deletions Example/Messaging/Tests/FIRMessagingRemoteNotificationsProxyTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ @implementation IncompleteAppDelegate
@interface FakeAppDelegate : NSObject <UIApplicationDelegate>
@property(nonatomic) BOOL remoteNotificationMethodWasCalled;
@property(nonatomic) BOOL remoteNotificationWithFetchHandlerWasCalled;
@property(nonatomic, strong) NSData *deviceToken;
@property(nonatomic, strong) NSError *registerForRemoteNotificationsError;
@end
@implementation FakeAppDelegate
#if TARGET_OS_IOS
Expand All @@ -78,6 +80,17 @@ - (void)application:(UIApplication *)application
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
self.remoteNotificationWithFetchHandlerWasCalled = YES;
}

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
self.deviceToken = deviceToken;
}

- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
self.registerForRemoteNotificationsError = error;
}

@end

#pragma mark - Incompete UNUserNotificationCenterDelegate
Expand Down Expand Up @@ -145,8 +158,7 @@ - (void)setUp {
OCMStub([_mockProxyClass sharedProxy]).andReturn(self.proxy);

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
_mockUserNotificationCenter = OCMClassMock([UNUserNotificationCenter class]);
OCMStub([_mockUserNotificationCenter currentNotificationCenter]).andReturn(_mockUserNotificationCenter);
_mockUserNotificationCenter = OCMPartialMock([UNUserNotificationCenter currentNotificationCenter]);
#endif
}

Expand All @@ -160,6 +172,9 @@ - (void)tearDown {
[_mockSharedApplication stopMocking];
_mockSharedApplication = nil;

[_mockUserNotificationCenter stopMocking];
_mockUserNotificationCenter = nil;

_proxy = nil;
[super tearDown];
}
Expand Down Expand Up @@ -238,6 +253,26 @@ - (void)testSwizzledAppDelegateRemoteNotificationMethods {
XCTAssertTrue(appDelegate.remoteNotificationWithFetchHandlerWasCalled);

[self.mockMessaging verify];

// Verify application:didRegisterForRemoteNotificationsWithDeviceToken:
NSData *deviceToken = [NSData data];

OCMExpect([self.mockMessaging setAPNSToken:deviceToken]);

[appDelegate application:self.mockSharedApplication
didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];

XCTAssertEqual(appDelegate.deviceToken, deviceToken);
[self.mockMessaging verify];

// Verify application:didFailToRegisterForRemoteNotificationsWithError:
NSError *error = [NSError errorWithDomain:@"tests" code:-1 userInfo:nil];

[appDelegate application:self.mockSharedApplication
didFailToRegisterForRemoteNotificationsWithError:error];

XCTAssertEqual(appDelegate.registerForRemoteNotificationsError, error);

#endif
}

Expand Down
100 changes: 0 additions & 100 deletions Firebase/Messaging/FIRMessagingRemoteNotificationsProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@
@"userNotificationCenter:willPresentNotification:withCompletionHandler:";
static NSString *kUserNotificationDidReceiveResponseSelectorString =
@"userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:";
static NSString *kReceiveDataMessageSelectorString = @"messaging:didReceiveMessage:";

@interface FIRMessagingRemoteNotificationsProxy () <UIApplicationDelegate>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an alternative than following this protocol? This makes macOS support very hard .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refer comment below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chliangGoogle Does FCM support MacOS now? If not, I can take care of MacOS support for GULAppDelegateSwizzler in a separate PR. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulb777 It looks like tvOS has no difference in the App Delegate, so supporting tvOS by GULAppDelegateSwizzler should be trivial. I'll update it in this PR

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will we make GULAppDelegateSwizzler macOS support in this PR? If not, we can add a todo and fix in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a ticket for this #2706


@property(strong, nonatomic) NSMutableDictionary<NSString *, NSValue *> *originalAppDelegateImps;
@property(strong, nonatomic) NSMutableDictionary<NSString *, NSArray *> *swizzledSelectorsByClass;

@property(nonatomic) BOOL didSwizzleMethods;
@property(nonatomic) BOOL didSwizzleAppDelegateMethods;

@property(nonatomic) BOOL hasSwizzledUserNotificationDelegate;
@property(nonatomic) BOOL isObservingUserNotificationDelegateChanges;
Expand Down Expand Up @@ -92,13 +90,6 @@ - (void)swizzleMethodsIfPossible {
[GULAppDelegateSwizzler proxyOriginalDelegate];
self.appDelegateInterceptorID = [GULAppDelegateSwizzler registerAppDelegateInterceptor:self];

UIApplication *application = [GULAppDelegateSwizzler sharedApplication];
if (!application) {
return;
}
NSObject<UIApplicationDelegate> *appDelegate = [application delegate];
[self swizzleAppDelegateMethods:appDelegate];

// Add KVO listener on [UNUserNotificationCenter currentNotificationCenter]'s delegate property
Class notificationCenterClass = NSClassFromString(@"UNUserNotificationCenter");
if (notificationCenterClass) {
Expand Down Expand Up @@ -127,24 +118,6 @@ - (void)unswizzleAllMethods {
[self.swizzledSelectorsByClass removeAllObjects];
}

- (void)swizzleAppDelegateMethods:(id<UIApplicationDelegate>)appDelegate {
Class appDelegateClass = [appDelegate class];

BOOL didSwizzleAppDelegate = NO;

// For data message from MCS.
SEL receiveDataMessageSelector = NSSelectorFromString(kReceiveDataMessageSelectorString);
if ([appDelegate respondsToSelector:receiveDataMessageSelector]) {
[self swizzleSelector:receiveDataMessageSelector
inClass:appDelegateClass
withImplementation:(IMP)FCM_swizzle_messagingDidReceiveMessage
inProtocol:@protocol(UIApplicationDelegate)];
didSwizzleAppDelegate = YES;
}

self.didSwizzleAppDelegateMethods = didSwizzleAppDelegate;
}

- (void)listenForDelegateChangesInUserNotificationCenter:(id)notificationCenter {
Class notificationCenterClass = NSClassFromString(@"UNUserNotificationCenter");
if (![notificationCenter isKindOfClass:notificationCenterClass]) {
Expand Down Expand Up @@ -446,37 +419,6 @@ - (void)application:(UIApplication *)application

#pragma mark - Swizzled Methods

void FCM_swizzle_appDidReceiveRemoteNotification(id self,
SEL _cmd,
UIApplication *app,
NSDictionary *userInfo) {
[[FIRMessaging messaging] appDidReceiveMessage:userInfo];

IMP original_imp =
[[FIRMessagingRemoteNotificationsProxy sharedProxy] originalImplementationForSelector:_cmd];
if (original_imp) {
((void (*)(id, SEL, UIApplication *, NSDictionary *))original_imp)(self,
_cmd,
app,
userInfo);
}
}

void FCM_swizzle_appDidReceiveRemoteNotificationWithHandler(
id self, SEL _cmd, UIApplication *app, NSDictionary *userInfo,
void (^handler)(UIBackgroundFetchResult)) {

[[FIRMessaging messaging] appDidReceiveMessage:userInfo];

IMP original_imp =
[[FIRMessagingRemoteNotificationsProxy sharedProxy] originalImplementationForSelector:_cmd];
if (original_imp) {
((void (*)(id, SEL, UIApplication *, NSDictionary *,
void (^)(UIBackgroundFetchResult)))original_imp)(
self, _cmd, app, userInfo, handler);
}
}

/**
* Swizzle the notification handler for iOS 10+ devices.
* Signature of original handler is as below:
Expand Down Expand Up @@ -652,46 +594,4 @@ id userInfoFromNotification(id notification) {
return notificationUserInfo;
}

void FCM_swizzle_messagingDidReceiveMessage(id self, SEL _cmd, FIRMessaging *message,
FIRMessagingRemoteMessage *remoteMessage) {
[[FIRMessaging messaging] appDidReceiveMessage:remoteMessage.appData];

IMP original_imp =
[[FIRMessagingRemoteNotificationsProxy sharedProxy] originalImplementationForSelector:_cmd];
if (original_imp) {
((void (*)(id, SEL, FIRMessaging *, FIRMessagingRemoteMessage *))original_imp)(
self, _cmd, message, remoteMessage);
}
}

void FCM_swizzle_appDidFailToRegisterForRemoteNotifications(id self,
SEL _cmd,
UIApplication *app,
NSError *error) {
// Log the fact that we failed to register for remote notifications
FIRMessagingLoggerError(kFIRMessagingMessageCodeRemoteNotificationsProxyAPNSFailed,
@"Error in "
@"application:didFailToRegisterForRemoteNotificationsWithError: %@",
error.localizedDescription);
IMP original_imp =
[[FIRMessagingRemoteNotificationsProxy sharedProxy] originalImplementationForSelector:_cmd];
if (original_imp) {
((void (*)(id, SEL, UIApplication *, NSError *))original_imp)(self, _cmd, app, error);
}
}

void FCM_swizzle_appDidRegisterForRemoteNotifications(id self,
SEL _cmd,
UIApplication *app,
NSData *deviceToken) {
// Pass the APNSToken along to FIRMessaging (and auto-detect the token type)
[FIRMessaging messaging].APNSToken = deviceToken;

IMP original_imp =
[[FIRMessagingRemoteNotificationsProxy sharedProxy] originalImplementationForSelector:_cmd];
if (original_imp) {
((void (*)(id, SEL, UIApplication *, NSData *))original_imp)(self, _cmd, app, deviceToken);
}
}

@end