custom ui local notification by user setting - ios

I know how to make a local notification in the applicationDidEnterBackground function in AppDelegate.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification * uln = [[UILocalNotification alloc] init];
uln.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
uln.timeZone = [NSTimeZone defaultTimeZone];
uln.alertBody = #"Did you forget something?";
uln.alertAction = #"Show me";
uln.soundName = UILocalNotificationDefaultSoundName;
//uln.applicationIconBadgeNumber = 0;
uln.repeatInterval = NSMinuteCalendarUnit;
application.scheduledLocalNotifications = [NSArray arrayWithObject:uln];
}
But is that possible a user can set a time for the fireDate and a do not disturb time like late in midnight?

Yes you can set the firedate property to any date in the future. Do not disturb is possible for the user to trigger himself in Settings in iOS6.

Related

Programmatically Create Reminder For NSUserActivity

With iOS 9, NSUserActivities can be used with Siri, for example, "Siri, remind me of this in an hour", creates the reminder with her knowing the context of what "THIS" is. Is there a way to programmatically do that, so at the click of a button you could create a reminder?
You could simply schedule a UILocalNotification that will remind the user:
NSDate *dateInOneHour = [[NSDate date] dateByAddingTimeInterval:3600];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = dateInOneHour;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"Don't forget about THIS";
localNotif.alertAction = #"View";
localNotif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
Not an answer as such, but a path to one: since reminders a la the Reminders app are managed by EventKit (see https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/EventKitProgGuide/Introduction/Introduction.html), try making a Smart Reminder using Siri, then examine the resulting reminder using the API. At a guess, it's using the -URL property on the reminder.

WebServices Call when application in Background and show the response in Local Notification

I have an enterprise application that I want to keep running, so it can call a webservice and inform to the user through Local Notification.
So, it now runs in the background, it makes the calls, gets results, informing to the user.
I Used a timer in applicationDidEnterBackground. My Code is,
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// 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.
NSLog(#"ENTER BACKGROUND");
if (UIApplication.sharedApplication.applicationState == UIApplicationStateBackground)
{
backgroundTimer=nil;
[backgroundTimer invalidate];
backgroundTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(backgroundTasks) userInfo:nil repeats:YES];
}
application.applicationIconBadgeNumber = 0;
}
When the timer is triggered, i will check the date condition. If Date is matched then i have to make webservice call.
-(void)backgroundTasks
{
NSDate *CurrentDate = [NSDate date];
NSDate *previousDate = [[NSUserDefaults standardUserDefaults]valueForKey:#"ScheduledTime"];
NSLog(#"Current Date : %# - Previous Date : %#",CurrentDate,previousDate);
NSString *CurrentdateString = [NSString stringWithFormat:#"%#",CurrentDate];
NSString *PreviousdateString = [NSString stringWithFormat:#"%#",previousDate];
NSLog(#"Current Date String : %# - Previous Date String : %#",CurrentdateString,PreviousdateString);
if ([PreviousdateString isEqualToString:CurrentdateString])
{
NSLog(#"Both dates are same");
previousDate = [previousDate dateByAddingTimeInterval:60];
[[NSUserDefaults standardUserDefaults] setObject:previousDate forKey:#"ScheduledTime"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self updateUserLatLong:str_Latitude :str_Longitude]; //Webservices call
}
else
{
NSLog(#"Dates are different");
}
}
I got the response from this request, at that time i will create Local Notification. like this,
str_Condition = [NSString stringWithFormat:#"%#",[conditionarray lastObject]];
str_TempFaren = [NSString stringWithFormat:#"%#",[mutArr_High lastObject]];
NSLog(#"Condition : %#, Temperature : %#",str_Condition,str_TempFaren);
NSLog(#"SHOW ALERT NOTIFICATION");
NSLog(#"-----------------------------------------------------------------------");
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = [NSDate date];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithFormat:#"%#° F - %#, See today's recommendations",str_TempFaren,str_Condition];
localNotif.alertAction = #"Reminder";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber =0;
[[UIApplication sharedApplication]presentLocalNotificationNow:localNotif];
I Create Default Notification in another class, like this.
pickerDate = [datePicker date];
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = #"See today's recommendations";
localNotification.alertAction = #"Reminder";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSMinuteCalendarUnit;
// localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
My Problem is, when the user remove the application from memory, obviously we can't make any webservices call. So when the app is removed from the memory, i want to show the message in localnotification which contains default alertbody. Now Both scenarios are working. but i want to display only one notification. if app removed from memory then only i have to show default notification. How to handle this problem, Please help me. i was strucked more than 7 hours for this issue.
I found the answer by myself. Solution is, I create the default UILocalNotification in the method,
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSDate *pickerDate = [[NSUserDefaults standardUserDefaults]valueForKey:#"ScheduledTime"];
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
localNotification.alertBody = #"See today's recommendations";
localNotification.alertAction = #"Reminder";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSHourCalendarUnit;
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
application.applicationIconBadgeNumber = 0;
}
So, when i remove the app from memory. That time i create this notification.

Perform Action At DatePicker Time

I need to send a text message when the current time equals the time selected in the UIDatePicker. How might I do this? You don't need to include the code to send the message, I already have that coded. I've tried all sorts of things with NSTimer and if - then statements but none have worked.
Edit: Since I wrote this question I've found a better way to do things. I just need to set a local notification and when received execute my code with -(void)didRevieveLocalNotification. Here is what I have so that any googlers can hopefully be helped.
NSDate *pickerDate = [self.datePicker date];
//Set Local Notification
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = pickerDate;
notif.timeZone = [NSTimeZone defaultTimeZone];
//----------------------------------------------------------------------
notif.alertBody = #"Tap to send your text message!";
notif.alertAction = #"send message...";
notif.soundName = #"sms_alert_nova.caf";
notif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
well i would use a local notification... something like this
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = theDate //The date that your picker has selected
notification.alertBody = #"Hey, the time just expired!"
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
Then in your AppDelegate
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
//Code to manage the notification logic
}
Hope this helps, the user will get the alert even if on background.. if on background the user must click the alert to let your application know that the local notification triggered, if he does (or he is on your app already, then the app delegate method will trigger letting your app know that the notification fired...
Hope this helps!

send local push - From Date to To Date with interval of hours

Below is the code I am using to send local push.
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
NSString *alertText = [NSString stringWithFormat:#"Test Push Title"];
localNotification.alertBody = alertText;
localNotification.soundName = #"Glass.caf";
localNotification.alertAction = #"Show me the item";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
// Request to reload table view data
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
But now I want to send push from to date with interval of hours.
E.g. I want to send push from 1-Jan-2014 to 2-Feb-2014.
Now daily I will have 3 push. Each push will start at 8 pm and in a day after 6 after I will get rest push. Means first push at 8am, second at 2pm and 3rd push at 8 pm.
Any idea how to do this?
Any guidance would be greatly appreciated.
Actually I want to do the reminder where I will have below inputs.
Daily how many times you will have medicine
From what time you will have medicine
After how many interval you will have another medicine
For how long days you will have medicine
Edit 1
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = pickerDate;
NSTimeInterval interval = 1*60;
for (int i =0;i<=2;i++ ) {
// setup the notification using the fire date
// pickerDate = [pickerDate dateByAddingTimeInterval:interval];
pickerDate = [pickerDate dateByAddingTimeInterval:interval];
}
I tried with this code, but still I am getting one push. I was expecting 4 pushes...
You can have up to 64 scheduled notification so be aware that there is a limit.
You can put your above code into a loop and add the interval date components (using a calendar) or the interval seconds (dateByAddingTimeInterval) onto the fire date of the previous notification.
Aside, this is pointless:
[NSString stringWithFormat:#"Test Push Title"]
And wasteful. There is no format, it's just a string so just directly use #"Test Push Title".
NSDate *fireDate = pickerDate;
NSTimeInterval interval = ...;
for ( however you decide how many notifications there are ) {
// setup the notification using the fire date
fireDate = [fireDate dateByAddingTimeInterval:interval];
}

ios local notification check before fire

I am trying to run some function every n-minutes when app is not running.
I thing local notification is suitable for that, but I have one problem.
If I set local notification as :
-(void)notification{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [[NSDate date] dateByAddingTimeInterval:15];
//localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = #"marko";
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"someValue" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
The notification is fired every time and then do:
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
NSLog(#"Recieved Notification %#",notif);
}
What I would want is to after 15s first fire some function and if function return YES then play sound and put badge on it.
How to do that?
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it.
Make sure you are testing in device.

Resources