Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 9 additions & 2 deletions Firebase/Messaging/FIRMessaging.m
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,10 @@ - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message {
});
return;
}
UIApplication *application = [UIApplication sharedApplication];
UIApplication *application = FIRMessagingUIApplication();
if (!application) {
return;
}
id<UIApplicationDelegate> appDelegate = application.delegate;
SEL continueUserActivitySelector =
@selector(application:continueUserActivity:restorationHandler:);
Expand Down Expand Up @@ -611,7 +614,11 @@ - (BOOL)shouldBeConnectedAutomatically {
// We require a token from Instance ID
NSString *token = self.defaultFcmToken;
// Only on foreground connections
UIApplicationState applicationState = [UIApplication sharedApplication].applicationState;
UIApplication *application = FIRMessagingUIApplication();
if (!application) {
return NO;
}
UIApplicationState applicationState = application.applicationState;
BOOL shouldBeConnected = _shouldEstablishDirectChannel &&
(token.length > 0) &&
applicationState == UIApplicationStateActive;
Expand Down
7 changes: 6 additions & 1 deletion Firebase/Messaging/FIRMessagingContextManagerService.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#import "FIRMessagingDefines.h"
#import "FIRMessagingLogger.h"
#import "FIRMessagingUtilities.h"

#define kFIRMessagingContextManagerPrefixKey @"google.c.cm."
#define kFIRMessagingContextManagerNotificationKeyPrefix @"gcm.notification."
Expand Down Expand Up @@ -174,7 +175,11 @@ + (void)scheduleLocalNotificationForMessage:(NSDictionary *)message
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
UIApplication *application = FIRMessagingUIApplication();
if (!application) {
return;
}
[application scheduleLocalNotification:notification];
#pragma clang diagnostic pop
}

Expand Down
19 changes: 12 additions & 7 deletions Firebase/Messaging/FIRMessagingReceiver.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#import <UIKit/UIKit.h>

#import "FIRMessaging.h"
#import "FIRMessaging_Private.h"
#import "FIRMessagingLogger.h"
#import "FIRMessagingUtilities.h"
#import "FIRMessaging_Private.h"

static NSString *const kUpstreamMessageIDUserInfoKey = @"messageID";
static NSString *const kUpstreamErrorUserInfoKey = @"error";
Expand Down Expand Up @@ -111,19 +112,23 @@ - (void)scheduleNotificationForMessage:(NSDictionary *)message {
SEL oldNotificationSelector = @selector(application:didReceiveRemoteNotification:);

dispatch_async(dispatch_get_main_queue(), ^{
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
UIApplication *application = FIRMessagingUIApplication();
if (!application) {
return;
}
id<UIApplicationDelegate> appDelegate = [application delegate];
if ([appDelegate respondsToSelector:newNotificationSelector]) {
// Try the new remote notification callback
[appDelegate application:[UIApplication sharedApplication]
didReceiveRemoteNotification:message
fetchCompletionHandler:^(UIBackgroundFetchResult result) {}];
[appDelegate application:application
didReceiveRemoteNotification:message
fetchCompletionHandler:^(UIBackgroundFetchResult result) {
}];

} else if ([appDelegate respondsToSelector:oldNotificationSelector]) {
// Try the old remote notification callback
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[appDelegate application:
[UIApplication sharedApplication] didReceiveRemoteNotification:message];
[appDelegate application:application didReceiveRemoteNotification:message];
#pragma clang diagnostic pop
} else {
FIRMessagingLoggerError(kFIRMessagingMessageCodeReceiver005,
Expand Down
7 changes: 6 additions & 1 deletion Firebase/Messaging/FIRMessagingRemoteNotificationsProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#import "FIRMessagingConstants.h"
#import "FIRMessagingLogger.h"
#import "FIRMessagingUtilities.h"
#import "FIRMessaging_Private.h"

static const BOOL kDefaultAutoRegisterEnabledValue = YES;
Expand Down Expand Up @@ -98,7 +99,11 @@ - (void)swizzleMethodsIfPossible {
return;
}

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

// Add KVO listener on [UNUserNotificationCenter currentNotificationCenter]'s delegate property
Expand Down
6 changes: 5 additions & 1 deletion Firebase/Messaging/FIRMessagingUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

typedef NS_ENUM(int8_t, FIRMessagingProtoTag) {
kFIRMessagingProtoTagInvalid = -1,
Expand Down Expand Up @@ -51,4 +51,8 @@ FOUNDATION_EXPORT int64_t FIRMessagingCurrentTimestampInMilliseconds(void);
FOUNDATION_EXPORT NSString *FIRMessagingCurrentAppVersion(void);
FOUNDATION_EXPORT NSString *FIRMessagingAppIdentifier(void);

#pragma mark - Others

FOUNDATION_EXPORT uint64_t FIRMessagingGetFreeDiskSpaceInMB(void);
FOUNDATION_EXPORT UIApplication *FIRMessagingUIApplication(void);

15 changes: 15 additions & 0 deletions Firebase/Messaging/FIRMessagingUtilities.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#import "FIRMessagingLogger.h"

#import <GoogleUtilities/GULAppEnvironmentUtil.h>

// Convert the macro to a string
#define STR_EXPAND(x) #x
#define STR(x) STR_EXPAND(x)
Expand Down Expand Up @@ -171,3 +173,16 @@ uint64_t FIRMessagingGetFreeDiskSpaceInMB(void) {
return 0;
}
}

UIApplication *FIRMessagingUIApplication(void) {
static Class applicationClass = nil;
// iOS App extensions should not call [UIApplication sharedApplication], even if UIApplication
// responds to it.
if (![GULAppEnvironmentUtil isAppExtension]) {
Class cls = NSClassFromString(@"UIApplication");
if (cls && [cls respondsToSelector:NSSelectorFromString(@"sharedApplication")]) {
applicationClass = cls;
}
}
return [applicationClass sharedApplication];
}