Multiple reminders in single iPhone app - iphone

how can we set more than one reminder's in our iPhone app, i have to set individual reminder's for every image projects in my app so that a user can take image (daily, weekly or monthly) from the camera for a specific project, i am able to set single reminder but when i tried to set more than one reminder for another project in my app it overwrites all the previous reminders of all projects. please give me any idea.

try this.
//KeyValue = used for identifying reminder
//RepeatType = NSWeekCalendarUnit or NSMonthCalendarUnit
//AlertBody = display text
-(void)setReminder:(NSDate*)date KeyValue:(NSString*)keyValue RepeatType:(NSInteger)repeatType AlertBody:(NSString*)alertBody
{
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = alertBody;
// Set the action button
localNotif.alertAction = NSLocalizedString(#"View",nil);
localNotif.soundName =UILocalNotificationDefaultSoundName;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:#"%#",keyValue] forKey:#"ReminderID"];
localNotif.userInfo = infoDict;
localNotif.repeatInterval = repeatType;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
}
//call this method for setting single notification.
//you can set as many as you want

Related

How to deliver five notification for five different times everyday?

I am working on an islamic PrayerTimes app that gives five prayertimes a day.So my goal for now is to deliver five notifications to the user everyday at specific prayerTimes. then i created five notifications for each prayers , assigned five firedate for each notification, and i can get the notification delivered .
However , my problem is every time i run the app or restart the app i am having a notification on notification center, at the console area i can see all the older notifications are delivered as well (I overrode application:didReceiveLocalNotification: method).
Frankly I am not a experienced developer , i really didn't get this , and I thought my code is very long.
So can somebody help me and tell how can i manage to do this ? :) (my english is not that good please be tolerant). if i missed some other sing to inform ,please tell.
Here is my code ;
Edit: I call this mehod from viewDidLoad.
I got five times in an array blow:
NSSArray *timeArray = #[time0,time1,time2,time3,time4];
I sceduled five UILocalNotification like this:
if (localNotification0 != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification0];
}
NSDate *date0 = [_timeArray objectAtIndex:0];
NSLog(#"date %#",date0);
localNotification0 = [[UILocalNotification alloc] init];
localNotification0.fireDate = date0;
localNotification0.timeZone =[NSTimeZone defaultTimeZone];
NSLog(#"firedate %#",localNotification0.timeZone);
localNotification0.alertBody = #"Se";
localNotification0.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification0];
if (localNotification1 != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification1];
}
NSDate *date1 = [_timeArray objectAtIndex:1];
localNotification1 = [[UILocalNotification alloc] init];
localNotification1.fireDate = date1;
NSLog(#"firedate %#",localNotification1.fireDate);
localNotification1.timeZone =[NSTimeZone defaultTimeZone];
localNotification1.alertBody = #"Se";
localNotification1.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification1];
[localNotification1 release];
if (localNotification2 != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification2];
}
NSDate *date2 = [_timeArray objectAtIndex:2];
localNotification2 = [[UILocalNotification alloc] init];
localNotification2.fireDate = date2;
localNotification2.timeZone =[NSTimeZone defaultTimeZone];
NSLog(#"firedate %#",localNotification2.fireDate);
localNotification2.alertBody = #"Se";
localNotification2.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification2];
[localNotification2 release];
if (localNotification3 != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification3];
}
NSDate *date3 = [_timeArray objectAtIndex:3];
localNotification3 = [[UILocalNotification alloc] init];
localNotification3.fireDate = date3;
localNotification3.timeZone =[NSTimeZone defaultTimeZone];
NSLog(#"firedate %#",localNotification3.fireDate);
localNotification3.alertBody = #"Se";
localNotification3.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification3];
[localNotification3 release];
if (localNotification4 != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification4];
}
NSDate *date4 = [_timeArray objectAtIndex:4];
localNotification4 = [[UILocalNotification alloc] init];
localNotification4.fireDate = date4;
localNotification4.timeZone =[NSTimeZone defaultTimeZone];
NSLog(#"firedate %#",localNotification4.fireDate);
localNotification4.alertBody = #"Se";
localNotification4.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification4];
[localNotification4 release];
Is there other more easy way to do this ? pleas help me out!
For starters, it looks like you can shorten your code by using a loop, something like this:
for (NSDate *time in times) {
if (localNotification3 != nil) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotification3];
}
UILocalNotification *note = [[UILocalNotification alloc] init];
note.fireDate = time;
note.timeZone =[NSTimeZone defaultTimeZone];
note.alertBody = #"Se";
note.soundName = _adhanName;
[[UIApplication sharedApplication] scheduleLocalNotification:note];
[note release]; // no need to release note if you use ARC
}
That loop will run once for each entry in your times array, reducing the amount of code you need by a factor of 5, in this case, or [times count] in general.
every time i run the app or restart the app i am having a notification on notification center
I'm not sure I fully understand what's wrong here, but if any/all the notifications are being presented too soon, it sounds like you should check the times for which they're scheduled. If the problem is that your app keeps scheduling new notifications every time it starts, resulting in too many notifications, then you have at least two options:
use -cancelAllLocalNotifications before you schedule any new notifications
record the times for which notifications have already been scheduled (NSUserDefaults is good for that kind of thing) and avoid scheduling new notifications for those times

Invoking Alarm for certain time in ios

I am working on App which will set an alarm on ios for a time depending on user input.
Meaning: if a user selects row 1 of table then it will look into dictionary (which may say 20 minutes),,, then it should set an alarm in ios for (currrent time+ 20 minutes).
Can someone please tell me the best way to approach this.
You can use UILocalNotification:
UILocalNotification *local = [[UILocalNotification alloc] init];
// create date/time information
local.fireDate = [NSDate dateWithTimeIntervalSinceNow:20*60]; //time in seconds
local.timeZone = [NSTimeZone defaultTimeZone];
// set notification details
local.alertBody = #"Alarm!";
local.alertAction = #"Okay!";
local.soundName = [NSString stringWithFormat:#"Default.caf"];
// Gather any custom data you need to save with the notification
NSDictionary *customInfo =
[NSDictionary dictionaryWithObject:#"ABCD1234" forKey:#"yourKey"];
local.userInfo = customInfo;
// Schedule it!
[[UIApplication sharedApplication] scheduleLocalNotification:local];
[local release];

How to initialize local notification?

I want to implement local notification in my clock app.Basically i want that a music file should be played after every half an hour like in ship's clock in which chimes are played after every 30 minutes.
Can anyone give rough idea as how i can implement this functionality even when the app enters in background?
I recently used the Local notification stuff and used the following functions
//Setting up the Local Notifications
for (int i= 1 ; i<=10; i++) { //We here set 10 Notification after every 30 minutes from now you can modify it accordingly
NSDate *scheduled = [[NSDate date] dateByAddingTimeInterval:60*30*i]; //These are seconds
NSDictionary* dataDict = [NSDictionary dictionaryWithObjectsAndKeys:scheduled,FIRE_TIME_KEY,#"Background Notification received",NOTIFICATION_MESSAGE_KEY,nil];
[self scheduleNotificationWithItem:dataDict];
}
Where scheduleNotificationWithItem is defined as
- (void)scheduleNotificationWithItem:(NSDictionary*)item {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
if (localNotification == nil) return;
localNotification.fireDate = [item valueForKey:FIRE_TIME_KEY];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = [NSString stringWithFormat:NSLocalizedString(#"%#", nil), [item valueForKey:NOTIFICATION_MESSAGE_KEY]];
localNotification.alertAction = NSLocalizedString(#"View Details", nil);
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.userInfo = item;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
}
Finally you can handle these notifications as
You can handle these notifications as follows
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
// Do the required work you can obtain additional Info via notification.userInfo which happens to be a dictionary
}
reading the developer documentation will help you more to understand the stuff.Hope it helps
You can use UILocalNotifications and set their 'firedate', according to your requirement and then schedule the notification. These notifications doesn't bother whether your app is running or is in background they will always show up like an alertview.

how to set uilocalnotification firedate for a fixed date?

i want to set localnotification say for a program which starts at 6:00 pm.For that i have taken the time in a date variable and i am comparing it with current date from system.
Say setDate is for fixed date i.e 6.00 pm so i have to set firedate such that it shows the notification before 30 mintes the program starts. The examples i have seen in that the firedate is set according to currentdate.
Can someone tell me how can i set firedate according to my fixed date??
You fire the local notification this way
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [NSDate date];// Now here you can manage the fire time.
localNotif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif.alertBody = #"BusBuddy";
// Set the action button
localNotif.alertAction = #"View";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:#"You are near to reach the Bus Stop" forKey:#"someKey"];
localNotif.userInfo = infoDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
[localNotif release];
This line of code will work for you. you just need to provide the date time for this
localNotif.fireDate = [NSDate date];
And now for formatting your date time you can refer to these links
iphonedevelopertips.com
developer.apple.com, CFDatesAndTimes
developer.apple.com, DataFormatting
Well then you can handle your local notification in application delegate file when ever you get the notification.
e.g. here is the delegate method which is fired everytime when you get the local notification.
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
//Handle the notificaton when the app is running
NSLog(#"Recieved Notification %#",notif);
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Hey Neha" message:#"Sanjay wants to be your friend " delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
SystemSoundID bell;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"WhoopFlp" ofType:#"wav"]], &bell);
AudioServicesPlaySystemSound (bell);
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
i=0;
}
}
So what I am doing here is simply showing the alert and playing a system sound when my local notification occurs.
So now what you want is to navigate to the page in the program where you were when you get the local notification.So simply in this delegate method you need to allocate your view controller and need to push the view controller to that view where you want to be.
That would solve your problem.
Well i had a similar kind of problem what i wanted is to show the notification in the background and in the front end as well, so writing the 2 different methods in my app was not worthful.so i handled it this way in the delegate method which will show the notification in the front end as well.
Good luck to you.
localnotification =[[UILocalNotification alloc]init];
[localnotification setFireDate:[NSDate dateWithTimeIntervalSinceNow:[lodatepicker countDownDuration]]];
[localnotification setAlertAction:#"Launch"];
[localnotification setHasAction: YES];
[localnotification setAlertBody:[lotextview text]];
// [localnotification setSoundName:musicString];
localnotification.timeZone = [NSTimeZone defaultTimeZone];
[localnotification setApplicationIconBadgeNumber:[[UIApplication sharedApplication] applicationIconBadgeNumber]+1];
[[UIApplication sharedApplication] scheduleLocalNotification:localnotification];

iPhone sdk: how to increase the local notification count

Hi I am working on Google calendar, I have to remain user events with local notifications, when event starts.
for that I have to show notifications, if user have two events at a time then local notification count has to increase on app icon.(If I have two events at a time also count is showing only one local notification count on the app icon).
please suggest me how to increase the local notification count on the app icon.
please check my code.
//Local notifications delegates and methods.
Class cls = NSClassFromString(#"UILocalNotification");
if (cls != nil) {
UILocalNotification *notif = [[cls alloc] init];
notif.fireDate = [[when startTime] date];
notif.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
notif.alertBody = titles;// here title is the key word for the event
// Set the action button
notif.alertAction = nil;
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber = 1;
// Specify custom data for the notification
NSDictionary *userDict = [NSDictionary dictionaryWithObject:titles forKey:kRemindMeNotificationDataKey];
notif.userInfo = userDict;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
[notif release];
}
Thank you
Assuming that the variable count contains the correct number to shown on the icon's badge, you simply do the following:
[UIApplication sharedApplication].applicationIconBadgeNumber = count;
my answer will need a database:
create an array (database) sorted with date and count. When a notification fires up, call it's count and display it using
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: badgeSortCount]