Local Notifications - Repeat Interval / custom time doesn't work - cocoa-touch

Can't seem to get some basic notifications to work.
I'm using local notifications to bring alarms sounds to the user.
I can get a notification hourly to work.
I can't seem to get one to work every half hour (every 30 minutes, or at 30 minutes or 1 hour) or every 15 minutes.
How do I put an offset to the hourly repeat interval? The notifications are not firing.
If this is not possible, how would I manually write a loop that did it 'till the limit of 64 queued notifications?
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:[NSDate date]];
NSInteger hour = [components hour];
NSDate *h = [calendar dateBySettingHour:hour+1 minute:0 second:0 ofDate:[NSDate date] options:0];
NSDate *f = [calendar dateBySettingHour:hour minute:15 second:0 ofDate:[NSDate date] options:0];
NSDate *ha = [calendar dateBySettingHour:hour minute:30 second:0 ofDate:[NSDate date] options:0];
NSDate *q = [calendar dateBySettingHour:hour minute:45 second:0 ofDate:[NSDate date] options:0];
if (selected == 0)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.repeatInterval = NSCalendarUnitHour;
notification.alertBody = nil;
notification.alertAction = nil;
notification.soundName = #"4.mp3";
notification.fireDate = h;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
else if (selected == 1)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.fireDate = ha;
notification.repeatInterval = NSCalendarUnitHour;
notification.alertBody = nil;
notification.alertAction = nil;
notification.soundName = #"2.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = #"4.mp3";
notification.fireDate = h;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
else
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.fireDate = f;
notification.repeatInterval = NSCalendarUnitHour;
notification.alertBody = nil;
notification.alertAction = nil;
notification.soundName = #"1.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = #"2.mp3";
notification.fireDate = ha;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = #"3.mp3";
notification.fireDate = q;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
notification.soundName = #"4.mp3";
notification.fireDate = h;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}

Well, I tried it again, but it was actually working, but the asset was not imported properly. I just deleted the sound file in finder and reimported them and the app was firing the notifs right!

Related

How to get local notification at two different timings daily?

I want my app to send Local Notification at 7AM and 7PM daily. How to do that? please help
I need to send two local notification daily one at 7AM and another at 7PM.
{
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"HasLaunchedOnce"])
{
static dispatch_once_t once;
dispatch_once(&once, ^ {
NSLog(#"Do it once");
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDate *now = [NSDate date];
NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:now];
[components setHour:13];
[components setMinute:1];
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = [calendar dateFromComponents:components];
notification.repeatInterval = kCFCalendarUnitDay;
[notification setAlertBody:#"AM U got notification!!!"];
// notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[components setHour:17];
[components setMinute:28];
notification.fireDate = [calendar dateFromComponents:components];
notification.repeatInterval = NSCalendarUnitDay;
[notification setAlertBody:#"PM U got notification!!!"];
// notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
});
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"HasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
I have written the if condition to run the code only once after installing the app or else every time I open the app local notification is getting set. Now for the same day it is working fine but for the next day it is not getting repeated. please help. or is there any other way to get daily local notification at 7AM and 7PM?
There is no repeat on local notifications. You would have to schedule multiple notifications for however many days you want them to be scheduled for.

notification ever day xcode

I want to create an alert on every day at the same time.
Sample code is below :
- (IBAction)start:(id)sender {
[defaults setBool:YES forKey:#"notificationIsActive"];
[defaults synchronize];
self.message.text=#"Напоминание включено";
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
components.calendar = calendar;
components.hour = 7;
components.minute = 37;
date = components.date;
if([date compare:[NSDate date]] < 0) {
date = [date dateByAddingTimeInterval:60*60*24];
}
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody = #"ТЕКСТ НАПОМИНАНИЯ";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSCalendarUnitDay;//NSCalendarUnitMinute; //Repeating instructions here.
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Thanks in advance.
Try this
NSDate *date = [NSDate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval=NSCalendarUnitDay;
localNotif.alertBody = #"Daily Notification";
localNotif.userInfo=#{
};
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];

Repeat alarm on weekly basis

I am implementing the alarm application. I am stuck with the logic. How to repeat the alarm on weekly basic, mean if user selected the sunday than alarm will be rang on every sunday...
Here is my code.
-(void) repeataftersevendays: (NSString *) day
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
[components setTimeZone:[NSTimeZone localTimeZone]];
[calendar setTimeZone: [NSTimeZone localTimeZone]];
sunday = [calendar dateByAddingUnit:NSCalendarUnitDay
value:4
toDate:[NSDate date]
options:0];;
NSLog(#"value is %#",sunday);
}
and here is my notification fire code.
-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = fireDate;
localNotif.timeZone = [NSTimeZone localTimeZone];
localNotif.alertBody = #"Time to wake Up";
localNotif.alertAction = #"Show me";
localNotif.soundName = #"Tick-tock-sound.mp3";
localNotif.applicationIconBadgeNumber = 1;
localNotif.repeatInterval = kCFCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
you can use repeat interval parameter of NSLocalNotification. set it to NSWeekCalendarUnit
localNotification.repeatInterval = NSWeekCalendarUnit;

UILocalNotification every Saturday at 12:00 am

I try to repeat UILocalNotification every Saturday but I didn't succeed.
I try the following:
// Create notification for every saturday at 12:00 am
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSUIntegerMax fromDate:[NSDate date]];
[components setWeekday:7]; // Saturday
[components setHour:12]; // 12 am
[components setMinute:0];
[components setSecond:0];
NSDate *fireDate = [gregorian dateFromComponents:components];
NSLog(#"%#", fireDate);
[[NotificationsManager sharedManager] configureLocalAlertMessage:message
fireDate:fireDate
repeatIntervalUnit:NSWeekCalendarUnit
type:kNotificationTypeMoneyBox];
But "fireDate" returns: "2013-03-12 11:00:00 +0000" ie today.
Any help?
Thanks!
EDIT:
Code to configure notification
- (void)configureLocalAlertMessage:(NSString *)message fireDate:(NSDate *)date repeatIntervalUnit:(NSCalendarUnit)repeat type:(NotificationType)type {
// Local notification
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.timeZone = [NSTimeZone systemTimeZone];
notification.fireDate = date;
notification.repeatInterval = repeat;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.hasAction = YES;
notification.alertBody = message;
notification.userInfo = #{kNotificationTypeKey : #(type)};
notification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
// Schedule
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSWeekCalendarUnit | NSYearCalendarUnit) fromDate:idate];
int weekday = [weekdayComponents weekday];
NSDate *nextdate ;
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
if(weekday <= 5)
{
/* it is my case to check the weekday, if it is less or greater than 5*/
}
else
{
[offsetComponents setDay:7];
[offsetComponents setHour:12];
[offsetComponents setMinute:0];
[offsetComponents setSecond:0];
nextdate = [gregorian dateByAddingComponents:offsetComponents toDate:idate options:0];
}
[offsetComponents release];
[gregorian release];
you can use this next date as a fire date for your local notification.
Here idate is NSDate *idate = [NSDate date];

UILocalNotification fires immediately instead of at scheduled time

I have an object that stores times in an NSArray as a NSString. I chose NSString since that's what I end up displaying a lot instead of the NSDate. I'm trying to setup a UILocalNotification for it. What I've done is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone systemTimeZone];
dateFormatter.dateFormat = #"h:mm a";
for (NSString *s in _times) {
NSDate *date = [dateFormatter dateFromString:s];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)
fromDate:date];
NSDateComponents *timeComponents = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:date];
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
NSLog(#"%#", [dateFormatter stringFromDate:itemDate]);
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertAction = thePill.name;
note.alertBody = thePill.note;
note.fireDate = itemDate;
note.timeZone = [[NSCalendar currentCalendar] timeZone];
if (note.fireDate == nil) {
NSLog(#"nil firedate");
}
// note.repeatInterval = NSDayCalendarUnit;
NSData *data = [self archivePill:thePill];
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:data forKey:PILL_NOTIFICATION];
note.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:note];
}
When I NSLog the itemDate time, it is what I want. At the end of the method, the local notification fires immediately instead at that time. I'm not sure why since my date is the right time, and I thought I just set the fireDate to that date? Thanks.
// Fire notification immediately after 1 sec
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
To fire a notification immediately you should use
presentLocalNotificationNow
Instead of
scheduleLocalNotification