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

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
@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
19 20 21

@end

22
@implementation RCTConvert (UNMutableUserNotificationAction)
yogevbd's avatar
WIP  
yogevbd committed
23 24

+ (UNNotificationAction *)UNMutableUserNotificationAction:(id)json {
yogevbd's avatar
WIP  
yogevbd committed
25
    UNNotificationAction* action;
yogevbd's avatar
WIP  
yogevbd committed
26 27
    NSDictionary<NSString *, id> *details = [self NSDictionary:json];
    
yogevbd's avatar
WIP  
yogevbd committed
28 29 30 31 32 33
    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
34 35
    return action;
}
yogevbd's avatar
WIP  
yogevbd committed
36

yogevbd's avatar
WIP  
yogevbd committed
37 38
@end

39
@implementation RCTConvert (UNMutableUserNotificationCategory)
yogevbd's avatar
WIP  
yogevbd committed
40 41

+ (UNNotificationCategory *)UNMutableUserNotificationCategory:(id)json {
yogevbd's avatar
WIP  
yogevbd committed
42 43 44 45
    NSDictionary<NSString *, id> *details = [self NSDictionary:json];
    
    NSMutableArray* actions = [NSMutableArray new];
    for (NSDictionary* actionJson in [RCTConvert NSArray:details[@"actions"]]) {
46
        [actions addObject:[RCTConvert UNMutableUserNotificationAction:actionJson]];
yogevbd's avatar
WIP  
yogevbd committed
47 48
    }
    
49
    UNNotificationCategory* category = [UNNotificationCategory categoryWithIdentifier:details[@"identifier"] actions:actions intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];
yogevbd's avatar
WIP  
yogevbd committed
50 51 52 53 54 55 56
    
    return category;
}

@end

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

yogevbd's avatar
yogevbd committed
58
+ (UNNotificationRequest *)UNNotificationRequest:(id)json withId:(NSNumber*)notificationId
yogevbd's avatar
WIP  
yogevbd committed
59 60 61 62
{
    NSDictionary<NSString *, id> *details = [self NSDictionary:json];
    
    UNMutableNotificationContent *content = [UNMutableNotificationContent new];
yogevbd's avatar
WIP  
yogevbd committed
63 64 65 66
    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
67 68 69 70
    : [UNNotificationSound defaultSound];
    if ([RCTConvert BOOL:details[@"silent"]]) {
        content.sound = nil;
    }
yogevbd's avatar
yogevbd committed
71
    content.userInfo = [RCTConvert NSDictionary:details] ?: @{};
yogevbd's avatar
WIP  
yogevbd committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    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];
    }
    
yogevbd's avatar
yogevbd committed
87
    return [UNNotificationRequest requestWithIdentifier:[NSString stringWithFormat:@"%@", notificationId]
yogevbd's avatar
WIP  
yogevbd committed
88 89
                                                content:content trigger:trigger];
}
yogevbd's avatar
WIP  
yogevbd committed
90

yogevbd's avatar
WIP  
yogevbd committed
91
@end
yogevbd's avatar
WIP  
yogevbd committed
92 93

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

yogevbd's avatar
WIP  
yogevbd committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
+ (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);
yogevbd's avatar
yogevbd committed
112 113
    
    [formattedNotification addEntriesFromDictionary:[NSDictionary dictionaryWithDictionary:RCTNullIfNil(RCTJSONClean(content.userInfo))]];
yogevbd's avatar
WIP  
yogevbd committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
    
    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