Skip to content

Commit 277e17a

Browse files
committed
support notifications in FCM for tvos
1 parent eaff93f commit 277e17a

File tree

1 file changed

+49
-16
lines changed

1 file changed

+49
-16
lines changed

messaging/src/ios/messaging.mm

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,26 +196,58 @@ void NotifyListenerSet(Listener* listener) {
196196
LogInfo("FCM: Using FCM senderID %s", senderID.UTF8String);
197197
id appDelegate = [UIApplication sharedApplication];
198198

199-
#if FIREBASE_PLATFORM_IOS
199+
#if FIREBASE_PLATFORM_IOS || FIREBASE_PLATFORM_TVOS
200200
// Register for remote notifications. Both codepaths result in
201201
// application:didRegisterForRemoteNotificationsWithDeviceToken: being called when they
202202
// complete, or application:didFailToRegisterForRemoteNotificationsWithError: if there was an
203203
// error. We complete the future there.
204-
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
205-
// iOS 7.1 or earlier
206-
UIRemoteNotificationType allNotificationTypes =
207-
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert |
208-
UIRemoteNotificationTypeBadge);
209-
[appDelegate registerForRemoteNotificationTypes:allNotificationTypes];
210-
} else {
211-
// iOS 8 or later
212-
UIUserNotificationType allNotificationTypes =
213-
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
214-
UIUserNotificationSettings *settings =
215-
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
216-
[appDelegate registerUserNotificationSettings:settings];
204+
// if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
205+
// // iOS 7.1 or earlier
206+
// UIRemoteNotificationType allNotificationTypes =
207+
// (UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert |
208+
// UIRemoteNotificationTypeBadge);
209+
// [appDelegate registerForRemoteNotificationTypes:allNotificationTypes];
210+
// } else {
211+
// // iOS 8 or later
212+
// UIUserNotificationType allNotificationTypes =
213+
// (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
214+
// UIUserNotificationSettings *settings =
215+
// [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
216+
// [appDelegate registerUserNotificationSettings:settings];
217+
// [appDelegate registerForRemoteNotifications];
218+
// }
219+
220+
if ([UNUserNotificationCenter class] != nil) {
221+
// iOS 10 or later, and tvOS
222+
// For iOS 10 display notification (sent via APNS)
223+
[UNUserNotificationCenter currentNotificationCenter].delegate = appDelegate;
224+
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
225+
UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
226+
[[UNUserNotificationCenter currentNotificationCenter]
227+
requestAuthorizationWithOptions:authOptions
228+
completionHandler:^(BOOL granted, NSError * _Nullable error) {
229+
// ...
230+
}];
217231
[appDelegate registerForRemoteNotifications];
218232
}
233+
#endif
234+
235+
#if FIREBASE_PLATFORM_IOS
236+
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
237+
// iOS 7.1 or earlier
238+
UIRemoteNotificationType allNotificationTypes =
239+
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert |
240+
UIRemoteNotificationTypeBadge);
241+
[appDelegate registerForRemoteNotificationTypes:allNotificationTypes];
242+
} else if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_10_0) {
243+
// iOS 8 or later
244+
UIUserNotificationType allNotificationTypes =
245+
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
246+
UIUserNotificationSettings *settings =
247+
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
248+
[appDelegate registerUserNotificationSettings:settings];
249+
[appDelegate registerForRemoteNotifications];
250+
}
219251
#endif // FIREBASE_PLATFORM_IOS
220252

221253
// Only request the token automatically if permitted
@@ -524,11 +556,13 @@ static BOOL AppDelegateApplicationDidFinishLaunchingWithOptions(id self, SEL sel
524556
[user_notification_center setDelegate:(id<UNUserNotificationCenterDelegate>)application];
525557
}
526558

527-
#if FIREBASE_PLATFORM_IOS
528559
// If the app was launched with a notification, cache it until we're connected.
560+
g_message_notification_opened = false;
561+
#if FIREBASE_PLATFORM_IOS
529562
g_launch_notification =
530563
[launch_options objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
531564
g_message_notification_opened = g_launch_notification != nil;
565+
#endif
532566

533567
IMP app_delegate_application_did_finish_launching_with_options =
534568
SwizzledMethodCache().GetMethodForObject(
@@ -551,7 +585,6 @@ static BOOL AppDelegateApplicationDidFinishLaunchingWithOptions(id self, SEL sel
551585
[invocation getReturnValue:&ret];
552586
return ret;
553587
}
554-
#endif // FIREBASE_PLATFORM_IOS
555588
return NO;
556589
}
557590

0 commit comments

Comments
 (0)