diff options
author | Chen Liang <chliang@google.com> | 2018-01-24 13:15:28 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-24 13:15:28 -0800 |
commit | 0481e1e27f2b62382f126fa422618426af2be3fb (patch) | |
tree | 3544f4504c196bf1244a527596a65f66a9f43d30 | |
parent | 8f0c3254632db38ad1dd040b395aaf8f55042fd9 (diff) | |
parent | fa81c488551575d65fd681e08ff417f354f54e75 (diff) |
Merge pull request #699 from firebase/messaging-copy
Adding enable/disable property to FCM token auto Initialization.
-rw-r--r-- | Firebase/Messaging/FIRMessaging.m | 41 | ||||
-rw-r--r-- | Firebase/Messaging/FIRMessaging_Private.h | 3 | ||||
-rw-r--r-- | Firebase/Messaging/Public/FIRMessaging.h | 15 |
3 files changed, 56 insertions, 3 deletions
diff --git a/Firebase/Messaging/FIRMessaging.m b/Firebase/Messaging/FIRMessaging.m index 85fd185..11cacb3 100644 --- a/Firebase/Messaging/FIRMessaging.m +++ b/Firebase/Messaging/FIRMessaging.m @@ -70,6 +70,14 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification = @"com.firebase.messaging.notif.fcm-token-refreshed"; #endif // defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 +NSString *const kFIRMessagingUserDefaultsKeyAutoInitEnabled = + @"com.firebase.messaging.auto-init.enabled"; // Auto Init Enabled key stored in NSUserDefaults +NSString *const kFIRMessagingSuiteName = + @"com.firebase.messaging.user_defaults"; // Suite name for NSUserDefaults + +static NSString *const kFIRMessagingPlistAutoInitEnabled = + @"FirebaseMessagingAutoInitEnabled"; // Auto Init Enabled key stored in Info.plist + // Copied from Apple's header in case it is missing in some cases (e.g. pre-Xcode 8 builds). #ifndef NSFoundationVersionNumber_iOS_8_x_Max #define NSFoundationVersionNumber_iOS_8_x_Max 1199 @@ -120,8 +128,8 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification = @end -@interface FIRMessaging () - <FIRMessagingClientDelegate, FIRMessagingReceiverDelegate, FIRReachabilityDelegate> +@interface FIRMessaging ()<FIRMessagingClientDelegate, FIRMessagingReceiverDelegate, + FIRReachabilityDelegate> // FIRApp properties @property(nonatomic, readwrite, copy) NSString *fcmSenderID; @@ -141,6 +149,7 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification = @property(nonatomic, readwrite, strong) FIRMessagingRmqManager *rmq2Manager; @property(nonatomic, readwrite, strong) FIRMessagingReceiver *receiver; @property(nonatomic, readwrite, strong) FIRMessagingSyncMessageManager *syncMessageManager; +@property(nonatomic, readwrite, strong) NSUserDefaults *messagingUserDefaults; /// Message ID's logged for analytics. This prevents us from logging the same message twice /// which can happen if the user inadvertently calls `appDidReceiveMessage` along with us @@ -166,6 +175,7 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification = if (self) { _loggedMessageIDs = [NSMutableSet set]; _instanceIDProxy = [[FIRMessagingInstanceIDProxy alloc] init]; + _messagingUserDefaults = [[NSUserDefaults alloc] initWithSuiteName:kFIRMessagingSuiteName]; } return self; } @@ -451,6 +461,33 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification = #pragma mark - FCM +- (BOOL)isAutoInitEnabled { + // Check storage + id isAutoInitEnabledObject = + [_messagingUserDefaults objectForKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled]; + if (isAutoInitEnabledObject) { + return [isAutoInitEnabledObject boolValue]; + } + + // Check Info.plist + isAutoInitEnabledObject = + [[NSBundle mainBundle] objectForInfoDictionaryKey:kFIRMessagingPlistAutoInitEnabled]; + if (isAutoInitEnabledObject) { + return [isAutoInitEnabledObject boolValue]; + } + // If none of above exists, we default assume FCM auto init is enabled. + return YES; +} + +- (void)setAutoInitEnabled:(BOOL)autoInitEnabled { + BOOL isFCMAutoInitEnabled = [self isAutoInitEnabled]; + [_messagingUserDefaults setBool:autoInitEnabled + forKey:kFIRMessagingUserDefaultsKeyAutoInitEnabled]; + if (!isFCMAutoInitEnabled && autoInitEnabled) { + self.defaultFcmToken = [self FCMToken]; + } +} + - (NSString *)FCMToken { NSString *token = self.defaultFcmToken; if (!token) { diff --git a/Firebase/Messaging/FIRMessaging_Private.h b/Firebase/Messaging/FIRMessaging_Private.h index 0c35179..135f091 100644 --- a/Firebase/Messaging/FIRMessaging_Private.h +++ b/Firebase/Messaging/FIRMessaging_Private.h @@ -25,6 +25,9 @@ typedef NS_ENUM(int8_t, FIRMessagingNetworkStatus) { kFIRMessagingReachabilityReachableViaWWAN, }; +FOUNDATION_EXPORT NSString *const kFIRMessagingUserDefaultsKeyAutoInitEnabled; +FOUNDATION_EXPORT NSString *const kFIRMessagingSuiteName; + @interface FIRMessagingRemoteMessage () @property(nonatomic, strong) NSDictionary *appData; diff --git a/Firebase/Messaging/Public/FIRMessaging.h b/Firebase/Messaging/Public/FIRMessaging.h index 7cd7b19..33dd596 100644 --- a/Firebase/Messaging/Public/FIRMessaging.h +++ b/Firebase/Messaging/Public/FIRMessaging.h @@ -289,7 +289,6 @@ NS_SWIFT_NAME(Messaging) */ @property(nonatomic, weak, nullable) id<FIRMessagingDelegate> delegate; - /** * Delegate to handle remote data messages received via FCM for devices running iOS 10 or above. */ @@ -354,6 +353,20 @@ NS_SWIFT_NAME(Messaging) #pragma mark - FCM Tokens /** + * Is Firebase Messaging token auto generation enabled? If this flag is disabled, + * Firebase Messaging will not generate token automatically for message delivery. + * + * This setting is persisted, and is applied on future + * invocations of your application. Once explicitly set, it overrides any + * settings in your Info.plist. + * + * By default, FCM automatic initialization is enabled. If you need to change the + * default (for example, because you want to prompt the user before getting token) + * set FirebaseMessagingAutoInitEnabled to false in your application's Info.plist. + */ +@property(nonatomic, assign, getter=isAutoInitEnabled) BOOL autoInitEnabled; + +/** * The FCM token is used to identify this device so that FCM can send notifications to it. * It is associated with your APNS token when the APNS token is supplied, so that sending * messages to the FCM token will be delivered over APNS. |