I need to show certain local notifications while app is in the background. How can I do it, without the help of NSNotificationCenter?
Andrey Chernukha in first comment is right.
Here is the code to implement simple Local Notification:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.repeatInterval = 0;
NSDate *now = [NSDate date];
NSDate *dateToFire = [now dateByAddingTimeInterval:10];
localNotification.fireDate = dateToFire;
localNotification.alertBody = #"Test local notification";
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Related
ViewController:
- (void)viewDidLoad
{
`[super viewDidLoad];`
dateFormatter.dateFormat = #"dd/MM";
timeFormatter.dateFormat = #"HH:mm";
NSString *dateString = [dateFormatter stringFromDate:dateTime];
NSString *timeString = [timeFormatter stringFromDate:dateTime];
if ([dateString isEqual:#"23/06"]) {
if ([timeString isEqual:#"23:30"]) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
}
AppDelegate:
[[UIApplication sharedApplication] scheduledLocalNotifications];
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];
}
The notification isn't receiving when time equals string and date.
Please help!
The core issue with this (aside from using strings to compare dates) is that viewDidLoad gets called only once in this application. Because you are only scheduling the local notification when the current date is 11:30, it will never get called, since you also set the fireDate to the same exact time.
The thing is, local notifications get scheduled prior to the event. In fact, you should set dateTime to the desired schedule time and then set the local notification.
For example:
- (void)viewDidLoad {
[super viewDidLoad];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:2016];
[dateComponents setMonth:6];
[dateComponents setDay:23];
[dateComponents setHour:23];
[dateComponents setMinute:30];
dateTime = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:#"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Based on your uploaded code, it looks like you are going to be grabbing the date from the datePicker, so eventually, you will move the UILocalNotification portion to the button method and use [datePicker date] as the fireDate. Meaning, you can ignore the dateComponents part.
I am creating an alarm app. I want user set time and Local notification come at that time. For this i am using following code:-
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = _datePicker.date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = #"This is the Alert-Body Text";
localNotif.alertAction = #"Button-Text";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = NSDayCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
But the problem is notification comes only once and how i set multiple times for local notification so that it comes for every alarm.
In my app I am using UILocalNotifications. I want a functionality in which user can repeat alarms for specific days. Like(Monday,Wednesday,Sunday) at say 9pm. How can i do this.
Thanks in Advance
I am using the following code
UILocalNotification *notification = [[UILocalNotification alloc]init];
[notification setAlertBody:alertBody];
[notification setFireDate:[myDatePicker date]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = notification.applicationIconBadgeNumber+1;
[[UIApplication sharedApplication]scheduleLocalNotification:notification];
There's a repeatInterval property on the noitification , use that.
Err... ...why don't just schedule three notifications for the three days, each with a week as repreatInterval?
Try this code
NSDate *AlarmTime1 =[[NSDate date] dateByAddingTimeInterval:expiretime];
UIApplication *app1 = [UIApplication sharedApplication];
UILocalNotification *notifyAlarm1 = [[UILocalNotification alloc] init];
if (notifyAlarm1) {
notifyAlarm1.fireDate = AlarmTime1;
notifyAlarm1.timeZone = [NSTimeZone defaultTimeZone];
notifyAlarm.repeatInterval=0;
notifyAlarm.soundName= #"applering_zwnNYFkA.wav";
notifyAlarm1.soundName=#"stop.wav";
notifyAlarm1.alertBody =NSLocalizedString(#"EXPIRE_DESC", nil);
[app1 scheduleLocalNotification:notifyAlarm1];
I am trying to schedule a notification to be delivered at 7 AM. But it gets delivered at 12 AM. Please help me what I am doing wrong. This is my code.
NSString *date = #"4/14/2013 07:00";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy HH:mm"];
NSDate *thisDate = [df dateFromString:date];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = thisDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = #"This is notification";
localNotification.alertAction = #"view";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertLaunchImage = Nil;
self.badgeCount ++;
localNotification.applicationIconBadgeNumber = self.badgeCount;
localNotification.userInfo = Nil;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
I expect this notification to be delivered at 7 AM. Instead it pops up at 12 AM. What is going wrong here ?
I bet localNotification.timeZone and df.timeZone are not the same...
Add this to your DateFormatter:
df.timeZone = [NSTimeZone defaultTimeZone];
I am developing an IOS application with phonegap and need to set local notification for it which will repeat on every friday and specified time
Also there is requirement that user will decide to receive or not the local notification
I recommend you read the following article on the topic which i found very helpful
http://useyourloaf.com/blog/2010/7/31/adding-local-notifications-with-ios-4.html
- (void)scheduleNotification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *notif = [[UILocalNotification alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Body";
notif.alertAction = #"AlertButtonCaption";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
This is just a basic outline of how it works but starting from this you should be able to schedule a notification.