RCTConvert+Notifications.m 6.05 KB
Newer Older
yogevbd's avatar
WIP  
yogevbd committed
1 2
#import "RCTConvert+Notifications.h"

yogevbd's avatar
WIP  
yogevbd committed
3

yogevbd's avatar
WIP  
yogevbd committed
4 5 6 7 8 9 10
@implementation RCTConvert (UIUserNotificationActivationMode)
RCT_ENUM_CONVERTER(UIUserNotificationActivationMode, (@{
                                                        @"foreground": @(UIUserNotificationActivationModeForeground),
                                                        @"background": @(UIUserNotificationActivationModeBackground)
                                                        }), UIUserNotificationActivationModeForeground, integerValue)
@end

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
@implementation RCTConvert (UNNotificationActionOptions)

+ (UNNotificationActionOptions)UNUserNotificationActionOptions:(id)json {
    UNNotificationActionOptions options = UNNotificationActionOptionNone;
    if ([json[@"activationMode"] isEqualToString:@"foreground"]) {
        options = options | UNNotificationActionOptionForeground;
    }
    if ([RCTConvert BOOL:json[@"authenticationRequired"]]) {
        options = options | UNNotificationActionOptionAuthenticationRequired;
    }
    if ([RCTConvert BOOL:json[@"destructive"]]) {
        options = options | UNNotificationActionOptionDestructive;
    }
    
    return options;
}
yogevbd's avatar
WIP  
yogevbd committed
27 28 29

@end

30
@implementation RCTConvert (UNMutableUserNotificationAction)
yogevbd's avatar
WIP  
yogevbd committed
31 32

+ (UNNotificationAction *)UNMutableUserNotificationAction:(id)json {
yogevbd's avatar
WIP  
yogevbd committed
33
    UNNotificationAction* action;
yogevbd's avatar
WIP  
yogevbd committed
34 35
    NSDictionary<NSString *, id> *details = [self NSDictionary:json];
    
yogevbd's avatar
WIP  
yogevbd committed
36 37 38 39 40 41
    if (details[@"textInput"]) {
        action = [UNTextInputNotificationAction actionWithIdentifier:details[@"identifier"] title:details[@"title"] options:[RCTConvert UNUserNotificationActionOptions:details] textInputButtonTitle:details[@"textInput"][@"buttonTitle"] textInputPlaceholder:details[@"textInput"][@"placeholder"]];
    } else {
        action = [UNNotificationAction actionWithIdentifier:details[@"identifier"] title:details[@"title"] options:[RCTConvert UNUserNotificationActionOptions:details]];
    }
    
yogevbd's avatar
WIP  
yogevbd committed
42 43
    return action;
}
yogevbd's avatar
WIP  
yogevbd committed
44

yogevbd's avatar
WIP  
yogevbd committed
45 46
@end

47
@implementation RCTConvert (UNMutableUserNotificationCategory)
yogevbd's avatar
WIP  
yogevbd committed
48 49

+ (UNNotificationCategory *)UNMutableUserNotificationCategory:(id)json {
yogevbd's avatar
WIP  
yogevbd committed
50 51 52 53
    NSDictionary<NSString *, id> *details = [self NSDictionary:json];
    
    NSMutableArray* actions = [NSMutableArray new];
    for (NSDictionary* actionJson in [RCTConvert NSArray:details[@"actions"]]) {
54
        [actions addObject:[RCTConvert UNMutableUserNotificationAction:actionJson]];
yogevbd's avatar
WIP  
yogevbd committed
55 56
    }
    
57
    UNNotificationCategory* category = [UNNotificationCategory categoryWithIdentifier:details[@"identifier"] actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
yogevbd's avatar
WIP  
yogevbd committed
58 59 60 61 62 63 64
    
    return category;
}

@end

@implementation RCTConvert (UNNotificationRequest)
yogevbd's avatar
WIP  
yogevbd committed
65

yogevbd's avatar
WIP  
yogevbd committed
66 67 68 69 70
+ (UNNotificationRequest *)UNNotificationRequest:(id)json withId:(NSString*)notificationId
{
    NSDictionary<NSString *, id> *details = [self NSDictionary:json];
    
    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
yogevbd's avatar
WIP  
yogevbd committed
71 72 73 74
    content.body = [RCTConvert NSString:details[@"body"]];
    content.title = [RCTConvert NSString:details[@"title"]];
    content.sound = [RCTConvert NSString:details[@"sound"]]
    ? [UNNotificationSound soundNamed:[RCTConvert NSString:details[@"sound"]]]
yogevbd's avatar
WIP  
yogevbd committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    : [UNNotificationSound defaultSound];
    if ([RCTConvert BOOL:details[@"silent"]]) {
        content.sound = nil;
    }
    content.userInfo = [RCTConvert NSDictionary:details[@"userInfo"]] ?: @{};
    content.categoryIdentifier = [RCTConvert NSString:details[@"category"]];
    
    NSDate *triggerDate = [RCTConvert NSDate:details[@"fireDate"]];
    UNCalendarNotificationTrigger *trigger = nil;
    if (triggerDate != nil) {
        NSDateComponents *triggerDateComponents = [[NSCalendar currentCalendar]
                                                   components:NSCalendarUnitYear +
                                                   NSCalendarUnitMonth + NSCalendarUnitDay +
                                                   NSCalendarUnitHour + NSCalendarUnitMinute +
                                                   NSCalendarUnitSecond + NSCalendarUnitTimeZone
                                                   fromDate:triggerDate];
        trigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:triggerDateComponents
                                                                           repeats:NO];
    }
    
    return [UNNotificationRequest requestWithIdentifier:notificationId
                                                content:content trigger:trigger];
}
yogevbd's avatar
WIP  
yogevbd committed
98

yogevbd's avatar
WIP  
yogevbd committed
99
@end
yogevbd's avatar
WIP  
yogevbd committed
100 101

@implementation RCTConvert (UNNotification)
yogevbd's avatar
WIP  
yogevbd committed
102

yogevbd's avatar
WIP  
yogevbd committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
+ (NSDictionary *)UNNotificationPayload:(UNNotification *)notification {
    NSMutableDictionary *formattedNotification = [NSMutableDictionary dictionary];
    UNNotificationContent *content = notification.request.content;
    
    formattedNotification[@"identifier"] = notification.request.identifier;
    
    if (notification.date) {
        NSDateFormatter *formatter = [NSDateFormatter new];
        [formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"];
        NSString *dateString = [formatter stringFromDate:notification.date];
        formattedNotification[@"date"] = dateString;
    }
    
    formattedNotification[@"title"] = RCTNullIfNil(content.title);
    formattedNotification[@"body"] = RCTNullIfNil(content.body);
    formattedNotification[@"category"] = RCTNullIfNil(content.categoryIdentifier);
    formattedNotification[@"thread"] = RCTNullIfNil(content.threadIdentifier);
    formattedNotification[@"userInfo"] = RCTNullIfNil(RCTJSONClean(content.userInfo));
    
    return formattedNotification;
}

@end

@implementation RCTConvert (UNNotificationPresentationOptions)

+ (UNNotificationPresentationOptions)UNNotificationPresentationOptions:(id)json {
    UNNotificationPresentationOptions options = UNNotificationPresentationOptionNone;
    if ([RCTConvert BOOL:json[@"alert"]]) {
        options = options | UNNotificationPresentationOptionAlert;
    }
    if ([RCTConvert BOOL:json[@"badge"]]) {
        options = options | UNNotificationPresentationOptionBadge;
    }
    if ([RCTConvert BOOL:json[@"sound"]]) {
        options = options | UNNotificationPresentationOptionSound;
    }
    
    return options;
}

@end