Skip to content

Commit

Permalink
Remove useMessagingDelegateForDirectChannel property (#2711)
Browse files Browse the repository at this point in the history
  • Loading branch information
charlotteliang authored and Corrob committed Apr 22, 2019
1 parent 1a633c5 commit 3a93066
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 734 deletions.
6 changes: 0 additions & 6 deletions Example/Firebase.xcodeproj/project.pbxproj
Expand Up @@ -137,7 +137,6 @@
511DD29D2225C8C40094D78D /* FIRMessagingLinkHandlingTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE9315CC1E8738B70083EDBF /* FIRMessagingLinkHandlingTest.m */; };
511DD29E2225C8C40094D78D /* FIRMessagingPendingTopicsListTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE9315CD1E8738B70083EDBF /* FIRMessagingPendingTopicsListTest.m */; };
511DD29F2225C8C40094D78D /* FIRMessagingPubSubTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE9315CE1E8738B70083EDBF /* FIRMessagingPubSubTest.m */; };
511DD2A02225C8C40094D78D /* FIRMessagingReceiverTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DEF61BFC216E8B1000A738D4 /* FIRMessagingReceiverTest.m */; };
511DD2A12225C8C40094D78D /* FIRMessagingRegistrarTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE9315CF1E8738B70083EDBF /* FIRMessagingRegistrarTest.m */; };
511DD2A22225C8C40094D78D /* FIRMessagingRemoteNotificationsProxyTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE9315D01E8738B70083EDBF /* FIRMessagingRemoteNotificationsProxyTest.m */; };
511DD2A32225C8C40094D78D /* FIRMessagingRmqManagerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DE9315D11E8738B70083EDBF /* FIRMessagingRmqManagerTest.m */; };
Expand Down Expand Up @@ -635,7 +634,6 @@
DEE14D931E84468D006FA992 /* FIROptionsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DEE14D7A1E844677006FA992 /* FIROptionsTest.m */; };
DEE14D941E84468D006FA992 /* FIRTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = DEE14D7C1E844677006FA992 /* FIRTestCase.m */; };
DEF288411F9AB6E100D480CF /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = DEF288401F9AB6E100D480CF /* Default-568h@2x.png */; };
DEF61BFD216E8B1100A738D4 /* FIRMessagingReceiverTest.m in Sources */ = {isa = PBXBuildFile; fileRef = DEF61BFC216E8B1000A738D4 /* FIRMessagingReceiverTest.m */; };
DEF6C30D1FBCE72F005D0740 /* FIRAuthDispatcherTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE9314FF1E86C6FF0083EDBF /* FIRAuthDispatcherTests.m */; };
DEF6C30F1FBCE775005D0740 /* FIRAdditionalUserInfoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE9314FA1E86C6FF0083EDBF /* FIRAdditionalUserInfoTests.m */; };
DEF6C3101FBCE775005D0740 /* FIRApp+FIRAuthUnitTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DE9314FC1E86C6FF0083EDBF /* FIRApp+FIRAuthUnitTests.m */; };
Expand Down Expand Up @@ -1478,7 +1476,6 @@
DEE14D7C1E844677006FA992 /* FIRTestCase.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRTestCase.m; sourceTree = "<group>"; };
DEE14D7D1E844677006FA992 /* Tests-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Tests-Info.plist"; sourceTree = "<group>"; };
DEF288401F9AB6E100D480CF /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; };
DEF61BFC216E8B1000A738D4 /* FIRMessagingReceiverTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRMessagingReceiverTest.m; sourceTree = "<group>"; };
E2C2834C90DBAB56D568189F /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
ED34CF4A20DC16DC000EA5D1 /* FIRComponentContainerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRComponentContainerTest.m; sourceTree = "<group>"; };
ED34CF4B20DC16DC000EA5D1 /* FIRTestComponents.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = FIRTestComponents.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2557,7 +2554,6 @@
DE9315CC1E8738B70083EDBF /* FIRMessagingLinkHandlingTest.m */,
DE9315CD1E8738B70083EDBF /* FIRMessagingPendingTopicsListTest.m */,
DE9315CE1E8738B70083EDBF /* FIRMessagingPubSubTest.m */,
DEF61BFC216E8B1000A738D4 /* FIRMessagingReceiverTest.m */,
DE9315CF1E8738B70083EDBF /* FIRMessagingRegistrarTest.m */,
DE9315D01E8738B70083EDBF /* FIRMessagingRemoteNotificationsProxyTest.m */,
DE9315D11E8738B70083EDBF /* FIRMessagingRmqManagerTest.m */,
Expand Down Expand Up @@ -4319,7 +4315,6 @@
511DD29D2225C8C40094D78D /* FIRMessagingLinkHandlingTest.m in Sources */,
511DD29E2225C8C40094D78D /* FIRMessagingPendingTopicsListTest.m in Sources */,
511DD29F2225C8C40094D78D /* FIRMessagingPubSubTest.m in Sources */,
511DD2A02225C8C40094D78D /* FIRMessagingReceiverTest.m in Sources */,
511DD2A12225C8C40094D78D /* FIRMessagingRegistrarTest.m in Sources */,
511DD2A22225C8C40094D78D /* FIRMessagingRemoteNotificationsProxyTest.m in Sources */,
511DD2A32225C8C40094D78D /* FIRMessagingRmqManagerTest.m in Sources */,
Expand Down Expand Up @@ -4941,7 +4936,6 @@
EDF5242C21EA37AA00BB24C6 /* FIRMessagingTestUtilities.m in Sources */,
DE9316031E8738E60083EDBF /* FIRMessagingSyncMessageManagerTest.m in Sources */,
DE9315FF1E8738E60083EDBF /* FIRMessagingRemoteNotificationsProxyTest.m in Sources */,
DEF61BFD216E8B1100A738D4 /* FIRMessagingReceiverTest.m in Sources */,
DE9315F81E8738E60083EDBF /* FIRMessagingDataMessageManagerTest.m in Sources */,
51284D16224ABD6A00274321 /* FIRMessagingExtensionHelperTest.m in Sources */,
DE9316051E8738E60083EDBF /* FIRMessagingTestNotificationUtilities.m in Sources */,
Expand Down
75 changes: 0 additions & 75 deletions Example/Messaging/Tests/FIRMessagingReceiverTest.m

This file was deleted.

11 changes: 1 addition & 10 deletions Firebase/Messaging/FIRMessaging.m
Expand Up @@ -319,7 +319,7 @@ - (void)setupNotificationListeners {
}

- (void)setupReceiver {
self.receiver = [[FIRMessagingReceiver alloc] initWithUserDefaults:self.messagingUserDefaults];
self.receiver = [[FIRMessagingReceiver alloc] init];
self.receiver.delegate = self;
}

Expand Down Expand Up @@ -659,15 +659,6 @@ - (void)notifyDelegateOfFCMTokenAvailability {
}
}


- (void)setUseMessagingDelegateForDirectChannel:(BOOL)useMessagingDelegateForDirectChannel {
self.receiver.useDirectChannel = useMessagingDelegateForDirectChannel;
}

- (BOOL)useMessagingDelegateForDirectChannel {
return self.receiver.useDirectChannel;
}

#pragma mark - Application State Changes

- (void)applicationStateChanged {
Expand Down
8 changes: 0 additions & 8 deletions Firebase/Messaging/FIRMessagingReceiver.h
Expand Up @@ -30,15 +30,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface FIRMessagingReceiver : NSObject <FIRMessagingDataMessageManagerDelegate>

/// Default initializer for creating the messaging receiver.
- (instancetype)initWithUserDefaults:(GULUserDefaults *)defaults NS_DESIGNATED_INITIALIZER;

/// Use `initWithUserDefaults:` instead.
- (instancetype)init NS_UNAVAILABLE;

@property(nonatomic, weak, nullable) id<FIRMessagingReceiverDelegate> delegate;
/// Whether to use direct channel for direct channel message callback handler in all iOS versions.
@property(nonatomic, assign) BOOL useDirectChannel;

@end

Expand Down
95 changes: 2 additions & 93 deletions Firebase/Messaging/FIRMessagingReceiver.m
Expand Up @@ -18,22 +18,13 @@

#import <UIKit/UIKit.h>

#import <GoogleUtilities/GULAppEnvironmentUtil.h>
#import <GoogleUtilities/GULUserDefaults.h>

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

static NSString *const kUpstreamMessageIDUserInfoKey = @"messageID";
static NSString *const kUpstreamErrorUserInfoKey = @"error";
/// "Should use Messaging delegate" key stored in NSUserDefaults
NSString *const kFIRMessagingUserDefaultsKeyUseMessagingDelegate =
@"com.firebase.messaging.useMessagingDelegate";
/// "Should use Messaging Delegate" key stored in Info.plist
NSString *const kFIRMessagingPlistUseMessagingDelegate =
@"FirebaseMessagingUseMessagingDelegateForDirectChannel";

static int downstreamMessageID = 0;

Expand All @@ -43,31 +34,14 @@ @interface FIRMessagingReceiver ()

@implementation FIRMessagingReceiver

#pragma mark - Initializer

- (instancetype)initWithUserDefaults:(GULUserDefaults *)defaults {
self = [super init];
if (self != nil) {
_defaults = defaults;
}
return self;
}

#pragma mark - FIRMessagingDataMessageManager protocol

- (void)didReceiveMessage:(NSDictionary *)message withIdentifier:(nullable NSString *)messageID {
if (![messageID length]) {
messageID = [[self class] nextMessageID];
}

NSInteger majorOSVersion = [[GULAppEnvironmentUtil systemVersion] integerValue];
if (majorOSVersion >= 10 || self.useDirectChannel) {
// iOS 10 and above or use direct channel is enabled.
[self scheduleIos10NotificationForMessage:message withIdentifier:messageID];
} else {
// Post notification directly to AppDelegate handlers. This is valid pre-iOS 10.
[self scheduleNotificationForMessage:message];
}
[self handleDirectChannelMessage:message withIdentifier:messageID];
}

- (void)willSendDataMessageWithID:(NSString *)messageID error:(NSError *)error {
Expand Down Expand Up @@ -112,83 +86,18 @@ - (void)didDeleteMessagesOnServer {
}

#pragma mark - Private Helpers
// As the new UserNotifications framework in iOS 10 doesn't support constructor/mutation for
// UNNotification object, FCM can't inject the message to the app with UserNotifications framework.
// Define our own protocol, which means app developers need to implement two interfaces to receive
// display notifications and data messages respectively for devices running iOS 10 or above. Devices
// running iOS 9 or below are not affected.
- (void)scheduleIos10NotificationForMessage:(NSDictionary *)message
withIdentifier:(NSString *)messageID {
- (void)handleDirectChannelMessage:(NSDictionary *)message withIdentifier:(NSString *)messageID {
FIRMessagingRemoteMessage *wrappedMessage = [[FIRMessagingRemoteMessage alloc] init];
// TODO: wrap title, body, badge and other fields
wrappedMessage.appData = [message copy];
wrappedMessage.messageID = messageID;
[self.delegate receiver:self receivedRemoteMessage:wrappedMessage];
}

- (void)scheduleNotificationForMessage:(NSDictionary *)message {
SEL newNotificationSelector =
@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:);
SEL oldNotificationSelector = @selector(application:didReceiveRemoteNotification:);

dispatch_async(dispatch_get_main_queue(), ^{
UIApplication *application = FIRMessagingUIApplication();
if (!application) {
return;
}
id<UIApplicationDelegate> appDelegate = [application delegate];
if ([appDelegate respondsToSelector:newNotificationSelector]) {
// Try the new remote notification callback
[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:application didReceiveRemoteNotification:message];
#pragma clang diagnostic pop
} else {
FIRMessagingLoggerError(kFIRMessagingMessageCodeReceiver005,
@"None of the remote notification callbacks implemented by "
@"UIApplicationDelegate");
}
});
}

+ (NSString *)nextMessageID {
@synchronized (self) {
++downstreamMessageID;
return [NSString stringWithFormat:@"gcm-%d", downstreamMessageID];
}
}

- (BOOL)useDirectChannel {
// Check storage
id shouldUseMessagingDelegate =
[_defaults objectForKey:kFIRMessagingUserDefaultsKeyUseMessagingDelegate];
if (shouldUseMessagingDelegate) {
return [shouldUseMessagingDelegate boolValue];
}

// Check Info.plist
shouldUseMessagingDelegate =
[[NSBundle mainBundle] objectForInfoDictionaryKey:kFIRMessagingPlistUseMessagingDelegate];
if (shouldUseMessagingDelegate) {
return [shouldUseMessagingDelegate boolValue];
}
// If none of above exists, we go back to default behavior which is NO.
return NO;
}

- (void)setUseDirectChannel:(BOOL)useDirectChannel {
BOOL shouldUseMessagingDelegate = [self useDirectChannel];
if (useDirectChannel != shouldUseMessagingDelegate) {
[_defaults setBool:useDirectChannel forKey:kFIRMessagingUserDefaultsKeyUseMessagingDelegate];
[_defaults synchronize];
}
}

@end
26 changes: 2 additions & 24 deletions Firebase/Messaging/Public/FIRMessaging.h
Expand Up @@ -266,15 +266,10 @@ NS_SWIFT_NAME(MessagingDelegate)
didReceiveRegistrationToken:(NSString *)fcmToken
NS_SWIFT_NAME(messaging(_:didReceiveRegistrationToken:));

/// This method is called on iOS 10+ devices to handle data messages received via FCM
/// direct channel (not via APNS). For iOS 9 and below, the direct channel data message
/// is handled by the UIApplicationDelegate's -application:didReceiveRemoteNotification: method.
/// You can enable all direct channel data messages to be delivered in FIRMessagingDelegate
/// by setting the flag `useMessagingDelegateForDirectMessages` to true.
/// Handle data messages received via FCM direct channel (not via APNS).
- (void)messaging:(FIRMessaging *)messaging
didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage
NS_SWIFT_NAME(messaging(_:didReceive:))
__IOS_AVAILABLE(10.0);
NS_SWIFT_NAME(messaging(_:didReceive:));

@end

Expand Down Expand Up @@ -309,23 +304,6 @@ NS_SWIFT_NAME(Messaging)
*/
@property(nonatomic, readonly) BOOL isDirectChannelEstablished;

/*
* Whether direct channel message should only use FIRMessagingDelegate messaging(_:didReceive:)
* for message delivery callback. The default value is false. If you need to change
* the default, set FirebaseMessagingUseMessagingDelegateForDirectChannel to true in
* your application’s Info.plist.
*
* If false, the message via direct channel for iOS 9 and below is still delivered in
* `-UIApplicationDelegate application(_:didReceiveRemoteNotification:fetchCompletionHandler:)`,
* and the FIRMessagingRemoteMessage object and its associated data will be unavailable.
* For iOS 10 and above, it is still delivered in `FIRMessagingDelegate messaging(_:didReceive:)`.
*
* If true, the data message sent by direct channel will be delivered via
* `FIRMessagingDelegate messaging(_:didReceive:)` and across all iOS versions.
*/
@property(nonatomic, assign) BOOL useMessagingDelegateForDirectChannel
__deprecated_msg("This is soon to be deprecated. All direct messages will by default delivered in `FIRMessagingDelegate messaging(_:didReceive:)` across all iOS versions");

/**
* FIRMessaging
*
Expand Down

0 comments on commit 3a93066

Please sign in to comment.