I have the code below in my AppDelegate.m applicationDidEnterBackground which will display a local notification 10 seconds after closing the app.
However, the time stamp (and any other data I want to include) is whatever it is at the time of creating the notification.
I would like to be able to show the latest data at the point of displaying the notification, as opposed to the data at the time of creating it.
Can I do this with the notification, or do I need to do something with background activity? And if so, what do I call / how in order to schedule that activity?
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh-mm-ss"];
NSString *resultString = [dateFormatter stringFromDate: currentTime];
NSLog([NSString stringWithFormat:#"Time: %#",resultString]);
NSDate *alertTime = [[NSDate date]
dateByAddingTimeInterval:10];
UIApplication* app = [UIApplication sharedApplication];
UILocalNotification* notifyAlarm = [[UILocalNotification alloc]
init];
if (notifyAlarm)
{
notifyAlarm.fireDate = alertTime;
notifyAlarm.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval = 0;
notifyAlarm.alertBody = resultString;
[app scheduleLocalNotification:notifyAlarm];
}
AFAIK you are not able to show dynamic data in local notifications. If that dynamic data comes from server, then I'd suggest to use push notifications and build their messages with that dynamic data.
If your app is VoIP or Location-based app you can use corresponding background modes and then schedule local notification as soon as new data was polled.
Related
I am working on Alarm base project in iOS objective c.
I had implemented UILocalnotification for alarm when application is in background.
But problem i am facing is sound only plays for 30sec.
How can i increase the time interval for the sound or is there any other way to implement it instead of UILocalnotification .
- (IBAction)Alert:(id)sender{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd"];
//NSDate *date = [NSDate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate =[NSDate dateWithTimeIntervalSinceNow:15];
localNotif.timeZone = [NSTimeZone localTimeZone];
localNotif.alertBody = #"Emergency";
localNotif.alertAction = #"View";
localNotif.soundName = #"police.mp3";
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSYearCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Note : mp3 file i am using for UILocalnotification is of more that 60min duration.
Please advice.
Thank you
If device is in active mode, notification sound only play for 5 seconds. If device is in sleep mode, notification will play 30 seconds sound. I guess, this is standard behavi
As per Apple guidelines, you need to use mp3 file with 30 seconds duration. Please check below link.
https://developer.apple.com/reference/usernotifications/unnotificationsound
Note: FYI, UILocalNotification is deprecated from iOS10. You need to start using UNUserNotificationCenter.
As the title says: How do I determine how long my iOS App has been closed or has been in the background? I need to know this since I want to call a method if the app has been closed or has been in the background for more than 3 hours.
You can track the time application was is background/killed by saving times in NSUserDefaults and then use them once your application is re-launched. Try this code (I have formatted the dates as I used them further in my app in the formatted way. You may choose to ignore date formatting.):
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *backGroundTime = [dateFormat stringFromDate:[NSDate date]];
[[NSUserDefaults standardUserDefaults]setValue:backGroundTime forKey:#"backGroundTime"];
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSString *foreGroundTime = [dateFormat stringFromDate:[NSDate date]];
NSString *backGroundTime = [[NSUserDefaults standardUserDefaults]valueForKey:#"backGroundTime"];
[self minCalculation_backgroundtime:backGroundTime forgroundTime:foreGroundTime];
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
// Call this method to calculate the duration of inactivity
-(void)minCalculation_backgroundtime:(NSString *)backgroundTime forgroundTime:(NSString *)foreGroundTime
{
NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];
[dateformat setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSDate *lastDate = [dateformat dateFromString:foreGroundTime];
NSDate *todaysDate = [dateformat dateFromString:backgroundTime];
NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
NSTimeInterval dateDiff = lastDiff - todaysDiff;
int min = dateDiff/60;
NSLog(#"Good to see you after %i minutes",min);
}
You can save time in NSUSerDefaults for going in background. When your app comes back on foreground, you can get difference of that time.
When your app will go in background this method will execute - (void)applicationDidEnterBackground:(UIApplication *)application and when it will come back in foreground - (void)applicationWillEnterForeground:(UIApplication *)application this method will get call.
Hello i am facing a weird kind problem. Actually i want to schedule a daily notification (only once a day) at 8:00 AM. Below is my code for scheduling daily notification.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm"];
NSDate *date = [[NSDate alloc] init];
date = [formatter dateFromString:#"08:00"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone=[NSTimeZone defaultTimeZone];
localNotification.alertBody = #"You just received a local notification";
localNotification.alertAction = #"View Details";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[formatter release];
[date release];
My problem is, I receive 2 local notifications. One at 8:00 AM and other at 10:00 AM. Why i am getting notification at 10:00 AM. I am scheduling it at 8:00 AM only. I know UILocalNotification library have some other weird kind of problems/bugs on most of apple devices. I just want to confirm whether there is some mistake in my code or it is a weird behaviour of UILocalNotification Library. I dont know why Apple is not working on resolving the issues being reported by many developers about UILocalNotification.
Note: I am using Xcode 4.6 and iOS 6.1
This would be the BEST link to get your answer.
I wonder if it's possible to set end date for UILocalNotification?
I want my notification to fire everyday (NSDayCalendarUnit) but I have end date which I cannot cross (deadline) e.g. I'm taking a photo of my growing mustache everyday for one year period and after a year notifications won't be displayed.
I hope you got my point of view...
There is not such option in UILocalNotification as you can read in the documentation.
Your only option is to check wether the year is over when every the user starts the app.
In the UILocalNotification object, I'd recommend setting the repeatInterval property and putting the end date in the userInfo dictionary for querying later to determine if the notification has expired. For example:
UILocalNotification* uiLocalNotification;
uiLocalNotification = [[UILocalNotification alloc] init];
//Set the repeat interval
uiLocalNotification.repeatInterval = NSCalendarUnitDay;
NSDate* fireDate = ...
//Set the fire date
uiLocalNotification.fireDate = fireDate;
//Set the end date
NSDate* endDate = ...
uiLocalNotification.userInfo = #{
#"endDate": endDate
};
UIApplication* application = [UIApplication sharedApplication];
[application scheduleLocalNotification:uiLocalNotification];
//...
//Somewhere else in your codebase, query for the expiration and cancel, if necessary
UIApplication* application = [UIApplication sharedApplication];
NSArray* scheduledNotifications = application.scheduledLocalNotifications;
NSDate* today = [NSDate date];
for (UILocalNotification* notification in scheduledNotifications) {
NSDate* endDate = notification.userInfo[#"endDate"];
if ([today earlierDate:endDate] == endDate) {
//Cancel the notification
[application cancelLocalNotification:notification];
}
}
Use following code:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = NSDayCalendarUnit;
I know this question has asked many times on StackOverflow but i couldn't able to set alarm in my app because i am very new to iOS? I am following this tutorial to set an alarm:
Setting a reminder using UILocalNotification in iOS.
However, it doesn't seems to be working for me.
I am in need to set alarm daily lets say 5.00 PM daily. I can't use date picker for choosing the time.
First on your xib, (or code) set the date picker mode: Time (Default is date & time)
The system assumes that the firedate is the current date, and the time is the time the user have chosen. This is not a problem because you set a repeat interval so it will work. I have tested it.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setFireDate:datePicker.date];
[localNotif setRepeatInterval:NSDayCalendarUnit];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
PS: It would be a good idea to set the seconds to 0 using NSDateComponents class so as to set the alarm to ring at the first second of the minute you want. You can check the:
Local notifications in iOS.
tutorial you posted on how to do this.
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID
Call this method with parameters and use this
+ (void)addLocalNotification:(int)year:(int)month:(int)day:(int)hours:(int)minutes:(int)seconds:(NSString*)alertSoundName:(NSString*)alertBody:(NSString*)actionButtonTitle:(NSString*)notificationID {
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
//set the notification date/time
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:hours];
[dateComps setMinute:minutes];
[dateComps setSecond:seconds];
NSDate *notificationDate = [calendar dateFromComponents:dateComps];
[dateComps release];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Set notification message
localNotif.alertBody = alertBody;
// Title for the action button
localNotif.alertAction = actionButtonTitle;
localNotif.soundName = (alertSoundName == nil) ? UILocalNotificationDefaultSoundName : alertSoundName;
//use custom sound name or default one - look here to find out more: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html%23//apple_ref/doc/uid/TP40008194-CH103-SW13
localNotif.applicationIconBadgeNumber += 1; //increases the icon badge number
// Custom data - we're using them to identify the notification. comes in handy, in case we want to delete a specific one later
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:notificationID forKey:notificationID];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
You may need to change the style of the date picker to allow changing the time in addition to just the date.
You can try this
UILocalNotification *todolistLocalNotification=[[UILocalNotification alloc]init];
[todolistLocalNotification setFireDate:[lodatepicker date]];
[todolistLocalNotification setAlertAction:#"Note list"];
[todolistLocalNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[todolistLocalNotification setAlertBody:text_todolist];
[todolistLocalNotification setHasAction:YES];