How to call the snooze feature on local notification in iOS? - iphone

I am implementing an alarm application in iOS with snooze feature.
Local notification is set properly. When I select a particular time it fires at that particular time displaying 2 buttons close and view.
I have renamed the view button as snooze so that when the user clicks on snooze the notification will get repeated after 1 mins.
But I have a problem when the snooze button is clicked it shows properly in the console that it will repeat after 1 min from current time but the notification does not repeat in the console.
What may be the problem.
Please help me in solving this problem.
This is my code:-
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"Recieved local Notifications %#",notification.fireDate);
NSDate *date=[notification.fireDate dateByAddingTimeInterval:60];
NSLog(#"date After interval is %#",date);
UILocalNotification *notif=[[UILocalNotification alloc]init];
notif.fireDate=date;
NSLog(#"new notification:%#",notif.fireDate);
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
application.applicationIconBadgeNumber = 0;
}
In the didReceiveLocalNotification I have added because when the snooze button is clicked I want the 1 minute time interval notification to be set and called.

You're initializing a new UILocalNotification, but you not setting anything to be done when it actually fires - no body, or action.
From the Apple documentation,
alertBody - The default value is nil (no alert).
So, when the notification fires there will be no outward alert.
Are any of the application Local Notification callbacks being invoked? (which one, depends on the state of the App)

when ever your got notification application responds to below
method so.put one alertview you got alarm .did you want snooze .
then by that time remove all before notification and recreate notification.
with repeat interval.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
//write alert in alert delegate method create new notification object ,
}

Related

didReceiveLocalNotification: method works only if I press on notification banner

I have some UILocalNotifications in my app. When I receive one while I am in the app or if I press on the notification banner from the block screen or when it appears, the didReceiveLocalNotification works just fine. However, if I do not use the application and if I press on the icon of it (not on the banner) after notification banner appeared, the didReceiveLocalNotification method is not being called.
What should I do in this case, if I still want receive information about local notifications? Can I get it somehow in the applicationDidBecomeActive: method for example?
If you're app is completely shut down and you open it with a local notification, your local notification info will be passed into application:didFinishLaunchingWithOptions:.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification)
{
// Do whatever
}
// ...
}

application didReceiveLocalNotification not fired iOS7

The problem:
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
is not called sometimes with iOS7.
It doesn't mater how we schedule the notification:
alarm.fireDate = [[NSDate date] dateByAddingTimeInterval:0.1];
[app scheduleLocalNotification:alarm];
or:
[app presentLocalNotificationNow:alarm];
My thoughts:
This happens in the case when the user slides before the notification alert animation is finished.
And if he waits just a half second before he slides - the notification is fired and the App proceeds as expected.
The problem probably is that application enters foreground before the notification is received.
Did anyone meet this? Is it a bug? Any solution? Thank you!
Is your application in the background or foreground? If it's in the foreground, I'm pretty sure that method is called. If it isn't, maybe you aren't putting that method in your application delegate.
If it's on the background, here's a few possible scenarios:
Your app has been killed by the user or the OS. In this case when the user wake up your app by tapping on the notification on the notification centre (or swiping in lock screen), your application delegate will have the application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method called. You can get the notification from this method by:
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
Your app is in background, and the user tap on the notification in notification centre or lock screen. In this case, no delegate methods will be called. The documentation specifically said that didReceiveLocalNotification: is for when the app is in the foreground:
If the app is running in the foreground, there is no alert, badging,
or sound; instead, the application:didReceiveLocalNotification: method
is called if the delegate implements it.
So hopefully you can make an informed decision about what to do when you receive the notification. I personally find it a little bit weird that we don't get the notification object when the user launches the app by tapping the icon (not the notification). But I currently just write my logic around it.
Apple explicitly mentions in their documentation (Local and Remote Notification Programming Guide) that different methods get called depending on what state the app is in and what action the user takes (as Enrico mentioned).
Summary:
The user taps a custom action button in an iOS 8 notification.
In this case, iOS calls either application:handleActionWithIdentifier:forRemoteNotification:completionHandler: or application:handleActionWithIdentifier:forLocalNotification:completionHandler:. In both methods, you get the identifier of the action so that you can determine which button the user tapped. You also get either the remote or local notification object, so that you can retrieve any information you need to handle the action.
The user taps the default button in the alert or taps (or clicks) the app icon. ... the system launches the app and the app calls its delegate’s application:didFinishLaunchingWithOptions: method, passing in the notification payload (for remote notifications) or the local-notification object (for local notifications). ...
The notification is delivered when the app is running in the foreground. The app calls the UIApplicationDelegate method application:didReceiveLocalNotification: or application:didReceiveRemoteNotification:fetchCompletionHandler:.
So didReceiveLocalNotification is only fired when the app is already running and is in the foreground. You should also handle the second scenario where the isn't running, for which apple has the following code example:
Objective-C:
- (BOOL)application:(UIApplication *)app didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *itemName = [localNotif.userInfo objectForKey:ToDoItemKey];
[viewController displayItem:itemName]; // custom method
app.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber-1;
}
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
Swift (approximation provided by myself):
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let localNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification {
if let itemName = localNotification.userInfo?[ToDoItemKey] as? String {
handleNotification(localNotification)
application.applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber - 1
}
}
.
.
.
}

Push notifications ios

If my app is closed , and the iphone receives a push notification for that app, will it receive it and open the app?
Thanks
No if the message is received it will not open the app.
You app wil get launched if the user selects to view the notification.
Thus if the user does not react to the notifications your app will not be launched.
If your app is already running and in the foreground than the app delegate will receive the notification directly.
Yes it will launch if "View" button is clicked and application will call the delegate method.
If clicked on "close" button it will discard the notification.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
}
For more info http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 and http://mobiforge.com/developing/story/programming-apple-push-notification-services
YES. http://urbanairship.com/products/push-notifications/
when the notification aries that time you open the application that for you can use the following code.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UILocalNotification *localNotification = [[[UILocalNotification alloc] init] autorelease];
// Current date
NSDate *date = [NSDate date];
// Add one second to the current time
NSDate *dateToFire = [date dateByAddingTimeInterval:1];
// Set the fire date/time
[localNotification setFireDate:dateToFire];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
// Setup alert notification
[localNotification setAlertBody:#"Tap to return to TestApp" ];
[localNotification setAlertAction:#"Open TestApp"];
[localNotification setHasAction:YES];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
And more information regarding this thing you can use Refer the Reference.
May this code helping to you.
Yes, when you receive push - it appears as an alert on the screen on appears in the top (you can choose a way in Settings) as a message which will disappear after some seconds. If you click on push notification - it'll open App. But when you receive push, App will not be opened automatically with a receiving a push. Only after user clicks on push.

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.

Is it possible to stop the sound of a UILocalNotification from playing when the notification is delivered?

If a UILocalNotification fires with a sound set, and the user taps "Cancel" on the notification alert, the sound is stopped. But if the user taps "View", iOS then delivers the notification to the app and the sound keeps on playing. Is there any way to cancel this sound from the app? Canceling the notification in the app once the notification is delivered doesn't work (I didn't expect it to, the notification has already been delivered after all), and since I don't have the sound's system sound ID (and I didn't allocate it in the first place), I can't call AudioServicesDisposeSystemSoundID (can I?).
Is it possible to stop a UILocalNotification sound from playing if the user taps the Action button of the notification's alert?
It does not stop on the device too (5.1)
I have been trying to fix it but I can't figure it out.
I actually got it to stop on the simulator using this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
[[UIApplication sharedApplication] cancelLocalNotification:localNotif];
}
return YES;
}
but It still doesn't stop on the device
Apparently the problem only exists on the simulator (iOS 4.2 sdk), not on the actual device.
It can be handled in application delegate method as follows, if the user taps the Action button of the notification's alert, then the following method will be called, there we can cancel the notification
- (void)application:(UIApplication *)app
didReceiveLocalNotification:(UILocalNotification *)notif {
[[UIApplication sharedApplication]cancelLocalNotification:notif];
}
I had the same problem when initially testing with an iPhone 4.
The problem appears to have gone away as of iOS 8 and now Local Notification Sounds stop when canceled after transitioning to the App.
iPhone4 [ iOS - 7.1.2 ] - Local Notification sound keeps playing in App no matter what
iPhone6 [ iOS - 8.1.1 ] - Local Notification sound stop when canceled programmatically
From what I can infer, it appears a fix exists somewhere in iOS 8.x
(Alas I didn't manage to find any documentation or release notes on the matter.)
The Problem becomes a non-issue for apps focusing on iOS 8
(provided you cancel the Local Notification coming in)