Skip to content

Commit

Permalink
android keeps vibrating forever fixes #79
Browse files Browse the repository at this point in the history
  • Loading branch information
emekalites committed Aug 27, 2020
1 parent fe97849 commit 25219d4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,11 @@ void removeAllFiredNotifications() {
}

void stopAlarmSound() {
Log.e(TAG, "stop alarm sound");
Log.e(TAG, "stop vibration and alarm sound");
Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator.hasVibrator()) {
vibrator.cancel();
}
audioInterface.stopPlayer();
}

Expand Down
118 changes: 59 additions & 59 deletions ios/RnAlarmNotification.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,47 @@
#import <React/RCTConvert.h>
#import <React/RCTEventDispatcher.h>
#import <React/RCTUtils.h>
#import <AudioToolbox/AudioToolbox.h>

@implementation RnAlarmNotification

RCT_EXPORT_MODULE(RNAlarmNotification)


- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0)){
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(ios(10.0)) {
if ([response.actionIdentifier isEqualToString:UNNotificationDismissActionIdentifier]) {
NSLog(@"dimiss notification");
}
else if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
} else if ([response.actionIdentifier isEqualToString:UNNotificationDefaultActionIdentifier]) {
NSLog(@"open app");
}

completionHandler(UNNotificationPresentationOptionSound);
completionHandler(UNNotificationPresentationOptionSound);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(ios(10.0)){
if ([response.notification.request.content.categoryIdentifier isEqualToString:@"TIMER_EXPIRED"]) {
if ([response.actionIdentifier isEqualToString:@"SNOOZE_ACTION"])
{
if ([response.actionIdentifier isEqualToString:@"SNOOZE_ACTION"]) {
NSLog(@"snooze notification");
}
else if ([response.actionIdentifier isEqualToString:@"STOP_ACTION"])
{
} else if ([response.actionIdentifier isEqualToString:@"STOP_ACTION"]) {
NSLog(@"delete notifications");
}


// completionHandler();
}
}

+ (void)vibratePhone {
NSLog(@"vibratePhone %@", @"here");
// if([[UIDevice currentDevice].model isEqualToString:@"iPhone"]) {
// AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
// } else {
// AudioServicesPlayAlertSound (kSystemSoundID_Vibrate);
// }
//
// [RnAlarmNotification vibratePhone];
}

RCT_EXPORT_METHOD(scheduleAlarm: (NSDictionary *)details resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
if (@available(iOS 10.0, *)) {
UNMutableNotificationContent* content = [[UNMutableNotificationContent alloc] init];
Expand All @@ -62,7 +67,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
NSString *strNumHour = splitHour[0];
NSString *strNumMinute = splitHour[1];
NSString *strNumSecond = splitHour[2];

// Configure the trigger for date
NSDateComponents* fireDate = [[NSDateComponents alloc] init];
fireDate.day = [strNumDay intValue];
Expand All @@ -74,23 +79,23 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
fireDate.timeZone = [NSTimeZone defaultTimeZone];

UNCalendarNotificationTrigger* trigger = [UNCalendarNotificationTrigger
triggerWithDateMatchingComponents:fireDate repeats:NO];
triggerWithDateMatchingComponents:fireDate repeats:NO];

content.sound = [UNNotificationSound defaultSound];

NSString *alarmId = [NSString stringWithFormat: @"%ld", (long) NSDate.date.timeIntervalSince1970];

// Create the request object.
UNNotificationRequest* request = [UNNotificationRequest
requestWithIdentifier:alarmId content:content trigger:trigger];
requestWithIdentifier:alarmId content:content trigger:trigger];

UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];

[center addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"%@", error.localizedDescription);
reject(@"error", nil, error);
}
if (error != nil) {
NSLog(@"%@", error.localizedDescription);
reject(@"error", nil, error);
}
}];

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

RCT_EXPORT_METHOD(sendNotification: (NSDictionary *)details){
NSLog(@"send notification");
}

RCT_EXPORT_METHOD(deleteAlarm: (NSInteger *)id){
NSLog(@"delete alarm: %li", (long) id);
}
Expand All @@ -113,10 +122,6 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
NSLog(@"stop alarm sound");
}

RCT_EXPORT_METHOD(sendNotification: (NSDictionary *)details){
NSLog(@"send notification");
}

RCT_EXPORT_METHOD(removeFiredNotification: (NSInteger)id){
NSLog(@"remove fired notification: %li", (long) id);
}
Expand All @@ -133,37 +138,37 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
}
}

RCT_EXPORT_METHOD(getScheduledAlarms: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
NSLog(@"get all notifications");
//RCT_EXPORT_METHOD(getScheduledAlarms: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
// NSLog(@"get all notifications");
// NSArray<UILocalNotification *> *scheduledLocalNotifications = RCTSharedApplication().scheduledLocalNotifications;
// NSMutableArray<NSDictionary *> *formattedScheduledLocalNotifications = [NSMutableArray new];
// for (UILocalNotification *notification in scheduledLocalNotifications) {
// [formattedScheduledLocalNotifications addObject:RCTFormatLocalNotification(notification)];
// [formattedScheduledLocalNotifications addObject:RCTFormatLocalNotification(notification)];
// }
// resolve(@[formattedScheduledLocalNotifications]);
}
//}

RCT_EXPORT_METHOD(requestPermissions:(NSDictionary *)permissions
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
if (RCTRunningInAppExtension()) {
reject(@"E_UNABLE_TO_REQUEST_PERMISSIONS", nil, RCTErrorWithMessage(@"Requesting push notifications is currently unavailable in an app extension"));
return;
reject(@"E_UNABLE_TO_REQUEST_PERMISSIONS", nil, RCTErrorWithMessage(@"Requesting push notifications is currently unavailable in an app extension"));
return;
}

UIUserNotificationType types = UIUserNotificationTypeNone;
if (permissions) {
if ([RCTConvert BOOL:permissions[@"alert"]]) {
types |= UIUserNotificationTypeAlert;
}
if ([RCTConvert BOOL:permissions[@"badge"]]) {
types |= UIUserNotificationTypeBadge;
}
if ([RCTConvert BOOL:permissions[@"sound"]]) {
types |= UIUserNotificationTypeSound;
}
if ([RCTConvert BOOL:permissions[@"alert"]]) {
types |= UIUserNotificationTypeAlert;
}
if ([RCTConvert BOOL:permissions[@"badge"]]) {
types |= UIUserNotificationTypeBadge;
}
if ([RCTConvert BOOL:permissions[@"sound"]]) {
types |= UIUserNotificationTypeSound;
}
} else {
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;
}

if (@available(iOS 10.0, *)) {
Expand All @@ -188,33 +193,28 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
}
}

RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback)
{
if (RCTRunningInAppExtension()) {
callback(@[RCTSettingsDictForUNNotificationSettings(NO, NO, NO, NO, NO)]);
return;
}

RCT_EXPORT_METHOD(checkPermissions:(RCTResponseSenderBlock)callback) {
if (RCTRunningInAppExtension()) {
callback(@[RCTSettingsDictForUNNotificationSettings(NO, NO, NO, NO, NO)]);
return;
}

if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter.currentNotificationCenter getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {
callback(@[RCTPromiseResolveValueForUNNotificationSettings(settings)]);
}];
} else {
// Fallback on earlier versions
}
}
}

API_AVAILABLE(ios(10.0))
static inline NSDictionary *RCTPromiseResolveValueForUNNotificationSettings(UNNotificationSettings* _Nonnull settings) {
return RCTSettingsDictForUNNotificationSettings(settings.alertSetting == UNNotificationSettingEnabled,
settings.badgeSetting == UNNotificationSettingEnabled,
settings.soundSetting == UNNotificationSettingEnabled,
settings.lockScreenSetting == UNNotificationSettingEnabled,
settings.notificationCenterSetting == UNNotificationSettingEnabled);
}
return RCTSettingsDictForUNNotificationSettings(settings.alertSetting == UNNotificationSettingEnabled, settings.badgeSetting == UNNotificationSettingEnabled, settings.soundSetting == UNNotificationSettingEnabled, settings.lockScreenSetting == UNNotificationSettingEnabled, settings.notificationCenterSetting == UNNotificationSettingEnabled);
}

static inline NSDictionary *RCTSettingsDictForUNNotificationSettings(BOOL alert, BOOL badge, BOOL sound, BOOL lockScreen, BOOL notificationCenter) {
return @{@"alert": @(alert), @"badge": @(badge), @"sound": @(sound), @"lockScreen": @(lockScreen), @"notificationCenter": @(notificationCenter)};
}
return @{@"alert": @(alert), @"badge": @(badge), @"sound": @(sound), @"lockScreen": @(lockScreen), @"notificationCenter": @(notificationCenter)};
}

@end

0 comments on commit 25219d4

Please sign in to comment.