Skip to content

Commit 783b507

Browse files
authored
[Messaging] Fix build issues on visionOS (#11483)
Fixed Firebase Messaging build issues in `FIRMessaging.m` and `FIRMessagingContextManagerService.m` when targeting visionOS. Also cleaned up extraneous `@availability` checks for platforms below the current minimum deployment target in these files.
1 parent 56cbd2d commit 783b507

File tree

3 files changed

+20
-66
lines changed

3 files changed

+20
-66
lines changed

FirebaseMessaging/Sources/FIRMessaging.m

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,11 @@ - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message {
420420
SEL openURLWithOptionsSelector = @selector(application:openURL:options:);
421421
SEL openURLWithSourceApplicationSelector = @selector(application:
422422
openURL:sourceApplication:annotation:);
423-
#if TARGET_OS_IOS
423+
// TODO(Xcode 15): When Xcode 15 is the minimum supported Xcode version, it will be unnecessary to
424+
// check if `TARGET_OS_XR` is defined.
425+
#if TARGET_OS_IOS && (!defined(TARGET_OS_XR) || !TARGET_OS_XR)
424426
SEL handleOpenURLSelector = @selector(application:handleOpenURL:);
425-
#endif
427+
#endif // TARGET_OS_IOS && (!defined(TARGET_OS_XR) || !TARGET_OS_XR)
426428
// Due to FIRAAppDelegateProxy swizzling, this selector will most likely get chosen, whether or
427429
// not the actual application has implemented
428430
// |application:continueUserActivity:restorationHandler:|. A warning will be displayed to the user
@@ -439,14 +441,13 @@ - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message {
439441
}];
440442

441443
} else if ([appDelegate respondsToSelector:openURLWithOptionsSelector]) {
442-
#pragma clang diagnostic push
443-
#pragma clang diagnostic ignored "-Wunguarded-availability"
444444
[appDelegate application:application openURL:url options:@{}];
445-
#pragma clang diagnostic pop
446445
// Similarly, |application:openURL:sourceApplication:annotation:| will also always be called,
447446
// due to the default swizzling done by FIRAAppDelegateProxy in Firebase Analytics
448447
} else if ([appDelegate respondsToSelector:openURLWithSourceApplicationSelector]) {
449-
#if TARGET_OS_IOS
448+
// TODO(Xcode 15): When Xcode 15 is the minimum supported Xcode version, it will be unnecessary to
449+
// check if `TARGET_OS_XR` is defined.
450+
#if TARGET_OS_IOS && (!defined(TARGET_OS_XR) || !TARGET_OS_XR)
450451
#pragma clang diagnostic push
451452
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
452453
[appDelegate application:application
@@ -459,9 +460,9 @@ - (void)handleIncomingLinkIfNeededFromMessage:(NSDictionary *)message {
459460
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
460461
[appDelegate application:application handleOpenURL:url];
461462
#pragma clang diagnostic pop
462-
#endif
463+
#endif // TARGET_OS_IOS && (!defined(TARGET_OS_XR) || !TARGET_OS_XR)
463464
}
464-
#endif
465+
#endif // TARGET_OS_IOS || TARGET_OS_TV
465466
}
466467

467468
- (NSURL *)linkURLFromMessage:(NSDictionary *)message {

FirebaseMessaging/Sources/FIRMessagingContextManagerService.m

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -209,57 +209,10 @@ + (UNMutableNotificationContent *)contentFromContextualMessage:(NSDictionary *)m
209209
}
210210

211211
+ (void)scheduleLocalNotificationForMessage:(NSDictionary *)message atDate:(NSDate *)date {
212-
if (@available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *)) {
212+
if (@available(macOS 10.14, *)) {
213213
[self scheduleiOS10LocalNotificationForMessage:message atDate:date];
214214
return;
215215
}
216-
#if TARGET_OS_IOS
217-
NSDictionary *apsDictionary = message;
218-
#pragma clang diagnostic push
219-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
220-
UILocalNotification *notification = [[UILocalNotification alloc] init];
221-
#pragma clang diagnostic pop
222-
223-
// A great way to understand timezones and UILocalNotifications
224-
// http://stackoverflow.com/questions/18424569/understanding-uilocalnotification-timezone
225-
notification.timeZone = [NSTimeZone defaultTimeZone];
226-
notification.fireDate = date;
227-
228-
// In the current solution all of the display stuff goes into a special "aps" dictionary
229-
// being sent in the message.
230-
if ([apsDictionary[kFIRMessagingContextManagerBodyKey] length]) {
231-
notification.alertBody = apsDictionary[kFIRMessagingContextManagerBodyKey];
232-
}
233-
if (@available(iOS 8.2, *)) {
234-
if ([apsDictionary[kFIRMessagingContextManagerTitleKey] length]) {
235-
notification.alertTitle = apsDictionary[kFIRMessagingContextManagerTitleKey];
236-
}
237-
}
238-
239-
if (apsDictionary[kFIRMessagingContextManagerSoundKey]) {
240-
notification.soundName = apsDictionary[kFIRMessagingContextManagerSoundKey];
241-
}
242-
if (apsDictionary[kFIRMessagingContextManagerBadgeKey]) {
243-
notification.applicationIconBadgeNumber =
244-
[apsDictionary[kFIRMessagingContextManagerBadgeKey] integerValue];
245-
}
246-
if (apsDictionary[kFIRMessagingContextManagerCategoryKey]) {
247-
notification.category = apsDictionary[kFIRMessagingContextManagerCategoryKey];
248-
}
249-
250-
NSDictionary *userInfo = [self parseDataFromMessage:message];
251-
if (userInfo.count) {
252-
notification.userInfo = userInfo;
253-
}
254-
UIApplication *application = [GULAppDelegateSwizzler sharedApplication];
255-
if (!application) {
256-
return;
257-
}
258-
#pragma clang diagnostic push
259-
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
260-
[application scheduleLocalNotification:notification];
261-
#pragma clang diagnostic pop
262-
#endif
263216
}
264217

265218
+ (NSDictionary *)parseDataFromMessage:(NSDictionary *)message {

FirebaseMessaging/Tests/UnitTests/FIRMessagingContextManagerServiceTest.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636

3737
@interface FIRMessagingContextManagerService (ExposedForTest)
3838
+ (void)scheduleiOS10LocalNotificationForMessage:(NSDictionary *)message atDate:(NSDate *)date;
39-
+ (UNMutableNotificationContent *)contentFromContextualMessage:(NSDictionary *)message;
39+
+ (UNMutableNotificationContent *)contentFromContextualMessage:(NSDictionary *)message
40+
API_AVAILABLE(macos(10.14));
4041
@end
4142

4243
API_AVAILABLE(macos(10.14))
4344
@interface FIRMessagingContextManagerServiceTest : XCTestCase
4445

4546
@property(nonatomic, readwrite, strong) NSDateFormatter *dateFormatter;
4647
@property(nonatomic, readwrite, strong) NSMutableArray *scheduledLocalNotifications;
47-
@property(nonatomic, readwrite, strong)
48-
NSMutableArray<UNNotificationRequest *> *requests API_AVAILABLE(ios(10.0), macos(10.4));
48+
@property(nonatomic, readwrite, strong) NSMutableArray<UNNotificationRequest *> *requests;
4949

5050
@end
5151

@@ -57,7 +57,7 @@ - (void)setUp {
5757
self.dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
5858
[self.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
5959
self.scheduledLocalNotifications = [[NSMutableArray alloc] init];
60-
if (@available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *)) {
60+
if (@available(macOS 10.14, *)) {
6161
self.requests = [[NSMutableArray alloc] init];
6262
}
6363

@@ -104,7 +104,7 @@ - (void)testMessageWithFutureStartTime {
104104
};
105105
XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
106106

107-
if (@available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *)) {
107+
if (@available(macOS 10.14, *)) {
108108
XCTAssertEqual(self.requests.count, 1);
109109
UNNotificationRequest *request = self.requests.firstObject;
110110
XCTAssertEqualObjects(request.identifier, kMessageIdentifierValue);
@@ -146,7 +146,7 @@ - (void)testMessageWithPastEndTime {
146146
};
147147

148148
XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
149-
if (@available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *)) {
149+
if (@available(macOS 10.14, *)) {
150150
XCTAssertEqual(self.requests.count, 0);
151151
return;
152152
}
@@ -176,7 +176,7 @@ - (void)testMessageWithPastStartAndFutureEndTime {
176176

177177
XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
178178

179-
if (@available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *)) {
179+
if (@available(macOS 10.14, *)) {
180180
XCTAssertEqual(self.requests.count, 1);
181181
UNNotificationRequest *request = self.requests.firstObject;
182182
XCTAssertEqualObjects(request.identifier, kMessageIdentifierValue);
@@ -216,7 +216,7 @@ - (void)testTimedNotificationsUserInfo {
216216
};
217217

218218
XCTAssertTrue([FIRMessagingContextManagerService handleContextManagerMessage:message]);
219-
if (@available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *)) {
219+
if (@available(macOS 10.14, *)) {
220220
XCTAssertEqual(self.requests.count, 1);
221221
UNNotificationRequest *request = self.requests.firstObject;
222222
XCTAssertEqualObjects(request.identifier, kMessageIdentifierValue);
@@ -278,7 +278,7 @@ - (void)mockSchedulingLocalNotifications {
278278
}
279279

280280
- (void)testScheduleiOS10LocalNotification {
281-
if (@available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *)) {
281+
if (@available(macOS 10.14, *)) {
282282
id mockContextManagerService = OCMClassMock([FIRMessagingContextManagerService class]);
283283
NSDictionary *message = @{};
284284

@@ -290,7 +290,7 @@ - (void)testScheduleiOS10LocalNotification {
290290
}
291291

292292
- (void)testContentFromConetxtualMessage {
293-
if (@available(macOS 10.14, iOS 10.0, watchOS 3.0, tvOS 10.0, *)) {
293+
if (@available(macOS 10.14, *)) {
294294
NSDictionary *message = @{
295295
@"aps" : @{@"content-available" : @1},
296296
@"gcm.message_id" : @1623702615599207,

0 commit comments

Comments
 (0)