Skip to content

Commit

Permalink
feat(apple): build framework for tvos and watchos (+ simulators)
Browse files Browse the repository at this point in the history
these framework artifacts are untested but do in general seem in harmony with
the tvOS and watchOS platforms after the #if/#endif precompiler removal of areas
those OSs don't support (tvOS just appears to show a badge and allow data payloads
and that's it, for instance, watchOS doesn't have settings)
  • Loading branch information
mikehardy committed Jan 6, 2021
1 parent 66a640e commit 03b92ed
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 18 deletions.
2 changes: 2 additions & 0 deletions ios/NotifeeCore/NotifeeCore+NSNotificationCenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ - (void)observe {
NotifeeCoreNSNotificationCenter *strongSelf = weakSelf;
// Application
// ObjC -> Initialize other delegates & observers
#if !TARGET_OS_WATCH
[[NSNotificationCenter defaultCenter]
addObserver:strongSelf
selector:@selector(application_onDidFinishLaunchingNotification:)
name:UIApplicationDidFinishLaunchingNotification
object:nil];
#endif
[[NSNotificationCenter defaultCenter]
addObserver:strongSelf
selector:@selector(messaging_didReceiveRemoteNotification:)
Expand Down
22 changes: 18 additions & 4 deletions ios/NotifeeCore/NotifeeCore+UNUserNotificationCenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:
(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSDictionary *notifeeNotification =
notification.request.content.userInfo[kNotifeeUserInfoNotification];
NSDictionary *notifeeNotification = nil;
#if !TARGET_OS_TV
notifeeNotification = notification.request.content.userInfo[kNotifeeUserInfoNotification];
#endif

// we only care about notifications created through notifee
if (notifeeNotification != nil) {
Expand All @@ -95,7 +97,11 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
presentationOptions |= UNNotificationPresentationOptionAlert;
}

NSDictionary *notifeeTrigger = notification.request.content.userInfo[kNotifeeUserInfoTrigger];
NSDictionary *notifeeTrigger = nil;
#if !TARGET_OS_TV
notifeeTrigger = notification.request.content.userInfo[kNotifeeUserInfoTrigger];
#endif

if (notifeeTrigger != nil) {
// post DELIVERED event
[[NotifeeCoreDelegateHolder instance] didReceiveNotifeeCoreEvent:@{
Expand All @@ -118,6 +124,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
// The method will be called when the user responded to the notification by opening the application,
// dismissing the notification or choosing a UNNotificationAction. The delegate must be set before
// the application returns from application:didFinishLaunchingWithOptions:.
#if !TARGET_OS_TV
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler {
Expand Down Expand Up @@ -188,12 +195,19 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
withCompletionHandler:completionHandler];
}
}
#endif

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
openSettingsForNotification:(nullable UNNotification *)notification {
#if !TARGET_OS_TV && !TARGET_OS_WATCH
if (_originalDelegate != nil && originalUNCDelegateRespondsTo.openSettingsForNotification) {
[_originalDelegate userNotificationCenter:center openSettingsForNotification:notification];
if (@available(iOS 12.0, *)) {
[_originalDelegate userNotificationCenter:center openSettingsForNotification:notification];
} else {
// Fallback on earlier versions
}
}
#endif
}

@end
56 changes: 49 additions & 7 deletions ios/NotifeeCore/NotifeeCore.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ + (void)cancelNotification:(NSString *)notificationId withNotificationType: (NSI
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
// cancel displayed notification
if (notificationType == NotifeeCoreNotificationTypeDisplayed ||
notificationType == NotifeeCoreNotificationTypeAll)
notificationType == NotifeeCoreNotificationTypeAll) {
#if !TARGET_OS_TV
[center removeDeliveredNotificationsWithIdentifiers:@[ notificationId ]];
#endif
}

// cancel trigger notification
if (notificationType == NotifeeCoreNotificationTypeTrigger ||
Expand All @@ -52,8 +55,11 @@ + (void)cancelAllNotifications:(NSInteger)notificationType withBlock:(notifeeMet

// cancel displayed notifications
if (notificationType == NotifeeCoreNotificationTypeDisplayed ||
notificationType == NotifeeCoreNotificationTypeAll)
notificationType == NotifeeCoreNotificationTypeAll) {
#if !TARGET_OS_TV
[center removeAllDeliveredNotifications];
#endif
}

// cancel trigger notifications
if (notificationType == NotifeeCoreNotificationTypeTrigger ||
Expand Down Expand Up @@ -159,6 +165,10 @@ + (UNMutableNotificationContent *)buildNotificationContent:(NSDictionary *)notif
NSDictionary *iosDict = notification[@"ios"];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];

// badgeCount - nil is an acceptable value so no need to check key existence
content.badge = iosDict[@"badgeCount"];

#if !TARGET_OS_TV
// title
if (notification[@"title"] != nil) {
content.title = notification[@"title"];
Expand All @@ -185,9 +195,6 @@ + (UNMutableNotificationContent *)buildNotificationContent:(NSDictionary *)notif

content.userInfo = userInfo;

// badgeCount - nil is an acceptable value so no need to check key existence
content.badge = iosDict[@"badgeCount"];

// categoryId
if (iosDict[@"categoryId"] != nil) {
content.categoryIdentifier = iosDict[@"categoryId"];
Expand All @@ -205,7 +212,9 @@ + (UNMutableNotificationContent *)buildNotificationContent:(NSDictionary *)notif
NSNumber *criticalSoundVolume = iosDict[@"criticalVolume"];
NSString *soundName = iosDict[@"sound"] != nil ? iosDict[@"sound"] : @"default";

#if !TARGET_OS_WATCH
if ([soundName isEqualToString:@"default"]) {
#endif
if (criticalSound) {
if (@available(iOS 12.0, *)) {
if (criticalSoundVolume != nil) {
Expand All @@ -220,6 +229,7 @@ + (UNMutableNotificationContent *)buildNotificationContent:(NSDictionary *)notif
} else {
notificationSound = [UNNotificationSound defaultSound];
}
#if !TARGET_OS_WATCH
} else {
if (criticalSound) {
if (@available(iOS 12.0, *)) {
Expand All @@ -237,16 +247,21 @@ + (UNMutableNotificationContent *)buildNotificationContent:(NSDictionary *)notif
notificationSound = [UNNotificationSound soundNamed:soundName];
}
}
#endif
content.sound = notificationSound;
} else if (iosDict[@"sound"] != nil) {
UNNotificationSound *notificationSound;
NSString *soundName = iosDict[@"sound"];

#if !TARGET_OS_WATCH
if ([soundName isEqualToString:@"default"]) {
#endif
notificationSound = [UNNotificationSound defaultSound];
#if !TARGET_OS_WATCH
} else {
notificationSound = [UNNotificationSound soundNamed:soundName];
}
#endif

content.sound = notificationSound;

Expand All @@ -257,6 +272,7 @@ + (UNMutableNotificationContent *)buildNotificationContent:(NSDictionary *)notif
content.threadIdentifier = iosDict[@"threadId"];
}

#if !TARGET_OS_WATCH
if (@available(iOS 12.0, *)) {
// summaryArgument
if (iosDict[@"summaryArgument"] != nil) {
Expand All @@ -268,6 +284,7 @@ + (UNMutableNotificationContent *)buildNotificationContent:(NSDictionary *)notif
content.summaryArgumentCount = [iosDict[@"summaryArgumentCount"] unsignedIntValue];
}
}
#endif

if (@available(iOS 13.0, *)) {
// targetContentId
Expand All @@ -281,10 +298,12 @@ + (UNMutableNotificationContent *)buildNotificationContent:(NSDictionary *)notif
content.attachments =
[NotifeeCoreUtil notificationAttachmentsFromDictionaryArray:iosDict[@"attachments"]];
}
#endif

return content;
}

#if !TARGET_OS_TV
+ (void)getNotificationCategories:(notifeeMethodNSArrayBlock)block {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getNotificationCategoriesWithCompletionHandler:^(
Expand All @@ -298,6 +317,7 @@ + (void)getNotificationCategories:(notifeeMethodNSArrayBlock)block {
categoryDictionary[@"allowInCarPlay"] =
@(((notificationCategory.options & UNNotificationCategoryOptionAllowInCarPlay) != 0));

#if !TARGET_OS_WATCH
if (@available(iOS 11.0, *)) {
categoryDictionary[@"hiddenPreviewsShowTitle"] =
@(((notificationCategory.options &
Expand All @@ -310,15 +330,18 @@ + (void)getNotificationCategories:(notifeeMethodNSArrayBlock)block {
notificationCategory.hiddenPreviewsBodyPlaceholder;
}
} else {
#endif
categoryDictionary[@"hiddenPreviewsShowTitle"] = @(NO);
categoryDictionary[@"hiddenPreviewsShowSubtitle"] = @(NO);
#if !TARGET_OS_WATCH
}

if (@available(iOS 12.0, *)) {
if (notificationCategory.categorySummaryFormat != nil) {
categoryDictionary[@"summaryFormat"] = notificationCategory.categorySummaryFormat;
}
}
#endif

if (@available(iOS 13.0, *)) {
categoryDictionary[@"allowAnnouncement"] = @(
Expand Down Expand Up @@ -353,8 +376,6 @@ + (void)setNotificationCategories:(NSArray<NSDictionary *> *)categories
UNNotificationCategory *category;

NSString *id = categoryDictionary[@"id"];
NSString *summaryFormat = categoryDictionary[@"summaryFormat"];
NSString *bodyPlaceHolder = categoryDictionary[@"hiddenPreviewsBodyPlaceholder"];

NSArray<UNNotificationAction *> *actions =
[NotifeeCoreUtil notificationActionsFromDictionaryArray:categoryDictionary[@"actions"]];
Expand All @@ -367,6 +388,7 @@ + (void)setNotificationCategories:(NSArray<NSDictionary *> *)categories
options |= UNNotificationCategoryOptionAllowInCarPlay;
}

#if !TARGET_OS_WATCH
if (@available(iOS 11.0, *)) {
if ([categoryDictionary[@"hiddenPreviewsShowTitle"] isEqual:@(YES)]) {
options |= UNNotificationCategoryOptionHiddenPreviewsShowTitle;
Expand All @@ -376,13 +398,17 @@ + (void)setNotificationCategories:(NSArray<NSDictionary *> *)categories
options |= UNNotificationCategoryOptionHiddenPreviewsShowSubtitle;
}
}
#endif

if (@available(iOS 13.0, *)) {
if ([categoryDictionary[@"allowAnnouncement"] isEqual:@(YES)]) {
options |= UNNotificationCategoryOptionAllowAnnouncement;
}
}

#if !TARGET_OS_WATCH
NSString *summaryFormat = categoryDictionary[@"summaryFormat"];
NSString *bodyPlaceHolder = categoryDictionary[@"hiddenPreviewsBodyPlaceholder"];
if (@available(iOS 12.0, *)) {
category = [UNNotificationCategory categoryWithIdentifier:id
actions:actions
Expand All @@ -397,11 +423,14 @@ + (void)setNotificationCategories:(NSArray<NSDictionary *> *)categories
hiddenPreviewsBodyPlaceholder:bodyPlaceHolder
options:options];
} else {
#endif
category = [UNNotificationCategory categoryWithIdentifier:id
actions:actions
intentIdentifiers:intentIdentifiers
options:options];
#if !TARGET_OS_WATCH
}
#endif

[UNNotificationCategories addObject:category];
}
Expand All @@ -410,6 +439,7 @@ + (void)setNotificationCategories:(NSArray<NSDictionary *> *)categories
[center setNotificationCategories:UNNotificationCategories];
block(nil);
}
#endif

/**
* Request UNAuthorizationOptions for user notifications.
Expand Down Expand Up @@ -448,11 +478,13 @@ + (void)requestPermission:(NSDictionary *)permissions
}
}

#if !TARGET_OS_TV
if ([permissions[@"announcement"] isEqual:@(YES)]) {
if (@available(iOS 13.0, *)) {
options |= UNAuthorizationOptionAnnouncement;
}
}
#endif

if ([permissions[@"carPlay"] isEqual:@(YES)]) {
options |= UNAuthorizationOptionCarPlay;
Expand Down Expand Up @@ -504,6 +536,7 @@ + (void)getNotificationSettings:(notifeeMethodNSDictionaryBlock)block {
}
}

#if !TARGET_OS_TV && !TARGET_OS_WATCH
NSNumber *showPreviews = @-1;
if (@available(iOS 11.0, *)) {
if (settings.showPreviewsSetting == UNShowPreviewsSettingNever) {
Expand Down Expand Up @@ -550,6 +583,7 @@ + (void)getNotificationSettings:(notifeeMethodNSDictionaryBlock)block {
[NotifeeCoreUtil numberForUNNotificationSetting:settings.lockScreenSetting];
settingsDictionary[@"notificationCenter"] =
[NotifeeCoreUtil numberForUNNotificationSetting:settings.notificationCenterSetting];
#endif
block(nil, settingsDictionary);
}];
}
Expand All @@ -559,22 +593,29 @@ + (void)getInitialNotification:(notifeeMethodNSDictionaryBlock)block {
}

+ (void)setBadgeCount:(NSInteger)count withBlock:(notifeeMethodVoidBlock)block {
#if !TARGET_OS_WATCH
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:count];
#endif
block(nil);
}

+ (void)getBadgeCount:(notifeeMethodNSIntegerBlock)block {
#if !TARGET_OS_WATCH
block(nil, [UIApplication sharedApplication].applicationIconBadgeNumber);
#endif
}

+ (void)incrementBadgeCount:(NSInteger)incrementBy withBlock:(notifeeMethodVoidBlock)block {
#if !TARGET_OS_WATCH
NSInteger currentCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
NSInteger newCount = currentCount + incrementBy;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:newCount];
#endif
block(nil);
}

+ (void)decrementBadgeCount:(NSInteger)decrementBy withBlock:(notifeeMethodVoidBlock)block {
#if !TARGET_OS_WATCH
NSInteger currentCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
NSInteger newCount = currentCount - decrementBy;

Expand All @@ -583,6 +624,7 @@ + (void)decrementBadgeCount:(NSInteger)decrementBy withBlock:(notifeeMethodVoidB
}

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:newCount];
#endif
block(nil);
}

Expand Down
Loading

0 comments on commit 03b92ed

Please sign in to comment.