-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add support for the new FCM registration ID. #16133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
12a6ad8
c3c3d67
b442d42
fe49a75
f1294be
8c0ba25
b471f82
4b72a42
c28628b
e7aeac8
836b697
dff8587
503c5b6
c12d985
1d4c3f8
e984467
125dac3
d95a204
f7cea1d
fc874aa
26c94d2
6093f94
e7433b8
77ad7f5
fd64ad9
1c52ccb
befa3a4
c28c64d
36fa768
312dcac
95f16b4
ef6663f
3f2164b
0715193
f931f38
e5b0f5e
7d091ff
95ac9aa
e74e1a9
fd07733
2189024
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,9 @@ | |
| NSString *const kFIRMessagingPlistAutoInitEnabled = | ||
| @"FirebaseMessagingAutoInitEnabled"; // Auto Init Enabled key stored in Info.plist | ||
|
|
||
| NSString *const kFIRMessagingPlistInstallationIdEnabled = | ||
| @"FirebaseMessagingInstallationIdEnabled"; // Installation ID Enabled key stored in Info.plist | ||
|
|
||
| NSString *const FIRMessagingErrorDomain = @"com.google.fcm"; | ||
|
|
||
| BOOL FIRMessagingIsAPNSSyncMessage(NSDictionary *message) { | ||
|
|
@@ -232,7 +235,7 @@ - (void)didCompleteConfigure { | |
| // happens before developers able to set the delegate | ||
| // Hence first token set must be happen here after listener is set | ||
| // TODO(chliangGoogle) Need to investigate better solution. | ||
| [self updateDefaultFCMToken:self.FCMToken]; | ||
| [self updateDefaultFCMToken:[self.tokenManager tokenAndRequestIfNotExist]]; | ||
| }]; | ||
| } else if (self.isAutoInitEnabled && self.APNSToken) { | ||
| // When there is no cached token, must check auto init is enabled. | ||
|
|
@@ -493,7 +496,19 @@ - (void)setAutoInitEnabled:(BOOL)autoInitEnabled { | |
| } | ||
| } | ||
|
|
||
| - (BOOL)isInstallationIdEnabled { | ||
| id isInstallationIdEnabledObject = | ||
| [[NSBundle mainBundle] objectForInfoDictionaryKey:kFIRMessagingPlistInstallationIdEnabled]; | ||
| return [isInstallationIdEnabledObject boolValue]; | ||
| } | ||
|
leojaygoogle marked this conversation as resolved.
|
||
|
|
||
| - (NSString *)FCMToken { | ||
| if (self.isInstallationIdEnabled) { | ||
| FIRMessagingLoggerDebug( | ||
| kFIRMessagingMessageCodeInstallationIdEnabled, | ||
| @"FirebaseMessagingInstallationIdEnabled is set to YES, token is not available."); | ||
| return nil; | ||
| } | ||
| // Gets the current default token, and requests a new one if it doesn't exist. | ||
| NSString *token = [self.tokenManager tokenAndRequestIfNotExist]; | ||
| return token; | ||
|
|
@@ -617,14 +632,27 @@ - (void)setDelegate:(id<FIRMessagingDelegate>)delegate { | |
| // NOTE: Once |didReceiveRegistrationToken:| can be made a required method, this | ||
| // check can be removed. | ||
| - (void)validateDelegateConformsToTokenAvailabilityMethods { | ||
| if (self.delegate && | ||
| ![self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) { | ||
| FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented, | ||
| @"The object %@ does not respond to " | ||
| @"-messaging:didReceiveRegistrationToken:. Please implement " | ||
| @"-messaging:didReceiveRegistrationToken: to be provided with an FCM " | ||
| @"token.", | ||
| self.delegate.description); | ||
| if (self.delegate) { | ||
| if ([self isInstallationIdEnabled]) { | ||
| if (![self.delegate respondsToSelector:@selector(messaging:didReceiveRegistration:)]) { | ||
| FIRMessagingLoggerWarn( | ||
| kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented, | ||
| @"The object %@ does not respond to " | ||
| @"-messaging:didReceiveRegistration:. Please implement " | ||
| @"-messaging:didReceiveRegistration: to be provided with an installation ID.", | ||
| self.delegate.description); | ||
| } | ||
| } else { | ||
| if (![self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) { | ||
| FIRMessagingLoggerWarn( | ||
| kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented, | ||
| @"The object %@ does not respond to " | ||
| @"-messaging:didReceiveRegistrationToken:. Please implement " | ||
| @"-messaging:didReceiveRegistrationToken: to be provided with an FCM " | ||
| @"token.", | ||
| self.delegate.description); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -636,8 +664,14 @@ - (void)notifyRefreshedFCMToken { | |
| }); | ||
| return; | ||
| } | ||
| if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) { | ||
| [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken]; | ||
| if ([self isInstallationIdEnabled]) { | ||
| if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistration:)]) { | ||
| [self.delegate messaging:self didReceiveRegistration:self.tokenManager.defaultFCMToken]; | ||
| } | ||
| } else { | ||
| if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) { | ||
| [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken]; | ||
| } | ||
| } | ||
|
|
||
| // Should always trigger the token refresh notification when the delegate method is called | ||
|
|
@@ -646,6 +680,57 @@ - (void)notifyRefreshedFCMToken { | |
| object:self.tokenManager.defaultFCMToken]; | ||
| } | ||
|
|
||
| #pragma mark - FID | ||
|
|
||
| - (void)register { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should always trigger the Consider adding a unit test for this scenario.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. also added a test case. |
||
| if (!self.isInstallationIdEnabled) { | ||
| FIRMessagingLoggerError(kFIRMessagingMessageCodeInstallationIdDisabled, | ||
| @"FirebaseMessagingInstallationIdEnabled is not set to YES, so " | ||
| @"FID operations are not supported."); | ||
| return; | ||
| } | ||
| if (!FIRApp.defaultApp.options.GCMSenderID.length) { | ||
| FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenFetch, | ||
| @"No Sender ID is available to register"); | ||
| return; | ||
| } | ||
| [self.tokenManager tokenAndRequestIfNotExist]; | ||
| } | ||
|
|
||
| - (void)unregister { | ||
| if (!self.isInstallationIdEnabled) { | ||
| FIRMessagingLoggerError(kFIRMessagingMessageCodeInstallationIdDisabled, | ||
| @"FirebaseMessagingInstallationIdEnabled is not set to YES, so " | ||
| @"FID operations are not supported."); | ||
| return; | ||
| } | ||
| NSString *senderID = FIRApp.defaultApp.options.GCMSenderID; | ||
| if (!senderID.length) { | ||
| FIRMessagingLoggerError(kFIRMessagingMessageCodeSenderIDNotSuppliedForTokenDelete, | ||
| @"No Sender ID is available to unregister"); | ||
| return; | ||
| } | ||
|
|
||
| FIRMessaging_WEAKIFY(self); | ||
| [self.installations installationIDWithCompletion:^(NSString *_Nullable identifier, | ||
| NSError *_Nullable error) { | ||
| FIRMessaging_STRONGIFY(self); | ||
| if (error) { | ||
| FIRMessagingLoggerError(kFIRMessagingMessageCodeTokenOperationInstallationIdNotAvailable, | ||
| @"Failed to get installation ID."); | ||
| } else { | ||
| [self.tokenManager deleteTokenWithAuthorizedEntity:senderID | ||
| scope:kFIRMessagingDefaultTokenScope | ||
| instanceID:identifier | ||
| handler:^(NSError *_Nullable error) { | ||
| if (!error && [self isAutoInitEnabled]) { | ||
| [self.tokenManager tokenAndRequestIfNotExist]; | ||
| } | ||
|
leojaygoogle marked this conversation as resolved.
Outdated
|
||
| }]; | ||
| } | ||
| }]; | ||
| } | ||
|
|
||
| #pragma mark - Topics | ||
|
|
||
| + (NSString *)normalizeTopic:(NSString *)topic { | ||
|
|
@@ -814,8 +899,14 @@ - (void)notifyDelegateOfFCMTokenAvailability { | |
| }); | ||
| return; | ||
| } | ||
| if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) { | ||
| [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken]; | ||
| if ([self isInstallationIdEnabled]) { | ||
| if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistration:)]) { | ||
| [self.delegate messaging:self didReceiveRegistration:self.tokenManager.defaultFCMToken]; | ||
| } | ||
| } else { | ||
| if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) { | ||
| [self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken]; | ||
| } | ||
| } | ||
| // Should always trigger the token refresh notification when the delegate method is called | ||
| NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | ||
|
|
||
|
leojaygoogle marked this conversation as resolved.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #import "FirebaseMessaging/Sources/Token/FIRMessagingTokenOperation.h" | ||
|
|
||
| @class FIRInstallations; | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| @interface FIRMessagingFIDRegisterOperation : FIRMessagingTokenOperation | ||
|
|
||
| - (instancetype)initWithAuthorizedEntity:(NSString *)authorizedEntity | ||
| scope:(NSString *)scope | ||
| options:(nullable NSDictionary<NSString *, NSString *> *)options | ||
| instanceID:(NSString *)instanceID | ||
| heartbeatLogger:(id<FIRHeartbeatLoggerProtocol>)heartbeatLogger | ||
| installations:(FIRInstallations *)installations; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
Uh oh!
There was an error while loading. Please reload this page.