6
6
#import < React/RCTConvert.h>
7
7
#import < React/RCTEventDispatcher.h>
8
8
#import < React/RCTUtils.h>
9
+ #import < AudioToolbox/AudioToolbox.h>
9
10
10
11
@implementation RnAlarmNotification
11
12
12
13
RCT_EXPORT_MODULE (RNAlarmNotification)
13
14
14
-
15
- - (void )userNotificationCenter:(UNUserNotificationCenter *)center
16
- willPresentNotification:(UNNotification *)notification
17
- didReceiveNotificationResponse:(UNNotificationResponse *)response
18
- withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0 )){
15
+ - (void )userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0 )) {
19
16
if ([response.actionIdentifier isEqualToString: UNNotificationDismissActionIdentifier ]) {
20
17
NSLog (@" dimiss notification" );
21
- }
22
- else if ([response.actionIdentifier isEqualToString: UNNotificationDefaultActionIdentifier ]) {
18
+ } else if ([response.actionIdentifier isEqualToString: UNNotificationDefaultActionIdentifier ]) {
23
19
NSLog (@" open app" );
24
20
}
25
21
26
- completionHandler (UNNotificationPresentationOptionSound );
22
+ completionHandler (UNNotificationPresentationOptionSound );
27
23
}
28
24
29
25
- (void )userNotificationCenter : (UNUserNotificationCenter *)center
30
- didReceiveNotificationResponse : (UNNotificationResponse *)response
26
+ didReceiveNotificationResponse : (UNNotificationResponse *)response
31
27
withCompletionHandler : (void (^)(void ))completionHandler API_AVAILABLE(ios(10.0 )){
32
28
if ([response.notification.request.content.categoryIdentifier isEqualToString: @" TIMER_EXPIRED" ]) {
33
- if ([response.actionIdentifier isEqualToString: @" SNOOZE_ACTION" ])
34
- {
29
+ if ([response.actionIdentifier isEqualToString: @" SNOOZE_ACTION" ]) {
35
30
NSLog (@" snooze notification" );
36
- }
37
- else if ([response.actionIdentifier isEqualToString: @" STOP_ACTION" ])
38
- {
31
+ } else if ([response.actionIdentifier isEqualToString: @" STOP_ACTION" ]) {
39
32
NSLog (@" delete notifications" );
40
33
}
41
-
34
+
35
+ // completionHandler();
42
36
}
43
37
}
44
38
39
+ + (void )vibratePhone {
40
+ NSLog (@" vibratePhone %@ " , @" here" );
41
+ // if([[UIDevice currentDevice].model isEqualToString:@"iPhone"]) {
42
+ // AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
43
+ // } else {
44
+ // AudioServicesPlayAlertSound (kSystemSoundID_Vibrate);
45
+ // }
46
+ //
47
+ // [RnAlarmNotification vibratePhone];
48
+ }
49
+
45
50
RCT_EXPORT_METHOD (scheduleAlarm: (NSDictionary *)details resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
46
51
if (@available (iOS 10.0 , *)) {
47
52
UNMutableNotificationContent * content = [[UNMutableNotificationContent alloc ] init ];
@@ -62,7 +67,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
62
67
NSString *strNumHour = splitHour[0 ];
63
68
NSString *strNumMinute = splitHour[1 ];
64
69
NSString *strNumSecond = splitHour[2 ];
65
-
70
+
66
71
// Configure the trigger for date
67
72
NSDateComponents * fireDate = [[NSDateComponents alloc ] init ];
68
73
fireDate.day = [strNumDay intValue ];
@@ -74,23 +79,23 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
74
79
fireDate.timeZone = [NSTimeZone defaultTimeZone ];
75
80
76
81
UNCalendarNotificationTrigger * trigger = [UNCalendarNotificationTrigger
77
- triggerWithDateMatchingComponents: fireDate repeats: NO ];
82
+ triggerWithDateMatchingComponents: fireDate repeats: NO ];
78
83
79
84
content.sound = [UNNotificationSound defaultSound ];
80
85
81
86
NSString *alarmId = [NSString stringWithFormat: @" %ld " , (long ) NSDate .date.timeIntervalSince1970];
82
-
87
+
83
88
// Create the request object.
84
89
UNNotificationRequest * request = [UNNotificationRequest
85
- requestWithIdentifier: alarmId content: content trigger: trigger];
90
+ requestWithIdentifier: alarmId content: content trigger: trigger];
86
91
87
92
UNUserNotificationCenter * center = [UNUserNotificationCenter currentNotificationCenter ];
88
93
89
94
[center addNotificationRequest: request withCompletionHandler: ^(NSError * _Nullable error) {
90
- if (error != nil ) {
91
- NSLog (@" %@ " , error.localizedDescription );
92
- reject (@" error" , nil , error);
93
- }
95
+ if (error != nil ) {
96
+ NSLog (@" %@ " , error.localizedDescription );
97
+ reject (@" error" , nil , error);
98
+ }
94
99
}];
95
100
96
101
NSDictionary *alarm = [NSDictionary dictionaryWithObjectsAndKeys: alarmId, @" id" , nil ];
@@ -101,6 +106,10 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
101
106
}
102
107
}
103
108
109
+ RCT_EXPORT_METHOD (sendNotification: (NSDictionary *)details){
110
+ NSLog (@" send notification" );
111
+ }
112
+
104
113
RCT_EXPORT_METHOD (deleteAlarm: (NSInteger *)id){
105
114
NSLog (@" delete alarm: %li " , (long ) id );
106
115
}
@@ -113,10 +122,6 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
113
122
NSLog (@" stop alarm sound" );
114
123
}
115
124
116
- RCT_EXPORT_METHOD (sendNotification: (NSDictionary *)details){
117
- NSLog (@" send notification" );
118
- }
119
-
120
125
RCT_EXPORT_METHOD (removeFiredNotification: (NSInteger )id){
121
126
NSLog (@" remove fired notification: %li " , (long ) id );
122
127
}
@@ -133,37 +138,37 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
133
138
}
134
139
}
135
140
136
- RCT_EXPORT_METHOD (getScheduledAlarms: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
137
- NSLog (@" get all notifications" );
141
+ // RCT_EXPORT_METHOD(getScheduledAlarms: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
142
+ // NSLog(@"get all notifications");
138
143
// NSArray<UILocalNotification *> *scheduledLocalNotifications = RCTSharedApplication().scheduledLocalNotifications;
139
144
// NSMutableArray<NSDictionary *> *formattedScheduledLocalNotifications = [NSMutableArray new];
140
145
// for (UILocalNotification *notification in scheduledLocalNotifications) {
141
- // [formattedScheduledLocalNotifications addObject:RCTFormatLocalNotification(notification)];
146
+ // [formattedScheduledLocalNotifications addObject:RCTFormatLocalNotification(notification)];
142
147
// }
143
148
// resolve(@[formattedScheduledLocalNotifications]);
144
- }
149
+ // }
145
150
146
151
RCT_EXPORT_METHOD (requestPermissions:(NSDictionary *)permissions
147
152
resolver:(RCTPromiseResolveBlock)resolve
148
153
rejecter:(RCTPromiseRejectBlock)reject) {
149
154
if (RCTRunningInAppExtension ()) {
150
- reject (@" E_UNABLE_TO_REQUEST_PERMISSIONS" , nil , RCTErrorWithMessage (@" Requesting push notifications is currently unavailable in an app extension" ));
151
- return ;
155
+ reject (@" E_UNABLE_TO_REQUEST_PERMISSIONS" , nil , RCTErrorWithMessage (@" Requesting push notifications is currently unavailable in an app extension" ));
156
+ return ;
152
157
}
153
158
154
159
UIUserNotificationType types = UIUserNotificationTypeNone;
155
160
if (permissions) {
156
- if ([RCTConvert BOOL: permissions[@" alert" ]]) {
157
- types |= UIUserNotificationTypeAlert;
158
- }
159
- if ([RCTConvert BOOL: permissions[@" badge" ]]) {
160
- types |= UIUserNotificationTypeBadge;
161
- }
162
- if ([RCTConvert BOOL: permissions[@" sound" ]]) {
163
- types |= UIUserNotificationTypeSound;
164
- }
161
+ if ([RCTConvert BOOL: permissions[@" alert" ]]) {
162
+ types |= UIUserNotificationTypeAlert;
163
+ }
164
+ if ([RCTConvert BOOL: permissions[@" badge" ]]) {
165
+ types |= UIUserNotificationTypeBadge;
166
+ }
167
+ if ([RCTConvert BOOL: permissions[@" sound" ]]) {
168
+ types |= UIUserNotificationTypeSound;
169
+ }
165
170
} else {
166
- types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
171
+ types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
167
172
}
168
173
169
174
if (@available (iOS 10.0 , *)) {
@@ -188,33 +193,28 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
188
193
}
189
194
}
190
195
191
- RCT_EXPORT_METHOD (checkPermissions:(RCTResponseSenderBlock)callback)
192
- {
193
- if (RCTRunningInAppExtension ()) {
194
- callback (@[RCTSettingsDictForUNNotificationSettings (NO , NO , NO , NO , NO )]);
195
- return ;
196
- }
197
-
196
+ RCT_EXPORT_METHOD (checkPermissions:(RCTResponseSenderBlock)callback) {
197
+ if (RCTRunningInAppExtension ()) {
198
+ callback (@[RCTSettingsDictForUNNotificationSettings (NO , NO , NO , NO , NO )]);
199
+ return ;
200
+ }
201
+
198
202
if (@available (iOS 10.0 , *)) {
199
203
[UNUserNotificationCenter .currentNotificationCenter getNotificationSettingsWithCompletionHandler: ^(UNNotificationSettings * _Nonnull settings) {
200
204
callback (@[RCTPromiseResolveValueForUNNotificationSettings (settings)]);
201
205
}];
202
206
} else {
203
207
// Fallback on earlier versions
204
208
}
205
- }
209
+ }
206
210
207
211
API_AVAILABLE (ios(10.0 ))
208
212
static inline NSDictionary *RCTPromiseResolveValueForUNNotificationSettings(UNNotificationSettings * _Nonnull settings) {
209
- return RCTSettingsDictForUNNotificationSettings (settings.alertSetting == UNNotificationSettingEnabled ,
210
- settings.badgeSetting == UNNotificationSettingEnabled ,
211
- settings.soundSetting == UNNotificationSettingEnabled ,
212
- settings.lockScreenSetting == UNNotificationSettingEnabled ,
213
- settings.notificationCenterSetting == UNNotificationSettingEnabled );
214
- }
213
+ return RCTSettingsDictForUNNotificationSettings (settings.alertSetting == UNNotificationSettingEnabled , settings.badgeSetting == UNNotificationSettingEnabled , settings.soundSetting == UNNotificationSettingEnabled , settings.lockScreenSetting == UNNotificationSettingEnabled , settings.notificationCenterSetting == UNNotificationSettingEnabled );
214
+ }
215
215
216
216
static inline NSDictionary *RCTSettingsDictForUNNotificationSettings (BOOL alert, BOOL badge, BOOL sound, BOOL lockScreen, BOOL notificationCenter) {
217
- return @{@" alert" : @(alert), @" badge" : @(badge), @" sound" : @(sound), @" lockScreen" : @(lockScreen), @" notificationCenter" : @(notificationCenter)};
218
- }
217
+ return @{@" alert" : @(alert), @" badge" : @(badge), @" sound" : @(sound), @" lockScreen" : @(lockScreen), @" notificationCenter" : @(notificationCenter)};
218
+ }
219
219
220
220
@end
0 commit comments