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.
Related
I had set the countdown timer for 1 hour 40 minutes and i want to set local notification for last 10 minutes and last 5 minutes remaining.
UILocalNotification *notification = [UILocalNotification new];
notification.repeatInterval = 0;
notification.timeZone = [NSTimeZone systemTimeZone];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5400];
notification.alertBody = #"Some body";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
I had tried this but its not working.
You need set the notification fireDate
UILocalNotification *notification = [UILocalNotification new];
notification.repeatInterval = 0;
notification.timeZone = [NSTimeZone systemTimeZone];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:*YOUR 1 hour 30 minutes*];
notification.alertBody = #"Some body";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
and
UILocalNotification *notification = [UILocalNotification new];
notification.repeatInterval = 0;
notification.timeZone = [NSTimeZone systemTimeZone];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:*YOUR 1 hour 35 minutes*];
notification.alertBody = #"Some body";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
Do like this,i hope, this will help you...
//Create Notification here
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = someTime;
localNotification.alertBody = #"Your time will expire in 10 mins";
localNotification.alertAction = #"View";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
Do same for last five min.
My time period is 60 you can change according to your choice.
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:**60**];
localNotification.alertBody = #"your message you want to show in notification";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
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";
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];
I'm trying to use a settings bundle to schedule a UILocalNotification. In settings, you can choose if you want the notifications to come daily (1/day), 1 every 2 days, only on Sundays or never.
Here is the code I used (this is all in AppDelegate.m):
-(void)defaultsChanged:(NSNotification *)notification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[[NSUserDefaults standardUserDefaults]synchronize];
NSString *testValue = [[NSUserDefaults standardUserDefaults] stringForKey:#"multi_preference"];
NSLog(#"%#", testValue);
NSDate *today = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* compoNents = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:today]; // Get necessary date components
[compoNents month];[compoNents day];
NSDictionary *dictToday= [self getDataFromdate : [compoNents day] month:[compoNents month]];
if ([testValue isEqualToString:#"one"]){
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date]dateByAddingTimeInterval:20];
localNotification.alertAction = #"View";
localNotification.alertBody = [dictToday objectForKey:#"saint_description"];
localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
}
if (testValue==Nil){
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date]dateByAddingTimeInterval:20];
localNotification.alertAction = #"View";
localNotification.alertBody = [dictToday objectForKey:#"saint_description"];
localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
}
if ([testValue isEqualToString:#"two"]){
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [[NSDate date]dateByAddingTimeInterval:86401];
localNotification.alertAction = #"View";
localNotification.alertBody = [dictToday objectForKey:#"saint_description"];
localNotification.repeatInterval = NSDayCalendarUnit;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
}
if ([testValue isEqualToString:#"three"]){
NSDate *today2 = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
// Get the weekday component of the current date
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit
fromDate:today2];
/*
Create a date components to represent the number of days to subtract from the current date.
The weekday value for Sunday in the Gregorian calendar is 1, so subtract 1 from the number of days to subtract from the date in question. (If today is Sunday, subtract 0 days.)
*/
NSDateComponents *componentsToSubtract = [[NSDateComponents alloc] init];
[componentsToSubtract setDay: 0 - ([weekdayComponents weekday] - 1)];
NSDate *beginningOfWeek = [gregorian dateByAddingComponents:componentsToSubtract
toDate:today2 options:0];
/*
Optional step:
beginningOfWeek now has the same hour, minute, and second as the original date (today).
To normalize to midnight, extract the year, month, and day components and create a new date from those components.
*/
NSDateComponents *components =
[gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit |
NSDayCalendarUnit) fromDate: beginningOfWeek];
beginningOfWeek = [gregorian dateFromComponents:components];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = beginningOfWeek;
localNotification.alertAction = #"View";
localNotification.alertBody = [dictToday objectForKey:#"saint_description"];
localNotification.repeatInterval = NSWeekdayCalendarUnit;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
}
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(defaultsChanged:)
name:NSUserDefaultsDidChangeNotification
object:nil];
UILocalNotification *locationNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (locationNotification) {
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
return yes;
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Today's Saint"
message:notification.alertBody
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
if (notification.alertBody!=Nil)
[alert show];
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
// Set icon badge number to zero
application.applicationIconBadgeNumber = 0;
}
Is the code correct for launching the notifications as I've stated?
If not, what's the problem with it? Thanks!
It seems you are scheduling notifications whenever the user changes preferences. However, you never unschedule previously scheduled notifications, which is why you are observing bursts of notifications at times that will correspond exactly to the times you repeatedly changed settings a day or two days ago.
The notifications you schedule are different objects than the set of notifications that you intend to modify. Unfortunately, UILocalNotifications have no identifier tokens.
However, you can unschedule all previous notifications whenever you receive a defaultsChanged: message with [[UIApplication sharedApplication] cancelAllLocalNotifications]; before you reschedule. This will solve your problem.
Also have a close look at this solution which suggests to cancel and reschedule notifications even upon launch to avoid bursts or duplicate notifications when a user re-installs your app.
I have two ideas,
First: what's the value of dictToday ?
NSDictionary *dictToday= [self getDataFromdate : [compoNents day] month:[compoNents month]];
[dictToday objectForKey:#"saint_description"]
If this value is nil, your notification will not pop.
Second: Check your time zone and the time zone returned from:
[NSTimeZone defaultTimeZone]
You could be on a +3 time zone and setting your localnotification to fire on a negative time zone and that is never going to happen.
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];