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

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.

Related

Read push notification message without picking from the notification center

I'm trying to read push message when app is in background or killed and user tap on application icon(which has badge) instead from notification center.I have seen few questions in stackoverflow regarding this, but non of them solve this problem,
Here is the scenario,
I receive a push message.
And then i ignore the message.
Next i launch the application by tapping icon which has badge(not from the notification center)
It won't call didReceiveRemoteNotification: method.
As per iOS application life cycle, it'll run didFinishLaunchingWithOptions: when app launches, and next time it'll call applicationWillEnterForeground: and applicationDidBecomeActive: methods. my question is how can i read all the push messages from above life cycle methods which i received previously and ignored?
I have already tried below snippet in didFinishLaunchingWithOptions: (From this answer) but i always get null as payload.
NSDictionary *pushNotificationPayload = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if(pushNotificationPayload) {
NSLog(#"Payload is not null");
[self application:application didReceiveRemoteNotification:pushNotificationPayload];
}else{
NSLog(#"Payload is null");
}
Any valuable answer appreciated, Thanks.
BTW, i use iOS 6

local notification handling

I am trying to crack the way local notification work.
I wrote this line in order to present the notification I scheduled:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
The issue is that it runs endless number of times.
If I write something else it runs only one time, but i understood that this line should pop up the message of the notification.
Can anyone shed some light?
Thanks,
presentLocalNotificationNow is triggering didReceiveLocalNotification which in turn is calling presentLocalNotificationNow... so you end up with an endless loop.
Found it,
I didn't realize that by reaching the didReceiveLocation method it automatically sends a notification message if the iPhone is not activated.
No need to write anything...

UILocalNotification - The right way to finish/wrap up a local notification after notification is received

In the application, I have received the local notification and taken the appropriate actions - so far so good. But after that, what's the appropriate actions to take to "clean up" the local notification object?
I haven't found any direct instruction on this in my search of Apple/StackOverflow, so I assume it's just discarded by iOS and as long as I release any related the object/properties then I should be good to go.
But am I missing anything? Do i have to cancel it from UIApplication? I wouldn't want for these just to be accumulating in the scheduledLocalNotifications array, for example.
Thanks.
Once the user actually receives the notification thats it, its gone from the schedule and you dont have to do anything to clean up the notification, if you have scheduled local notications (that have not fired) and want to clean those up you can do something like
UIApplication* app = [UIApplication sharedApplication];
NSArray* oldNotifications = [app scheduledLocalNotifications];
// Clear out the old notification before scheduling a new one.
if ([oldNotifications count] > 0)
[app cancelAllLocalNotifications];
Hope it helps

Schedule a local notification on firing a local notification

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.

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];