1818
1919#import < UIKit/UIKit.h>
2020
21- #import < GoogleUtilities/GULAppEnvironmentUtil.h>
22- #import < GoogleUtilities/GULUserDefaults.h>
23-
2421#import " FIRMessaging.h"
2522#import " FIRMessagingLogger.h"
2623#import " FIRMessagingUtilities.h"
2724#import " FIRMessaging_Private.h"
2825
2926static NSString *const kUpstreamMessageIDUserInfoKey = @" messageID" ;
3027static NSString *const kUpstreamErrorUserInfoKey = @" error" ;
31- // / "Should use Messaging delegate" key stored in NSUserDefaults
32- NSString *const kFIRMessagingUserDefaultsKeyUseMessagingDelegate =
33- @" com.firebase.messaging.useMessagingDelegate" ;
34- // / "Should use Messaging Delegate" key stored in Info.plist
35- NSString *const kFIRMessagingPlistUseMessagingDelegate =
36- @" FirebaseMessagingUseMessagingDelegateForDirectChannel" ;
3728
3829static int downstreamMessageID = 0 ;
3930
@@ -43,31 +34,14 @@ @interface FIRMessagingReceiver ()
4334
4435@implementation FIRMessagingReceiver
4536
46- #pragma mark - Initializer
47-
48- - (instancetype )initWithUserDefaults : (GULUserDefaults *)defaults {
49- self = [super init ];
50- if (self != nil ) {
51- _defaults = defaults;
52- }
53- return self;
54- }
55-
5637#pragma mark - FIRMessagingDataMessageManager protocol
5738
5839- (void )didReceiveMessage : (NSDictionary *)message withIdentifier : (nullable NSString *)messageID {
5940 if (![messageID length ]) {
6041 messageID = [[self class ] nextMessageID ];
6142 }
6243
63- NSInteger majorOSVersion = [[GULAppEnvironmentUtil systemVersion ] integerValue ];
64- if (majorOSVersion >= 10 || self.useDirectChannel ) {
65- // iOS 10 and above or use direct channel is enabled.
66- [self scheduleIos10NotificationForMessage: message withIdentifier: messageID];
67- } else {
68- // Post notification directly to AppDelegate handlers. This is valid pre-iOS 10.
69- [self scheduleNotificationForMessage: message];
70- }
44+ [self handleDirectChannelMessage: message withIdentifier: messageID];
7145}
7246
7347- (void )willSendDataMessageWithID : (NSString *)messageID error : (NSError *)error {
@@ -112,83 +86,18 @@ - (void)didDeleteMessagesOnServer {
11286}
11387
11488#pragma mark - Private Helpers
115- // As the new UserNotifications framework in iOS 10 doesn't support constructor/mutation for
116- // UNNotification object, FCM can't inject the message to the app with UserNotifications framework.
117- // Define our own protocol, which means app developers need to implement two interfaces to receive
118- // display notifications and data messages respectively for devices running iOS 10 or above. Devices
119- // running iOS 9 or below are not affected.
120- - (void )scheduleIos10NotificationForMessage : (NSDictionary *)message
121- withIdentifier : (NSString *)messageID {
89+ - (void )handleDirectChannelMessage : (NSDictionary *)message withIdentifier : (NSString *)messageID {
12290 FIRMessagingRemoteMessage *wrappedMessage = [[FIRMessagingRemoteMessage alloc ] init ];
123- // TODO: wrap title, body, badge and other fields
12491 wrappedMessage.appData = [message copy ];
12592 wrappedMessage.messageID = messageID;
12693 [self .delegate receiver: self receivedRemoteMessage: wrappedMessage];
12794}
12895
129- - (void )scheduleNotificationForMessage : (NSDictionary *)message {
130- SEL newNotificationSelector =
131- @selector (application:didReceiveRemoteNotification:fetchCompletionHandler: );
132- SEL oldNotificationSelector = @selector (application:didReceiveRemoteNotification: );
133-
134- dispatch_async (dispatch_get_main_queue (), ^{
135- UIApplication *application = FIRMessagingUIApplication ();
136- if (!application) {
137- return ;
138- }
139- id <UIApplicationDelegate> appDelegate = [application delegate ];
140- if ([appDelegate respondsToSelector: newNotificationSelector]) {
141- // Try the new remote notification callback
142- [appDelegate application: application
143- didReceiveRemoteNotification: message
144- fetchCompletionHandler: ^(UIBackgroundFetchResult result) {
145- }];
146-
147- } else if ([appDelegate respondsToSelector: oldNotificationSelector]) {
148- // Try the old remote notification callback
149- #pragma clang diagnostic push
150- #pragma clang diagnostic ignored "-Wdeprecated-declarations"
151- [appDelegate application: application didReceiveRemoteNotification: message];
152- #pragma clang diagnostic pop
153- } else {
154- FIRMessagingLoggerError (kFIRMessagingMessageCodeReceiver005 ,
155- @" None of the remote notification callbacks implemented by "
156- @" UIApplicationDelegate" );
157- }
158- });
159- }
160-
16196+ (NSString *)nextMessageID {
16297 @synchronized (self) {
16398 ++downstreamMessageID;
16499 return [NSString stringWithFormat: @" gcm-%d " , downstreamMessageID];
165100 }
166101}
167102
168- - (BOOL )useDirectChannel {
169- // Check storage
170- id shouldUseMessagingDelegate =
171- [_defaults objectForKey: kFIRMessagingUserDefaultsKeyUseMessagingDelegate ];
172- if (shouldUseMessagingDelegate) {
173- return [shouldUseMessagingDelegate boolValue ];
174- }
175-
176- // Check Info.plist
177- shouldUseMessagingDelegate =
178- [[NSBundle mainBundle ] objectForInfoDictionaryKey: kFIRMessagingPlistUseMessagingDelegate ];
179- if (shouldUseMessagingDelegate) {
180- return [shouldUseMessagingDelegate boolValue ];
181- }
182- // If none of above exists, we go back to default behavior which is NO.
183- return NO ;
184- }
185-
186- - (void )setUseDirectChannel : (BOOL )useDirectChannel {
187- BOOL shouldUseMessagingDelegate = [self useDirectChannel ];
188- if (useDirectChannel != shouldUseMessagingDelegate) {
189- [_defaults setBool: useDirectChannel forKey: kFIRMessagingUserDefaultsKeyUseMessagingDelegate ];
190- [_defaults synchronize ];
191- }
192- }
193-
194103@end
0 commit comments