Skip to content

Commit 75cc4df

Browse files
[Messaging] - Decline Registration when there is no APNS Token (#10590)
* [Messaging] - Decline Registration when there is no APNS Token
1 parent e3d7278 commit 75cc4df

File tree

7 files changed

+41
-1
lines changed

7 files changed

+41
-1
lines changed

Diff for: FirebaseMessaging/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 10.4.0
2+
- [changed] On app startup, an APNS Token must be provided to FCM SDK before retrieving an FCM Token otherwise an error will be returned as part of the completion.
3+
14
# 10.3.0
25
- [changed] Allow notification support on iOS 16 Simulator on Xcode 14 (#9968) (Reference: Xcode 14 Release Notes -> Simulator -> New Features: https://developer.apple.com/documentation/xcode-release-notes/xcode-14-release-notes)
36

Diff for: FirebaseMessaging/Sources/FIRMessaging.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ - (void)retrieveFCMTokenForSenderID:(nonnull NSString *)senderID
578578
} else {
579579
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeAPNSTokenNotAvailableDuringTokenFetch,
580580
@"APNS device token not set before retrieving FCM Token for Sender ID "
581-
@"'%@'. Notifications to this FCM Token will not be delivered over APNS."
581+
@"'%@'."
582582
@"Be sure to re-retrieve the FCM token once the APNS device token is "
583583
@"set.",
584584
senderID);

Diff for: FirebaseMessaging/Sources/NSError+FIRMessaging.h

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef NS_ENUM(NSUInteger, FIRMessagingErrorCode) {
3636
kFIRMessagingErrorCodeMissingAuthorizedEntity = 502,
3737
kFIRMessagingErrorCodeMissingScope = 503,
3838
kFIRMessagingErrorCodeMissingFid = 504,
39+
kFIRMessagingErrorCodeMissingDeviceToken = 505,
3940

4041
// Upstream send errors
4142
kFIRMessagingErrorCodeServiceNotAvailable = 1001,

Diff for: FirebaseMessaging/Sources/Token/FIRMessagingTokenManager.m

+14
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,20 @@ - (void)tokenWithAuthorizedEntity:(NSString *)authorizedEntity
180180
[tokenOptions addEntriesFromDictionary:options];
181181
}
182182

183+
// ensure we have an APNS Token
184+
if (tokenOptions[kFIRMessagingTokenOptionsAPNSKey] == nil) {
185+
// we don't have an APNS token. Don't fetch or return a FCM Token
186+
FIRMessagingLoggerWarn(kFIRMessagingMessageCodeAPNSTokenNotAvailableDuringTokenFetch,
187+
@"Declining request for FCM Token since no APNS Token specified");
188+
dispatch_async(dispatch_get_main_queue(), ^{
189+
NSError *missingAPNSTokenError =
190+
[NSError messagingErrorWithCode:kFIRMessagingErrorCodeMissingDeviceToken
191+
failureReason:@"No APNS token specified before fetching FCM Token"];
192+
handler(nil, missingAPNSTokenError);
193+
});
194+
return;
195+
}
196+
183197
#if TARGET_OS_SIMULATOR && TARGET_OS_IOS
184198
if (tokenOptions[kFIRMessagingTokenOptionsAPNSKey] != nil) {
185199
// If APNS token is available on iOS Simulator, we must use the sandbox profile

Diff for: FirebaseMessaging/Tests/IntegrationTests/FIRMessagingPubSubTest.swift

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
override func setUpWithError() throws {
3737
messaging = try XCTUnwrap(Messaging.messaging())
38+
// fake APNS Token
39+
messaging.apnsToken = "eb706b132b2f9270faac751e4ceab283f1803b729ac1dd399db3fd2a98bb101b"
40+
.data(using: .utf8)
3841
}
3942

4043
override func tearDown() {

Diff for: FirebaseMessaging/Tests/IntegrationTests/FIRMessagingTokenRefreshTests.swift

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444

4545
override func setUpWithError() throws {
4646
messaging = try XCTUnwrap(Messaging.messaging())
47+
// fake APNS Token
48+
messaging.apnsToken = "eb706b132b2f9270faac751e4ceab283f1803b729ac1dd399db3fd2a98bb101b"
49+
.data(using: .utf8)
4750
}
4851

4952
override func tearDown() {

Diff for: FirebaseMessaging/Tests/UnitTests/FIRMessagingTest.m

+16
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ - (void)testReturnsErrorWhenFetchingTokenWithoutSenderID {
206206
[self waitForExpectationsWithTimeout:0.1 handler:nil];
207207
}
208208

209+
- (void)testReturnsErrorWhenFetchingTokenWithoutAPNSToken {
210+
XCTestExpectation *expectation =
211+
[self expectationWithDescription:@"Returned an error fetching token without APNS Token"];
212+
#pragma clang diagnostic push
213+
#pragma clang diagnostic ignored "-Wnonnull"
214+
[self.messaging
215+
retrieveFCMTokenForSenderID:@"12345"
216+
completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error) {
217+
if (error != nil) {
218+
[expectation fulfill];
219+
}
220+
}];
221+
#pragma clang diagnostic pop
222+
[self waitForExpectationsWithTimeout:0.1 handler:nil];
223+
}
224+
209225
- (void)testReturnsErrorWhenFetchingTokenWithEmptySenderID {
210226
XCTestExpectation *expectation =
211227
[self expectationWithDescription:@"Returned an error fetching token with empty Sender ID"];

0 commit comments

Comments
 (0)