Skip to content

Commit 25219d4

Browse files
committed
android keeps vibrating forever fixes #79
1 parent fe97849 commit 25219d4

File tree

2 files changed

+64
-60
lines changed

2 files changed

+64
-60
lines changed

android/src/main/java/com/emekalites/react/alarm/notification/AlarmUtil.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,11 @@ void removeAllFiredNotifications() {
509509
}
510510

511511
void stopAlarmSound() {
512-
Log.e(TAG, "stop alarm sound");
512+
Log.e(TAG, "stop vibration and alarm sound");
513+
Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
514+
if (vibrator.hasVibrator()) {
515+
vibrator.cancel();
516+
}
513517
audioInterface.stopPlayer();
514518
}
515519

ios/RnAlarmNotification.m

+59-59
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,47 @@
66
#import <React/RCTConvert.h>
77
#import <React/RCTEventDispatcher.h>
88
#import <React/RCTUtils.h>
9+
#import <AudioToolbox/AudioToolbox.h>
910

1011
@implementation RnAlarmNotification
1112

1213
RCT_EXPORT_MODULE(RNAlarmNotification)
1314

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)) {
1916
if ([response.actionIdentifier isEqualToString:UNNotificationDismissActionIdentifier]) {
2017
NSLog(@"dimiss notification");
21-
}
22-
else if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
18+
} else if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
2319
NSLog(@"open app");
2420
}
2521

26-
completionHandler(UNNotificationPresentationOptionSound);
22+
completionHandler(UNNotificationPresentationOptionSound);
2723
}
2824

2925
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
30-
didReceiveNotificationResponse:(UNNotificationResponse *)response
26+
didReceiveNotificationResponse:(UNNotificationResponse *)response
3127
withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)){
3228
if ([response.notification.request.content.categoryIdentifier isEqualToString:@"TIMER_EXPIRED"]) {
33-
if ([response.actionIdentifier isEqualToString:@"SNOOZE_ACTION"])
34-
{
29+
if ([response.actionIdentifier isEqualToString:@"SNOOZE_ACTION"]) {
3530
NSLog(@"snooze notification");
36-
}
37-
else if ([response.actionIdentifier isEqualToString:@"STOP_ACTION"])
38-
{
31+
} else if ([response.actionIdentifier isEqualToString:@"STOP_ACTION"]) {
3932
NSLog(@"delete notifications");
4033
}
41-
34+
35+
// completionHandler();
4236
}
4337
}
4438

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+
4550
RCT_EXPORT_METHOD(scheduleAlarm: (NSDictionary *)details resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
4651
if (@available(iOS 10.0, *)) {
4752
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
@@ -62,7 +67,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
6267
NSString *strNumHour = splitHour[0];
6368
NSString *strNumMinute = splitHour[1];
6469
NSString *strNumSecond = splitHour[2];
65-
70+
6671
// Configure the trigger for date
6772
NSDateComponents* fireDate = [[NSDateComponents alloc] init];
6873
fireDate.day = [strNumDay intValue];
@@ -74,23 +79,23 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
7479
fireDate.timeZone = [NSTimeZone defaultTimeZone];
7580

7681
UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
77-
triggerWithDateMatchingComponents:fireDate repeats:NO];
82+
triggerWithDateMatchingComponents:fireDate repeats:NO];
7883

7984
content.sound = [UNNotificationSound defaultSound];
8085

8186
NSString *alarmId = [NSString stringWithFormat: @"%ld", (long) NSDate.date.timeIntervalSince1970];
82-
87+
8388
// Create the request object.
8489
UNNotificationRequest* request = [UNNotificationRequest
85-
requestWithIdentifier:alarmId content:content trigger:trigger];
90+
requestWithIdentifier:alarmId content:content trigger:trigger];
8691

8792
UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
8893

8994
[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+
}
9499
}];
95100

96101
NSDictionary *alarm = [NSDictionary dictionaryWithObjectsAndKeys: alarmId, @"id", nil];
@@ -101,6 +106,10 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
101106
}
102107
}
103108

109+
RCT_EXPORT_METHOD(sendNotification: (NSDictionary *)details){
110+
NSLog(@"send notification");
111+
}
112+
104113
RCT_EXPORT_METHOD(deleteAlarm: (NSInteger *)id){
105114
NSLog(@"delete alarm: %li", (long) id);
106115
}
@@ -113,10 +122,6 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
113122
NSLog(@"stop alarm sound");
114123
}
115124

116-
RCT_EXPORT_METHOD(sendNotification: (NSDictionary *)details){
117-
NSLog(@"send notification");
118-
}
119-
120125
RCT_EXPORT_METHOD(removeFiredNotification: (NSInteger)id){
121126
NSLog(@"remove fired notification: %li", (long) id);
122127
}
@@ -133,37 +138,37 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
133138
}
134139
}
135140

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");
138143
// NSArray<UILocalNotification *> *scheduledLocalNotifications = RCTSharedApplication().scheduledLocalNotifications;
139144
// NSMutableArray<NSDictionary *> *formattedScheduledLocalNotifications = [NSMutableArray new];
140145
// for (UILocalNotification *notification in scheduledLocalNotifications) {
141-
// [formattedScheduledLocalNotifications addObject:RCTFormatLocalNotification(notification)];
146+
// [formattedScheduledLocalNotifications addObject:RCTFormatLocalNotification(notification)];
142147
// }
143148
// resolve(@[formattedScheduledLocalNotifications]);
144-
}
149+
//}
145150

146151
RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions
147152
resolver:(RCTPromiseResolveBlock)resolve
148153
rejecter:(RCTPromiseRejectBlock)reject) {
149154
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;
152157
}
153158

154159
UIUserNotificationType types = UIUserNotificationTypeNone;
155160
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+
}
165170
} else {
166-
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
171+
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
167172
}
168173

169174
if (@available(iOS 10.0, *)) {
@@ -188,33 +193,28 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
188193
}
189194
}
190195

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+
198202
if (@available(iOS 10.0, *)) {
199203
[UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
200204
callback(@[RCTPromiseResolveValueForUNNotificationSettings(settings)]);
201205
}];
202206
} else {
203207
// Fallback on earlier versions
204208
}
205-
}
209+
}
206210

207211
API_AVAILABLE(ios(10.0))
208212
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+
}
215215

216216
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+
}
219219

220220
@end

0 commit comments

Comments
 (0)