How to On/Off UILocalNotification in iphone? - iphone

I have set UILocalNotification according UISWitch position by calling following method on button action set alarm.
- (void)scheduleNotificationWithInterval:(int)minutesBefore {
NSDateFormatter* theDateFormatter = [[NSDateFormatter alloc] init];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:#"EEEE"];
NSString *currentDay= [theDateFormatter stringFromDate:combo2.datePick.date];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [combo2.datePick.date dateByAddingTimeInterval:-30];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval=NSCalendarCalendarUnit;
localNotif.repeatInterval = NSDayCalendarUnit;
if (iBOfSwitchAlarm.on) {
if([combo1.selectedText isEqualToString:currentDay])
{
NSLog(#"In DayCheck");
localNotif.alertBody = #"Alarm Test";
localNotif.alertAction = #"Ok";
localNotif.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"THere Message for Alarm" forKey:#"Message"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
}else{
NSLog(#"SwOff");
}
}
I write following code in AppDelegate. Here also i check switch position.
ViewController *obj =[[ViewController alloc]init];
NSString *itemName = [notif.userInfo objectForKey:#"Message"];
if (obj.iBOfSwitchAlarm.on) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Alarm Title" message:itemName delegate:self cancelButtonTitle:#"Dismiss" otherButtonTitles:#"Continue", nil];
alertView.tag = 1111;
[alertView show];
app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber-1;
}else{
NSLog(#"SwitchOff");
}
Now my problem is when I set Notification according to time. It is getting alert on time. But in between when i make Switch OFF and again ON it is not getting alert. There is any way to make On and Off notification according to switch position. Please help me if any one knows. Thanks in advance.

to turn off you notification you can use [[UIApplication sharedApplication] cancelAllLocalNotifications]; or for specific [[UIApplication sharedApplication] cancelLocalNotification:<#(UILocalNotification *)#>];
and then you will have to reschedule your notification like this
[[MKLocalNotificationsScheduler sharedInstance] scheduleNotificationOn:[[NSDate Date] addTimeInterval:60*60*24*1]
text:#"Hi this is notification"
action:#"View"
sound:#"Raising_Sound.mp3"
launchImage:nil
andInfo:nil];
In my case i am rescheduling for one day later

If there can only be one UILocalNotification at a time, I would declare it as a variable so you can access it at will.
If you store your UILocalNotification you can use a combination of -cancelLocalNotification: to turn it OFF and -scheduleLocalNotification: to turn it back ON, while keeping the properties of it intact.

Related

Local Notification in background

Can anyone please tell me how to get a UILocalNotification while my app is in the background.
I am posting my code here. Thanks in advance.
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate =[startDate addTimeInterval:60];
NSLog(#"%#",localNotif.fireDate);
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
NSString *notifStr=[NSString stringWithFormat:#"Hey looks like you're meeting up with %#, why don't you let your other friends know what fun they're missing out on? Share a photo :)",[itemDict objectForKey:#"fname"]];
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:notifStr ,#"notifKey",nil];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
-(void)insert:(NSDate *)fire
{
[[UIApplication sharedApplication]cancelAllLocalNotifications];
self.localNotification = [[UILocalNotification alloc] init];
if (self.localNotification == nil)
{
return;
}
else
{
self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
self.localNotification.alertAction = nil;
self.localNotification.soundName = UILocalNotificationDefaultSoundName;
self.localNotification.alertBody = #"Hey looks like you're meeting up with %#, why don't you let your other friends know what fun they're missing out on? Share a photo :)";
self.localNotification.alertAction = NSLocalizedString(#"Read Msg", nil);
self.localNotification.applicationIconBadgeNumber=1;
self.localNotification.repeatInterval=0;
[[UIApplication sharedApplication] scheduleLocalNotification:self.localNotification];
}
}
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
[[UIApplication sharedApplication]cancelAllLocalNotifications];
app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber -1;
notif.soundName = UILocalNotificationDefaultSoundName;
[self _showAlert:[NSString stringWithFormat:#"%#",Your msg withTitle:#"Title"];
}
- (void) _showAlert:(NSString*)pushmessage withTitle:(NSString*)title
{
[self.alertView_local removeFromSuperview];
self.alertView_local = [[UIAlertView alloc] initWithTitle:title message:pushmessage delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[self.alertView_local show];
if (self.alertView_local)
{
}
}
Hope this will help you :)
addTimerInterval is depcrecated in iOS 4.0, make sure you deployment target. You can use.
- (id)dateByAddingTimeInterval:(NSTimeInterval)ti
Also make sure you set the correct value for fireDate
Please Use below code.
self.localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
self.localNotification.alertAction = nil;
self.localNotification.soundName = UILocalNotificationDefaultSoundName;
self.localNotification.alertBody = #"Hey looks like you're meeting up with %#, why don't you let your other friends know what fun they're missing out on? Share a photo :)";
self.localNotification.alertAction = NSLocalizedString(#"Read Msg", nil);
self.localNotification.applicationIconBadgeNumber=1;
self.localNotification.repeatInterval=0;

Appdelegate function 'didreceivelocalnotification' is called immediately after the notification is created

I'm using UILocalNotifications in iPhone to create an alarm. Alarm is scheduled when I click a UIButton. My issue is that – application:didReceiveLocalNotification: is called when I click this button, that is when I create a local notification. But , – application:didReceiveLocalNotification: should be called only when the notification time is reached. I checked it on both simulator and device and got the same results. Can anyone help me with this...thanks in advance.
-(void)createalarm:(NSString *)datend_time:(NSString *)message//creating alarm
{
NSLog(#"created!!!!!!!!!!!!!");
NSString *datestr = datend_time;
NSString *textstr = message;
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSDate *alerttime = [formatter dateFromString:datestr];
UIApplication * app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (notification)
{
notification.fireDate = alerttime;
//notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = 0;
notification.alertBody = textstr;
notification.soundName = UILocalNotificationDefaultSoundName;
[app scheduleLocalNotification:notification];
[app presentLocalNotificationNow:notification];
}
}
-(void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
NSLOG(#"delegate called")
}
It is because you call
[app presentLocalNotificationNow:notification];
So it shows the notification right after you created it. Remove this line of code.
[app scheduleLocalNotification:notification]; will work just fine and will shot the notification at fireDate.
Here is an example of NSLocalnotification:
-(void) scheduleNotificationForDate: (NSDate*)date {
/* Here we cancel all previously scheduled notifications */
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
NSLog(#"Notification will be shown on: %#",localNotification.fireDate);
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = [NSString stringWithFormat:
#"Your notification message"];
localNotification.alertAction = NSLocalizedString(#"View details", nil);
/* Here we set notification sound and badge on the app's icon "-1"
means that number indicator on the badge will be decreased by one
- so there will be no badge on the icon */
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = -1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Make sure that the date you are setting is not in the past!

how to set NSTimer for alarm application?

I am new in iPhone application development. Now i am developing alarm application for iPhone. In this application i am selected data from UIDataPicker. Then i applied to NSLocalNotification firedate with in alarm button action. It is working first time. Then second time agin click that button i again also working, but time also same. It is wrongly working.
Here i think i need to us NSTimer. I don't know how to use NSTimer, and also it is working background application also how to set this timer.
following developed code for alarm notification.
- (void) saveButtonAction:(id)sender {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil)
{
Resource *resourceLoader = [[Resource alloc] init];
NSDictionary *prefDic = [resourceLoader getPlistDataAsDictionary:#"preference"];
NSString *musicName;
NSDate *selectedDateTime = [[NSDate alloc]init];
if (prefDic && [prefDic objectForKey:#"alarmtime"]) {
//firstVal_textField.text = [prefDic objectForKey:#"value1"];
NSLog(#"saravanan %#",[prefDic objectForKey:#"alarmtime"]);
selectedDateTime = [prefDic objectForKey:#"alarmtime"];
NSLog(#"saravanan periyasamy %#", selectedDateTime);
musicName = [NSString stringWithFormat:#"%#%#",[prefDic objectForKey:#"alarmmusic"],#".wav" ];
}
UILocalNotification *notif = [[cls alloc] init];
//notif.fireDate = [datePicker date];
notif.fireDate = selectedDateTime;
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = #"Alarm";
notif.alertAction = #"Show me";
//notif.repeatInterval = 0;
//notif.soundName = UILocalNotificationDefaultSoundName;
notif.soundName = musicName;
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:#"saravanan"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
}
The set button is wired up to run a
method called
scheduleNotification in the
view controller which uses the
UILocalNotification class to
schedule a notification. The code
looks as follows:
(void )scheduleNotification
{
[reminderText resignFirstResponder];
[[ UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(# "UILocalNotification" );
if (cls != nil)
{
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [datePicker date];
notif.timeZone = [ NSTimeZone defaultTimeZone];
notif.alertBody = # "Did you forget something?" ;
notif.alertAction = # "Show me" ;
notif.soundName = UILocalNotificationDefaultSoundName ;
notif.applicationIconBadgeNumber = 1 ;
NSDictionary *userDict = [ NSDictionary dictionaryWithObject:reminderText.text
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[ UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}

Local Notification Issue?

I am new in iPhone application development. I am doing alarm application. In this application I am using local notification. I am calling notification method in done button action. Now I clicked done button means notification fired correctly. Then again clicked mean also fired, again and again press means working wrongly, but I am used notification fire date time same time. In this time completed means again user click done button means again triggered notification.
I want to fire that particular time. Can you please help me.
Here I am using this source code.
-(IBAction)doneButton
{
[self scheduledNotification];
}
-(void)scheduledNotification
{
Resource *resourceLoader = [[Resource alloc] init];
NSDictionary *prefDic = [resourceLoader getPlistDataAsDictionary:#"preference"];
if ([#"ON" isEqualToString:[prefDic objectForKey:#"alarm"]])
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil)
{
NSString *musicName;
//NSDate *selectedDateTime = [[NSDate alloc]init];
selectedDateTime = [prefDic objectForKey:#"alarmtime"];
NSLog(#"saravanan periyasamy %#", selectedDateTime);
musicName = [NSString stringWithFormat:#"%#%#",[prefDic objectForKey:#"alarmmusic"],#".wav" ];
NSLog(#"musicname %#", musicName);
NSLog(#"saravanan periyasamy123 %#", selectedDateTime);
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = selectedDateTime; /* time for fireDate*/
NSLog(#"selectedtime %#",selectedDateTime);
notif.timeZone = [NSTimeZone systemTimeZone];
notif.alertBody = #"Alarm";
notif.alertAction = #"Show me";
notif.repeatInterval = 0;
notif.soundName = musicName;
notif.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:#"saravanan"
forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
}
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Class cls = NSClassFromString(#"UILocalNotification");
if (cls) {
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
//[self.viewController showReminder:reminderText];
[self.settingViewController showReminder1:reminderText];
}
}
application.applicationIconBadgeNumber = 0;
return YES;
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
application.applicationIconBadgeNumber = 0;
}
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
application.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
//[self.viewController showReminder:reminderText];
[self.settingViewController showReminder1:reminderText];
}
hi i not understand what your question but here i post my one example code for LocalNotification just compare it or use this example....
here when user click done button then call "btnGo_Clicked" method
-(IBAction)btnGo_Clicked:(id)sender{
NSLog(#"\n ----------->>Go Clicked");
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
NSString *kRemindMeNotificationDataKey = #"kRemindMeNotificationDataKey";
NSDateComponents *dc = [[NSCalendar currentCalendar] components:NSDayCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSQuarterCalendarUnit fromDate:[exdatePicker date]];
[dc setDay:dc.day - 1];
NSDate *noticeDate = [[NSCalendar currentCalendar] dateFromComponents:dc];
NSDateFormatter* dateFormatterstring = [[NSDateFormatter alloc] init];
[dateFormatterstring setDateFormat:#"dd-MM-yyyy hh:mm:ss a"];
NSString *dateString = [dateFormatterstring stringFromDate:noticeDate];
txtExpirayDate.text = dateString;
UILocalNotification *notification = [[cls alloc] init];
notification.fireDate = noticeDate;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = [NSString stringWithFormat:#"%# your Licence Expiry Date is Tommorow",txtLicence.text];
notification.alertAction = #"Show me";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
NSDictionary *userDict = [NSDictionary dictionaryWithObject:txtLicence.text forKey:kRemindMeNotificationDataKey];
notification.userInfo = userDict;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
[notification release];
}
exdatePicker.hidden=YES;
}
do some changes and you got your output
i hope this answer help you...
:)
Remove this line if you want multiple press on button.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
because its removing your all previous notifications.

UILocalnotification not getting alert on set time?

I did this for getting alert on set time. But it's not alerting on time. I am setting time from datepicker. How I can solved this problem?
- (void)scheduleNotificationWithInterval:(int)minutesBefore {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
NSLog(#"Date %#",datePicker.date);
localNotif.fireDate = datePicker.date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
if ([freq isEqualToString:#"Daily"]) {
localNotif.repeatInterval = NSDayCalendarUnit;
}
localNotif.alertBody = #"Radio Asia Alarm";
localNotif.alertAction = #"Ok";
localNotif.soundName = UILocalNotificationDefaultSoundName;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"Radio Asia Alarm" forKey:#"Message"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
Also I call this method where I click button after set time and date
[self scheduleNotificationWithInterval:1];
I am not getting alert. What I can do?
Are you sure that you are setting the right timezone? Im sure the problem could be there
Have you tried without setting it?