Schedule a local notification on firing a local notification - iphone

I want to schedule a local notification when my previous local notification gets fired.
It should get scheduled disregarding the user taps 'View' or 'Cancel'.I am not getting proper place(delegate method)to schedule a new notification.According to Apple docs,application:(UIApplication *)application didFinishLaunchingWithOptions: can be used but it doesn't seems to be get called when application comes to foreground from background and application:(UIApplication *)application didReceiveLocalNotification: gets called only on click of 'View' and not on close.How should I do this?Any help is highly appreciated.

Yes. You are correct about the local notifications. You should click "view" to get the didReceiveLocalNotification: triggered. If you click "Cancel", you are not caring about the notificaiton. If you don't care, why should the iOS care? :-)
You are scheduling the notification. So, you know when it will be fired. Don't you? Then why wait for the first notification to be fired? Just schedule the second notification along with the first notification.
A workaround:
Local and Push Notification Programming Guide
says that only 64 local notifications are allowed per app. So, schedule the first 64 notifications initially. And when the app opens the next time, check [UIApplication sharedApplication] scheduledLocalNotifications], and schedule the next (64 - scheduledLocalNotifications) notifications.
int scheduledNotifications = [UIApplication sharedApplication] scheduledLocalNotifications];
int n = 64 - scheduledNotifications;
[self Schedule-next-n-notifications];
Note: We can't guarantee that this will work perfectly. In case, if the app opens after very long gap, for example after 1 or 2 months, some notifications would have not been scheduled at the proper time.

Related

How to cancel all local notifications when app is deleted from background not from device

I want to cancel all local notifications when app deleted from background, not from Device.
I know this is
[[UIApplication sharedApplication] cancelAllLocalNotifications];
but my Question is where should i pass it to fire event when application terminates from background.
I called inside applicationWillTerminateFromBackground but it is not working.
You could simply check to see if the app is in the background, and if it is, not fire the notifications.

How can I temporarily disable all local notification scheduled by my iPhone app?

Is there any method to temporarily disable all local notifications created by an iPhone app? I need to re enable them all with respect to a condition. my requirement is if the user turned off the notification button on my app , then no more notifications is shown.If he turned it on then all notifications should be shown. Any idea?
You can get all scheduled notifications and save them in shared preferences for example.
[[UIApplication sharedApplication] scheduledLocalNotifications];
Then cancel all notifications:
- (void)cancelLocalNotification:(UILocalNotification *)notification
If user activate notifications again, you can reschedule them again.
You can use "cancelAllLocalNotifications" method and When User turn it On you can set again All notification for it.

UILocalNotification fires after reinstalling the app

My app has an alarm function using UILocalNotification, and it works great. However, if the user uninstalls the app, then later REINSTALLS it, he would receive all the "in between" notifications at once.
I have tried to call:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if it's the first time the app is launched, but it doesn't help, because the notification is received even before application:didFinishLaunchingWithOptions: is called.
This was worse in 4.0 when the alarm was repeated even if the user has deleted the app, but at least that bug was fixed by Apple in later release. However now I'm stuck with this. Anyone has an idea?
According to Apple, this is not a bug (I filed a bug report). The system retains the UILocalNotifications for uninstalled apps for 24 hours just in case the user deleted the app by accident, and restores the said UILocalNotifications if the app is re-installed within that time frame.
The solution would be to remove all UILocalNotifications on first startup, like so:
- (BOOL) application: (UIApplication*) application
didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
if (self.isFirstRun)
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
self.firstRun = NO;
}
/* Other code here */
...
}
of course, implement your own firstRun setter and getter to fetch/save into persistent storage, like NSUserDefaults.
This is actually a bug in iPhone. If you removed the application and install it later also, it will have same app id, so when the application is reinstalled all the past local notifications were fired even if you didn't open the app.

Piling up UILocalNotifications

I'm developping an app that sends tips to users every five minutes through UILocalNotifications. The problem is that if you don't use your phone for a while, the notifications pile up and when you unlock the phone, you have to dismiss them one after the other, which can become quite annoying if you haven't used the phone for quite a while. Clicking the action button on the Alert sends you back to the application but even then, you still have to go through all the notifications.
Is there any means of dismissing all the notifications that have already been fired with a single click ?
Thanks for your help.
Miky Mike
Well, actually, I realize I made a mistake.
In order to cancel all the UIlocalNotifications at one, I just have to create this simple method : one line of code is enough :
- (void)application:(UIApplication *)app didReceiveLocalNotification :(UILocalNotification *)notification {
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
and there you are. Thanks anyway.
Before scheduling the next alarm you better to cancel all the previous notifications and then set the new one,
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if ([oldNotifications count] > 0)
[app cancelAllLocalNotifications];

Why am I receiving two alerts for the scheduled local notification?

I'm receiving two alerts for the notification below, and I cannot for the life of me figure out why. The code only appears to be called once, but App: didReceiveLocalNotification is being called twice. And I can't figure out why!
NSLog(#"Configuring notifications time is up.");
noTimeLeft=[[UILocalNotification alloc]init];
noTimeLeft.fireDate=meterEndTime;
noTimeLeft.alertBody=#"Your parking meter has expired!";
noTimeLeft.alertAction=#"Go to meter";
noTimeLeft.userInfo=[NSDictionary dictionaryWithObject:self.meterEndTime forKey:#"NSDate"];
[[UIApplication sharedApplication] scheduleLocalNotification:noTimeLeft];
NSLog(#"Configuring notifications finished time is up.");
local notification "didReceiveLocalNotification" calls twice
Turns out it's a bug in the simulator.
How did I MISS that question when I was searching though, lol.