I want to cancel an iOS local notification after a particular time.
For example : a week later
- (void)ViewDidLoad
{
NSDate *date = [NSdate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
//set some localNotif's properties
localNotif.repeatInterval = NSDayCalendarUnit;
localNotif.fireDate = date
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
How can I cancel localNotif after a week (7 days) and can you show me the code?
You can't do that unless your app is running in the foreground at the time the notification is due to be cancelled (in which case there would be no need to cancel it anyway).
The reason you can't do it is because you would need a timer to tell you when to cancel it, and you can't schedule a timer unless you are an app that has a background mode, in which case you could schedule a timer to notify you - but even background apps can be suspended still it would not be guaranteed.
See here
iOS Run Code Once a Day
Related
I have simple UILocalNotification:
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = #"Message";
notification.alertAction = #"Action";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.category = kCategoryIdentifier;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
Is it possible, to repeat notification once, for example after two minutes? I want behaviour exacly, like in Messages app.
I have tried to set repeatInterval property of notification object, but:
Notification will be presented to user every two minutes, not repeated only once
System shows to user new notification, not repeat the old one. User see two notifications, one with timestamp 2 minutes after another.
Which is not what I've expected.
Also, because of second reason, I don't want to schedule two separate notifications.
Edit: In my app time when something happend is very important. Because of that, in lock screen, when notification is repeated, I want user to know that is something that happend earlier, not in time when notification arrives. So repeated notification should have timestamp of first notification.
Yes, you can set repeatInterval.
See documentation here
The calendar interval at which to reschedule the notification.
Declaration SWIFT var repeatInterval: NSCalendarUnit OBJECTIVE-C
#property(nonatomic) NSCalendarUnit repeatInterval
try this code
localNotif.timeZone = [NSTimeZone systemTimeZone];
localNotif.alertBody = #"Message";
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber=1;
NSLog(#"LocalNotif.soundName %#",localNotif.soundName);
for (int i=0; i<20; i++)
{
localNotif.fireDate = [repeatAlarm dateByAddingTimeInterval:120*i];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
I am working on Alarm module in my app. I have develop code which take date and time input from user and set Event on that particular time in my app.
Also i store information into my local database to display list of alarm set by user.
Now i want to delete entry from database when particular alarm executed (When UILocalNotification displayed into app i want to call database method to delete that entry from db)
I set Notification by this way
NSUserDefaults* preferences = [NSUserDefaults standardUserDefaults];
int notificationCount = [preferences integerForKey:#"notification_count"];
notificationCount++;
NSDate* final_date = [calendar dateFromComponents:final_Components];
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
localNotification.fireDate = final_date;
localNotification.alertBody=titleTextField.text;localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = 1;
NSDictionary* userInfoDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:notificationCount] forKey:#"notification_id"]; localNotification.userInfo = userInfoDic;
localNotification.repeatInterval = 0;
[[UIApplication sharedApplication]scheduleLocalNotification:localNotification];
[preferences setInteger:notificationCount forKey:#"notification_count"];
I used Delegate didReceiveLocalNotification
-(void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSString *itemName = [notification.userInfo objectForKey:#"notification_id"];
NSLog(#"userInfo is %#",itemName);
databaseObject.deleteNotification(itemName)
}
My problem is "didReceiveLocalNotification" only call in 2 case
1) When user using app (app in foreground).
2) when notification displayed and user click on notification
But in 3rd case when app is in background mode and notification displayed and if user don't click on notification Or second case is open app directly clicking on app icon or user clear notification at that time didReceiveLocalNotification delegate is not get called..
Is there any way to detect Notification fire in all case or any other method by using i can detect that notification has been fire and then i will execute my delete method.
Any help is appreciated
Thank you
Yes, you are right, you won't know the information of the notification if your notification fires while your app is suspended/terminated and user didn't tap the notification to launch/active your app.
The only way to do this is to calculate the time every time your app is running and reschedule notifications and update your database.
For millisecond calculation:
NSInteger myMillisecond; //assume it exists
const NSTimeInterval oneSecondAsMilliseconds = 1000.0;
NSTimeInterval myTimeInterval = myMillisecond/oneSecondAsMilliseconds;
NSDate *currentDate = [NSDate date];
NSTimeInterval currentTimeStamp = [currentDate timeIntervalSince1970];
if (myTimeInterval > currentTimeStamp) {
//myTimeInterval is a later time than now
}
I'm not sure if an iOS app can post a notification from the background without internet connection? (so this is not a push notification, just to post from phone)
Example: An iOS app that plays music in background can prompt user how long the user has listened to music from background.
I think you should use local notification, please see the below code for local notification,
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.soundName = #"bell_tree.mp3";
notifyAlarm.alertBody = #"Staff meeting in 30 minutes";
[app scheduleLocalNotification:notifyAlarm];
}
With these requirements in the mind, the following code creates an NSDate object based on the current date and time plus 10 seconds. This date object is then used to schedule a notification with no repeats, a text message and the sound from the audio file
The local notifications in iOS can help you to fix the problem. May be this tutorial helps you.
I am developing an app that will fire an alarm and play 5 secs of a song the user select, or the default sound every x time depending on the user input.. my problem is I do not know how to create this alarm, I have tried with UILocalNotification, but the problem is how I made the alarm sound again even if my application is on background?
here is what I've got so far:
UILocalNotification *notification = [[UILocalNotification alloc] init];
NSDate * secs = [[NSDate alloc] initWithTimeIntervalSinceNow:i*[secs doubleValue]];
notification.alertBody = #"Alarm";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.fireDate = secs;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
but how could I make the notification and also, how to make the sound play a user song even in background... I have also tried with NSTimer but it stops when the app is moved to background
Use NSTimer when the app is in the foreground.
Look at the repeatInterval property of UILocalNotification for running in the background.
I am working with UILocalNotification. My app will give user facility to set the reminder for any meeting and medicine in my application.
I am Able to schedule UINotification according to my Schedule date. And also When user will schedule past date notification my secound schedule notification is not fired. After secound others are firing perfect but secound is not firing.
Following is my flow for notification:
I am creating 3 schedule for meeting one of its time is past of my current time say for e.g. 2014-01-28 18:00. And my current device time is 2014-01-28 19:00. Now i am creating another schedule its time is i.e. 2014-01-28 19:10. And another schedule date and time is i.e. 2014-01-28 19:20.
With the above schedule date time reference my first notification which was of past date was fired after schedule create.
When my current time of device come to 2014-01-28 19:10. I was especting notification. but it wount fire.
And at 2014-01-28 19:20 my 3rd notification fires as regularly. this things happen for me every times when I add past date as a schedule.
Following is my schedule notification code which i used to schedule notification:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = fireDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = alertText;
localNotification.alertAction = alertAction;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertLaunchImage = launchImage;
localNotification.applicationIconBadgeNumber = 1;
localNotification.userInfo = userInfo;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[UIApplication sharedApplication]registerForRemoteNotificationTypes:
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeSound];
Also Following is my idReceiveLocalNotification: method to hendle notification:
if([UIApplication sharedApplication].applicationIconBadgeNumber == 0) {
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
}else{
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
}
Also when i get notification at that time i will call MkLocalNotificationScheduler class to display alert which code is as follows:
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:#"Reminder"
message:[thisNotification alertBody]
delegate:self
cancelButtonTitle:#"NO"
otherButtonTitles:#"YES",nil] autorelease];
alert.tag = 1;
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:YES];
Please help me how to work on to get all Schedule notification weather it is past date or date yet to be come.
Do not create UILocalNotification for time that is past. The behavior that you are seeing is typical of UILocalNotification. It fires past event right away. So in your code check if the time has past and if it is then don't create a UILocalNotification. (also its non-sense to create a notification of past time)
Only create new UILocalNotification for times that are in future of your current time.