Past notification appearing when app closes - ios

I try to make an app that has to notify 4 deferent notification in a day for whole year this is my code when I run the code its work but the problem is if date was already passed for example 2014-03-12 01:12 the notification should not appear but it is appearing when the app close and I try with these as well [[UIApplication sharedApplication] cancelAllLocalNotifications]; or [[UIApplication sharedApplication] cancelLocalNotifications];.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *myDate = [df dateFromString:#"2014-03-10 01:04"];
NSDate *myDate1 = [df dateFromString:#"2014-03-10 01:21"];
NSDate *myDate2 = [df dateFromString:#"2014-03-10 01:22"];
UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.fireDate = myDate;
n1.alertBody = #"one";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
UILocalNotification* n2 = [[UILocalNotification alloc] init];
n2.fireDate = myDate1;
n2.alertBody = #"uuu";
[[UIApplication sharedApplication] scheduleLocalNotification: n2];
UILocalNotification* n3 = [[UILocalNotification alloc] init];
n3.fireDate = myDate2;
n3.alertBody = #"ontt";
[[UIApplication sharedApplication] scheduleLocalNotification: n3];

Related

UILocalNotification failed

The local notification never appeared. Can somebody tell me why?
- (IBAction)addReminder:(id)sender {
self.datePicker.timeZone = [NSTimeZone timeZoneWithName: #"Asia/Tokyo"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *formatedDate = [dateFormatter stringFromDate:self.datePicker.date];
NSLog(#"formatedDate>>>>>:%#", formatedDate);
NSDate *date = [dateFormatter dateFromString:formatedDate];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
NSLog(#"Setting a reminder for %#", localeDate);
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif. fireDate = localeDate;
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.alertBody = #"ZEIT!";
localNotif.alertAction = #"Show me the Timer!";
localNotif.timeZone = zone;
localNotif.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] +1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
Result:
formatedDate>>>>>:2015-08-09 23:32:00 Setting a reminder for 2015-08-09 23:32:00 +0000
Make sure you are requesting permission from the user to show notifications.
if ([UIApplication instancesRespondToSelector:#selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
To : kmcgrady , thanks for your answer, but i tried this and it worked
NSDate *now=[NSDate new];
localNotif.fireDate=[now dateByAddingTimeInterval:12];

Alarm Clock for iOS

I tried to re-write the following tutorial under Xcode 4.6.3 - http://vimeo.com/29824336
But I have the problem that at around 24:00 my code is not bringing up the alarm:
-(void) scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = fireDate;
notification.alertBody = #"Time to wake up!";
notification.soundName = #"SNSD-Oh.caf";
NSLog(#"Ring Ring Ring!");
[[UIApplication sharedApplication] scheduleLocalNotification: notification];
}
- (IBAction) alarmSetButtonTapped:(id)sender
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
NSLog(#"Alarm Set Button tapped: %#", dateTimeString);
[self scheduleLocalNotificationWithDate: dateTimePicker.date];
}
What did I do wrong?
Your app must not be running in foreground when the notification fires.
If you want to play sound in foreground,you can use combination of UIAlertView and 'AvAudioPlayer'.
You can setup this code in
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification delegate method of AppDelegate.m.
Feel free to ask further.

Set a sound as notification sound (no solution)?

Three sounds in my project :
}
- (IBAction)Sound1:(NSDate *) fireDate;
{
[[NSUserDefaults standardUserDefaults] setObject:#"Sound1.aiff" forKey:#"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Alarm went off!"];
[localNotification setAlertAction:#"View"];
[localNotification setHasAction:YES];
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:#"UserSoundChoice"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (IBAction)Sound2:(NSDate *) fireDate;
{
[[NSUserDefaults standardUserDefaults] setObject:#"Sound2.aiff" forKey:#"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Alarm went off!"];
[localNotification setAlertAction:#"View"];
[localNotification setHasAction:YES];
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:#"UserSoundChoice"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (IBAction)Sound3:(NSDate *) fireDate;
{
[[NSUserDefaults standardUserDefaults] setObject:#"Sound3.aiff" forKey:#"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Alarm went off!"];
[localNotification setAlertAction:#"View"];
[localNotification setHasAction:YES];
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:#"UserSoundChoice"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
- (IBAction)SetDatePicker
{
NSDateFormatter *dateFormatter =[ [NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
NSLog (#"Alarm saved: %#", dateTimeString);
[self Sound1:dateTimePicker.date];
[self Sound2:dateTimePicker.date];
[self Sound3:dateTimePicker.date];
}
-(void)scheduleLocalNotificationWithDate:(NSDate *)fireDate
{
UILocalNotification *notifiction =[[UILocalNotification alloc]init];
notifiction.FireDate = fireDate;
notifiction.AlertBody = #"Wake Up!!!";
notifiction.soundName =UILocalNotificationDefaultSoundName;
notifiction.repeatInterval= NSMinuteCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification: notifiction];
}
I want to let the users choose one of them to set it as a Notification sound
I have been searching a lot but i did not found any solution that helped me with
You can specify an audio file for local and push notifications. Allow the user to choose which file they want as the alert sound. Save that preference in NSUserDefaults and then Create a UILocalNotification with the sound.
Example:
You need to include your 3 sound files (Sound1.aiff, Sound2.aiff, and Sound3.aiff for example) in the Xcode project.
- (IBAction)Sound1
{
[[NSUserDefaults standardUserDefaults] setObject:#"Sound1.aiff" forKey:#"UserSoundChoice"];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:[NSDate date]];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
[localNotification setAlertBody:#"Alarm went off!"];
[localNotification setAlertAction:#"View"];
[localNotification setHasAction:YES];
localNotification.soundName= [[NSUserDefaults standardUserDefaults] objectForKey:#"UserSoundChoice"];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Sources:
Limits on iPhone push notification sounds?
UILocalNotifications playing Custom sound
https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/WhatAreRemoteNotif.html#//apple_ref/doc/uid/TP40008194-CH102-SW1

NSLocal Notification Repeatedly Loop the Alarm

I am working with Alarm App. I am create Alarm using NSLocal notification. The Alarm is working fine. My problem is I need to repeatedly loop the Alatm without interval.
My code:
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[localNotification setAlertAction:#"Launch"];
[localNotification setAlertBody:msg];
[localNotification setHasAction: YES];
localNotification.soundName = soundFile;
localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatCalendar = [NSCalendar currentCalendar];
localNotification.repeatInterval = kCFCalendarUnitSecond;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
Any one help me.
check the below answer. Its simple idea. You can add 1 minute and then send LocalNotification repeatly.
int myInt=60;
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
[localNotification setFireDate:date];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[localNotification setAlertAction:#"Launch"];
[localNotification setAlertBody:msg];
[localNotification setHasAction: YES];
localNotification.soundName = soundFile;
localNotification.applicationIconBadgeNumber = 1;
localNotification.repeatCalendar = [NSCalendar currentCalendar];
localNotification.repeatInterval = kCFCalendarUnitSecond;
NSDate *datePlusOneMinute = [date dateByAddingTimeInterval:myInt];
UILocalNotification *localNotification1 = [[UILocalNotification alloc] init];
[localNotification1 setFireDate:datePlusOneMinute];
localNotification1.timeZone = [NSTimeZone defaultTimeZone];
[localNotification1 setAlertAction:#"Launch"];
[localNotification1 setAlertBody:msg];
[localNotification1 setHasAction: YES];
localNotification1.soundName = soundFile;
localNotification1.applicationIconBadgeNumber = 1;
localNotification1.repeatCalendar = [NSCalendar currentCalendar];
localNotification1.repeatInterval = kCFCalendarUnitSecond;
NSDate *datePlusOneMinute1 = [datePlusOneMinute dateByAddingTimeInterval:myInt];
UILocalNotification *localNotification2 = [[UILocalNotification alloc] init];
[localNotification2 setFireDate:datePlusOneMinute1];
localNotification2.timeZone = [NSTimeZone defaultTimeZone];
[localNotification2 setAlertAction:#"Launch"];
[localNotification2 setAlertBody:msg];
[localNotification2 setHasAction: YES];
localNotification2.soundName = soundFile;
localNotification2.applicationIconBadgeNumber = 1;
localNotification2.repeatCalendar = [NSCalendar currentCalendar];
localNotification2.repeatInterval = kCFCalendarUnitSecond;
.....
....
...
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification1];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
.....
....
...
How many times you need. you can create repeatly.

UILocalNotification not being fired on right time

I am using UILocalNotification but it is not firing on time rather it notifies me 8-10 min after the specified time. Here is the code i am using
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormat stringFromDate:[NSDate date]];
NSDate *notificationDate = [dateFormat dateFromString:dateString];
localNotif.fireDate = notificationDate;
localNotif.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
// Notification details
localNotif.alertBody = #"Appear";
// Set the action button
localNotif.alertAction = #"Action";
localNotif.soundName = UILocalNotificationDefaultSoundName;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
I am using this in didEnterRegion delegate of CLLocationManager. What i am doing wrong?
This is Working Code:
UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.fireDate = [NSDate dateWithTimeIntervalSinceNow: 60];
n1.alertBody = #"one";
UILocalNotification* n2 = [[UILocalNotification alloc] init];
n2.fireDate = [NSDate dateWithTimeIntervalSinceNow: 90];
n2.alertBody = #"two";
[[UIApplication sharedApplication] scheduleLocalNotification: n1];
[[UIApplication sharedApplication] scheduleLocalNotification: n2];

Resources