UILocalNotification failed - ios

The local notification never appeared. Can somebody tell me why?
- (IBAction)addReminder:(id)sender {
self.datePicker.timeZone = [NSTimeZone timeZoneWithName: #"Asia/Tokyo"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *formatedDate = [dateFormatter stringFromDate:self.datePicker.date];
NSLog(#"formatedDate>>>>>:%#", formatedDate);
NSDate *date = [dateFormatter dateFromString:formatedDate];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
NSLog(#"Setting a reminder for %#", localeDate);
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif. fireDate = localeDate;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.alertBody = #"ZEIT!";
localNotif.alertAction = #"Show me the Timer!";
localNotif.timeZone = zone;
localNotif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] +1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Result:
formatedDate>>>>>:2015-08-09 23:32:00 Setting a reminder for 2015-08-09 23:32:00 +0000

Make sure you are requesting permission from the user to show notifications.
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

To : kmcgrady , thanks for your answer, but i tried this and it worked
NSDate *now=[NSDate new];
localNotif.fireDate=[now dateByAddingTimeInterval:12];

Related

actions and categories don't show in UILocalNotification in iOS

I have the following which I believe from the apple documentation here is all I need to have a category for UILocalNotification:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIMutableUserNotificationAction *acceptAction =
[[UIMutableUserNotificationAction alloc] init];
// Define an ID string to be passed back to your app when you handle the action
acceptAction.identifier = #"ACCEPT_IDENTIFIER";
// Localized string displayed in the action button
acceptAction.title = #"Accept";
// If you need to show UI, choose foreground
acceptAction.activationMode = UIUserNotificationActivationModeBackground;
// Destructive actions display in red
acceptAction.destructive = NO;
// Set whether the action requires the user to authenticate
acceptAction.authenticationRequired = NO;
// First create the category
UIMutableUserNotificationCategory *inviteCategory =
[[UIMutableUserNotificationCategory alloc] init];
// Identifier to include in your push payload and local notification
inviteCategory.identifier = #"INVITE_CATEGORY";
// Add the actions to the category and set the action context
[inviteCategory setActions:#[acceptAction]
forContext:UIUserNotificationActionContextDefault];
// Set the actions to present in a minimal context
[inviteCategory setActions:#[acceptAction]
forContext:UIUserNotificationActionContextMinimal];
NSSet *categories = [NSSet setWithObjects:inviteCategory, nil];
UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:categories];
[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(#"Recieved Notification %#",localNotif);
}
return YES;
}
Here is how I construct my local notification:
NSDate *dateChosen = [self.reminderDatePicker date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:dateChosen];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
// NSCalendar *calendar = [NSCalendar currentCalendar];
// NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay: 3];
[components setMonth: 7];
[components setYear: 2012];
[components setHour: hour];
[components setMinute: minute];
[components setSecond: 0];
[calendar setTimeZone: [NSTimeZone defaultTimeZone]];
NSDate *dateToFire = [calendar dateFromComponents:components];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateToFire;
[localNotification setRepeatInterval: kCFCalendarUnitDay];
NSLog(#"Notification will be shown on: %# ",localNotification.fireDate);
localNotification.userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:self.dayPeriod, #"name", nil];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = alertMessage;
localNotification.alertAction = NSLocalizedString(#"View details", nil);
localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = -1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
It just shows the default notification with a dismiss button and not my accept button.
How can I get this "accept" button to show on my local notification?
The one thing that your missing in your main body of code for UILocalNotification is explicitly telling it to be a category based notification.
Add the category property to your notification:
localNotification.category = #"INVITE_CATEGORY";

More than one local notification with iOS 8

I have about 60 local notifications in different switches but when I try it and apply more than one switch, it runs only for the last one.
Here's my code:
-(IBAction)theSwitch:(id)sender{
NSUserDefaults *defaultsB = [NSUserDefaults standardUserDefaults];
if (switcher.on == 1) {
NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit | NSWeekCalendarUnit fromDate:[NSDate date]];
[dateComponent setWeekday:1]; // For sunday
[dateComponent setHour:16];
[dateComponent setMinute:30];
NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:textClass1.text];
[notification setFireDate:fireDate];
notification.soundName = #"bells.mp3";
notification.repeatInterval = NSWeekCalendarUnit;
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[defaultsB setObject:#"ON" forKey:#"SwitchState"];
}
else { UIApplication *app=[UIApplication sharedApplication];
[app cancelAllLocalNotifications];
[defaultsB setObject:#"OFF" forKey:#"SwitchStateOFF"];
}
}

Past notification appearing when app closes

I try to make an app that has to notify 4 deferent notification in a day for whole year this is my code when I run the code its work but the problem is if date was already passed for example 2014-03-12 01:12 the notification should not appear but it is appearing when the app close and I try with these as well [[UIApplication sharedApplication] cancelAllLocalNotifications]; or [[UIApplication sharedApplication] cancelLocalNotifications];.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *myDate = [df dateFromString:#"2014-03-10 01:04"];
NSDate *myDate1 = [df dateFromString:#"2014-03-10 01:21"];
NSDate *myDate2 = [df dateFromString:#"2014-03-10 01:22"];
UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.fireDate = myDate;
n1.alertBody = #"one";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
UILocalNotification* n2 = [[UILocalNotification alloc] init];
n2.fireDate = myDate1;
n2.alertBody = #"uuu";
[[UIApplication sharedApplication] scheduleLocalNotification: n2];
UILocalNotification* n3 = [[UILocalNotification alloc] init];
n3.fireDate = myDate2;
n3.alertBody = #"ontt";
[[UIApplication sharedApplication] scheduleLocalNotification: n3];

NSLocal Notification Repeatedly Loop the Alarm

I am working with Alarm App. I am create Alarm using NSLocal notification. The Alarm is working fine. My problem is I need to repeatedly loop the Alatm without interval.
My code:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[localNotification setAlertAction:#"Launch"];
[localNotification setAlertBody:msg];
[localNotification setHasAction: YES];
localNotification.soundName = soundFile;
localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatCalendar = [NSCalendar currentCalendar];
localNotification.repeatInterval = kCFCalendarUnitSecond;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Any one help me.
check the below answer. Its simple idea. You can add 1 minute and then send LocalNotification repeatly.
int myInt=60;
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[localNotification setAlertAction:#"Launch"];
[localNotification setAlertBody:msg];
[localNotification setHasAction: YES];
localNotification.soundName = soundFile;
localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatCalendar = [NSCalendar currentCalendar];
localNotification.repeatInterval = kCFCalendarUnitSecond;
NSDate *datePlusOneMinute = [date dateByAddingTimeInterval:myInt];
UILocalNotification *localNotification1 = [[UILocalNotification alloc] init];
[localNotification1 setFireDate:datePlusOneMinute];
localNotification1.timeZone = [NSTimeZone defaultTimeZone];
[localNotification1 setAlertAction:#"Launch"];
[localNotification1 setAlertBody:msg];
[localNotification1 setHasAction: YES];
localNotification1.soundName = soundFile;
localNotification1.applicationIconBadgeNumber = 1;
localNotification1.repeatCalendar = [NSCalendar currentCalendar];
localNotification1.repeatInterval = kCFCalendarUnitSecond;
NSDate *datePlusOneMinute1 = [datePlusOneMinute dateByAddingTimeInterval:myInt];
UILocalNotification *localNotification2 = [[UILocalNotification alloc] init];
[localNotification2 setFireDate:datePlusOneMinute1];
localNotification2.timeZone = [NSTimeZone defaultTimeZone];
[localNotification2 setAlertAction:#"Launch"];
[localNotification2 setAlertBody:msg];
[localNotification2 setHasAction: YES];
localNotification2.soundName = soundFile;
localNotification2.applicationIconBadgeNumber = 1;
localNotification2.repeatCalendar = [NSCalendar currentCalendar];
localNotification2.repeatInterval = kCFCalendarUnitSecond;
.....
....
...
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification1];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
.....
....
...
How many times you need. you can create repeatly.

UILocalNotification not being fired on right time

I am using UILocalNotification but it is not firing on time rather it notifies me 8-10 min after the specified time. Here is the code i am using
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSDate *notificationDate = [dateFormat dateFromString:dateString];
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
// Notification details
localNotif.alertBody = #"Appear";
// Set the action button
localNotif.alertAction = #"Action";
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
I am using this in didEnterRegion delegate of CLLocationManager. What i am doing wrong?
This is Working Code:
UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.fireDate = [NSDate dateWithTimeIntervalSinceNow: 60];
n1.alertBody = #"one";
UILocalNotification* n2 = [[UILocalNotification alloc] init];
n2.fireDate = [NSDate dateWithTimeIntervalSinceNow: 90];
n2.alertBody = #"two";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
[[UIApplication sharedApplication] scheduleLocalNotification: n2];

Resources