-
Notifications
You must be signed in to change notification settings - Fork 767
/
Copy pathRNNotificationCenter.m
126 lines (112 loc) · 6.26 KB
/
RNNotificationCenter.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#import "RNNotificationCenter.h"
#import "RCTConvert+RNNotifications.h"
@implementation RNNotificationCenter
- (void)requestPermissions:(NSDictionary *)options {
BOOL carPlay = [options[@"carPlay"] boolValue];
BOOL criticalAlert = [options[@"criticalAlert"] boolValue];
BOOL providesAppNotificationSettings = [options[@"providesAppNotificationSettings"] boolValue];
BOOL provisional = [options[@"provisional"] boolValue];
BOOL announcement = [options[@"announcement"] boolValue];
UNAuthorizationOptions authOptions = (UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert);
if (carPlay) {
authOptions = authOptions | UNAuthorizationOptionCarPlay;
}
if (@available(iOS 12.0, *)) {
if (criticalAlert) {
authOptions = authOptions | UNAuthorizationOptionCriticalAlert;
}
if (providesAppNotificationSettings) {
authOptions = authOptions | UNAuthorizationOptionProvidesAppNotificationSettings;
}
if (provisional) {
authOptions = authOptions | UNAuthorizationOptionProvisional;
}
}
if (@available(iOS 13.0, *)) {
if (announcement) {
authOptions = authOptions | UNAuthorizationOptionAnnouncement;
}
}
[UNUserNotificationCenter.currentNotificationCenter requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (!error && granted) {
[UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.authorizationStatus == UNAuthorizationStatusAuthorized || settings.authorizationStatus == UNAuthorizationStatusProvisional) {
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] registerForRemoteNotifications];
});
}
}];
}
}];
}
- (void)setCategories:(NSArray *)json {
NSMutableSet<UNNotificationCategory *>* categories = nil;
if ([json count] > 0) {
categories = [NSMutableSet new];
for (NSDictionary* categoryJson in json) {
[categories addObject:[RCTConvert UNMutableUserNotificationCategory:categoryJson]];
}
}
[[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:categories];
}
- (void)postLocalNotification:(NSDictionary *)notification withId:(NSNumber *)notificationId {
UNNotificationRequest* localNotification = [RCTConvert UNNotificationRequest:notification withId:notificationId];
[[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:localNotification withCompletionHandler:nil];
}
- (void)cancelLocalNotification:(NSNumber *)notificationId {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removePendingNotificationRequestsWithIdentifiers:@[[notificationId stringValue]]];
}
- (void)removeAllDeliveredNotifications {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeAllDeliveredNotifications];
}
- (void)removeDeliveredNotifications:(NSArray<NSString *> *)identifiers {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center removeDeliveredNotificationsWithIdentifiers:identifiers];
}
- (void)getDeliveredNotifications:(RCTResponseSenderBlock)callback {
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) {
NSMutableArray<NSDictionary *> *formattedNotifications = [NSMutableArray new];
for (UNNotification *notification in notifications) {
[formattedNotifications addObject:[RCTConvert UNNotificationPayload:notification]];
}
callback(@[formattedNotifications]);
}];
}
- (void)cancelAllLocalNotifications {
[[UNUserNotificationCenter currentNotificationCenter] removeAllPendingNotificationRequests];
}
- (void)isRegisteredForRemoteNotifications:(RCTPromiseResolveBlock)resolve {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
if (settings.alertSetting == UNNotificationSettingEnabled || settings.soundSetting == UNNotificationSettingEnabled || settings.badgeSetting == UNNotificationSettingEnabled) {
resolve(@(YES));
} else {
resolve(@(NO));
}
}];
}
- (void)checkPermissions:(RCTPromiseResolveBlock)resolve {
[[UNUserNotificationCenter currentNotificationCenter] getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
NSMutableDictionary* allSettings = [NSMutableDictionary new];
[allSettings addEntriesFromDictionary:@{
@"badge": [NSNumber numberWithBool:settings.badgeSetting == UNNotificationSettingEnabled],
@"sound": [NSNumber numberWithBool:settings.soundSetting == UNNotificationSettingEnabled],
@"alert": [NSNumber numberWithBool:settings.alertSetting == UNNotificationSettingEnabled],
@"carPlay": [NSNumber numberWithBool:settings.carPlaySetting == UNNotificationSettingEnabled],
@"notificationCenter": [NSNumber numberWithBool:settings.notificationCenterSetting == UNNotificationSettingEnabled],
@"lockScreen": [NSNumber numberWithBool:settings.lockScreenSetting == UNNotificationSettingEnabled],
}];
if (@available(iOS 12.0, *)) {
allSettings[@"criticalAlert"] = [NSNumber numberWithBool:settings.criticalAlertSetting == UNNotificationSettingEnabled];
allSettings[@"providesAppNotificationSettings"] = [NSNumber numberWithBool:settings.providesAppNotificationSettings];
allSettings[@"provisional"] = [NSNumber numberWithBool:settings.authorizationStatus == UNAuthorizationStatusProvisional];
}
if (@available(iOS 13.0, *)) {
allSettings[@"announcement"] = [NSNumber numberWithBool:settings.announcementSetting == UNNotificationSettingEnabled];
}
resolve(allSettings);
}];
}
@end