Skip to content

Commit

Permalink
[expo-notification][ios] Add ScopedNotificationPresentationModule (#8386
Browse files Browse the repository at this point in the history
)

* [expo-notification][ios] Add ScopedNotificationPresentationModule

* [expo-notification] Pass to block only experienceId
  • Loading branch information
lukmccall committed May 22, 2020
1 parent 23f8d31 commit bfb365d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 5 deletions.
6 changes: 6 additions & 0 deletions ios/Exponent.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,17 @@
// Copyright 2018-present 650 Industries. All rights reserved.

#if __has_include(<EXNotifications/EXNotificationPresentationModule.h>)

#import <EXNotifications/EXNotificationPresentationModule.h>

NS_ASSUME_NONNULL_BEGIN

@interface EXScopedNotificationPresentationModule : EXNotificationPresentationModule

- (instancetype)initWithExperienceId:(NSString *)experienceId;

@end

NS_ASSUME_NONNULL_END

#endif
@@ -0,0 +1,67 @@
// Copyright 2018-present 650 Industries. All rights reserved.

#import "EXScopedNotificationPresentationModule.h"
#import "EXScopedNotificationsUtils.h"

#import <EXNotifications/EXNotificationSerializer.h>

@interface EXScopedNotificationPresentationModule ()

@property (nonatomic, strong) NSString *experienceId;

@end

@implementation EXScopedNotificationPresentationModule

- (instancetype)initWithExperienceId:(NSString *)experienceId
{
if (self = [super init]) {
_experienceId = experienceId;
}

return self;
}

- (NSArray * _Nonnull)serializeNotifications:(NSArray<UNNotification *> * _Nonnull)notifications
{
NSMutableArray *serializedNotifications = [NSMutableArray new];
for (UNNotification *notification in notifications) {
if ([EXScopedNotificationsUtils shouldNotification:notification beHandledByExperience:_experienceId]) {
[serializedNotifications addObject:[EXNotificationSerializer serializedNotification:notification]];
}
}
return serializedNotifications;
}

- (void)dismissNotificationWithIdentifier:(NSString *)identifier resolve:(UMPromiseResolveBlock)resolve reject:(UMPromiseRejectBlock)reject
{
__block NSString *experienceId = _experienceId;
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
for (UNNotification *notification in notifications) {
if ([notification.request.identifier isEqual:identifier]) {
if ([EXScopedNotificationsUtils shouldNotification:notification beHandledByExperience:experienceId]) {
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[identifier]];
}
break;
}
}
resolve(nil);
}];
}

- (void)dismissAllNotificationsWithResolver:(UMPromiseResolveBlock)resolve reject:(UMPromiseRejectBlock)reject
{
__block NSString *experienceId = _experienceId;
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
NSMutableArray<NSString *> *toDismiss = [NSMutableArray new];
for (UNNotification *notification in notifications) {
if ([EXScopedNotificationsUtils shouldNotification:notification beHandledByExperience:experienceId]) {
[toDismiss addObject:notification.request.identifier];
}
}
[[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:toDismiss];
resolve(nil);
}];
}

@end
Expand Up @@ -26,6 +26,7 @@
#import "EXScopedNotificationsHandlerModule.h"
#import "EXScopedNotificationBuilder.h"
#import "EXScopedNotificationSchedulerModule.h"
#import "EXScopedNotificationPresentationModule.h"

#if __has_include(<EXTaskManager/EXTaskManager.h>)
#import <EXTaskManager/EXTaskManager.h>
Expand Down Expand Up @@ -145,6 +146,11 @@ - (UMModuleRegistry *)moduleRegistryForParams:(NSDictionary *)params forExperien
EXScopedNotificationSchedulerModule *schedulerModule = [[EXScopedNotificationSchedulerModule alloc] initWithExperienceId:experienceId];
[moduleRegistry registerExportedModule:schedulerModule];
#endif

#if __has_include(<EXNotifications/EXNotificationPresentationModule.h>)
EXScopedNotificationPresentationModule *notificationPresentationModule = [[EXScopedNotificationPresentationModule alloc] initWithExperienceId:experienceId];
[moduleRegistry registerExportedModule:notificationPresentationModule];
#endif
return moduleRegistry;
}

Expand Down
Expand Up @@ -5,4 +5,7 @@
#import <EXNotifications/EXNotificationsDelegate.h>

@interface EXNotificationPresentationModule : UMExportedModule <UMModuleRegistryConsumer, EXNotificationsDelegate>

- (NSArray * _Nonnull)serializeNotifications:(NSArray<UNNotification *> * _Nonnull)notifications;

@end
Expand Up @@ -60,11 +60,7 @@ - (instancetype)init
reject:(UMPromiseRejectBlock)reject)
{
[[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
NSMutableArray *serializedNotifications = [NSMutableArray new];
for (UNNotification *notification in notifications) {
[serializedNotifications addObject:[EXNotificationSerializer serializedNotification:notification]];
}
resolve(serializedNotifications);
resolve([self serializeNotifications:notifications]);
}];
}

Expand Down Expand Up @@ -113,5 +109,15 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNot
completionHandler(presentationOptions);
}

# pragma mark - Helpers

- (NSArray * _Nonnull)serializeNotifications:(NSArray<UNNotification *> * _Nonnull)notifications
{
NSMutableArray *serializedNotifications = [NSMutableArray new];
for (UNNotification *notification in notifications) {
[serializedNotifications addObject:[EXNotificationSerializer serializedNotification:notification]];
}
return serializedNotifications;
}

@end

0 comments on commit bfb365d

Please sign in to comment.