How to make UILocalNotification which doesn't launch my app - ios

I am using UILocalNotification to let a user know something. However, it's for a note for him and it doesn't require any actions in my application.
I set:
localNotification setAlertAction:nil];
[localNotification setHasAction: NO];
So, it shows only in the top of Home screen. However, if the user clicks on it, my app got launched.
Is there a way to create a local notification, which won't launch my app, when it's tapped?

If the user clicks on a local notification alert, iOS will launch the associated app. This is the expected behaviour of handling local notification. I don't believe you can change it.

Related

How to know if a local notification was fired while the app is in background?

I've enabled the background mode for location updates. I create an scheduled local notification, and I'd like to be able to stop the location services when it is fired and the app is running in background.
It seems that the didReceiveLocalNotification method is only called when the app is in foreground, or it is in background and the user taps it, is there any way to notice that it is fired while the app is in background, but the user doesn't tap it?
I hope this might help
When the system delivers a local notification, several things can happen, depending on the app state and the type of notification. If the app is not frontmost and visible, the system displays the alert message, badges the app, and plays a sound—whatever is specified in the notification. If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the app is woken up or launched. (If the user taps one of the custom actions you specify using the additionalActions property, the app is woken up or launched into the background.) In its application:didFinishLaunchingWithOptions: method, the app delegate can obtain the UILocalNotification object from the launch options dictionary using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the app icon, and the user in response launches the app, the application:didFinishLaunchingWithOptions: method is called, but no UILocalNotification object is included in the options dictionary. When the user selects a custom action, the app delegate’s application:handleActionWithIdentifier:forLocalNotification:completionHandler: method is called to handle the action.
This from Apple documentation.

How to perform a segue when a UILocalNotification is fired?

I am making an app with multiple schedule alarms in the future. The design is that a Local Notification will fire, then when the user opens the app any time after this notification (either through Notification Action or just by tapping the icon on the home screen), the app will automatically show an Alarm view controller, which the user interacts with.
I know I can just call on a function with performSegueWithIdentifier with the Notification Action feature, but I would really like a comprehensive method that covers the case when the user ignores the notification, then launches the app with other methods.
Thank you for your help!

Swift UILocalNotification: Is it possible to fire an event when Notification is displayed?

So I know the didReceiveLocalNotification event is fired when a user selects an action on a Local Notification.
But, is an event fired prior to that when the notification is displayed as a banner or alert when the app is not active? And can that be accessed to do some background code?
I am looking for an answer in Swift if you can help.
No, local notifications do not and cannot awake the app when firing and the application is not active. The user must tap the notification, or perform a developer defined action on it before the app hears anything.
If you need your app to wake up, you need to use a remote notification with content available flag set.

How to take back local notification after being displayed?

Set the scene: Its 7:55 AM. Sets local notification for 8:01 AM. Pops up on lock screen at 8:01 AM. User ignores it.
Would it be possible to take back that notification at lets say 8:07?
In Knock, the app that lets you unlock your macbook with a nock does this. It shows “knock twice to unlock on the lock screen. When you knock, it disappears. How is this done?
Yes. You call -cancelLocationNotification on UIApplication passing to it the local notification instance you want to cancel.
Check the docs:
https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html
... and
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/cancelLocalNotification:
Also see:
Can I dismiss the local notification when next time the app launch?
Dismiss local notifications when application becomes active

Is it possible to change the display duration of a UILocalNotification?

I need to display a local notification and have it remain on screen longer than the default 4-5 seconds, preferably until the app itself removes it. I've seen other apps (e.g. Pandora) that manage to do this somehow (maybe a push notification?), but I can find no duration property on UILocalNotification or in UIApplication methods like presentLocalNotificationNow:, scheduleLocalNotification:, etc. Neither the documentation nor any of the tutorials I've found address the display time at all. Is this something that just can't be done with local notifications?
A couple of solutions here and I would not recommend either:
1- You can request from the user to go to settings > Notification Center > your app. And change the alert style from Banner (default) to Alerts. This will present the user an alert similar to the alert presented when the app is in the foreground. The user would have to dismiss the alert versus the banner style notification that just appears/disappears. Unless this is a corporate app and you have the users buy in, I would not go that route as this could annoy the user.
2- I tested the sound clip method and yes, if you present a notification with a clip < 30seconds; the notification will stay on the (top of the) screen until the sound clip is finished playing. Having said that, if the user taps any of the volume button (to reduce the sound for example), the notification is immediately dismissed even before its end! I think though that the purpose of the notification is a gentle reminder and, lasting more than the typical 4-5 seconds goes against the norm and, it might annoy the user (or the user might think something is stuck, phone froze, etc..). Here is the code anyway:
UILocalNotification *howLongCanANotificationLast = [[UILocalNotification alloc]init];
howLongCanANotificationLast.alertBody=#"I am a notification";
howLongCanANotificationLast.soundName=#"musicfilename.mp3";
[[UIApplication sharedApplication] presentLocalNotificationNow:howLongCanANotificationLast];
Hope this helps.

Resources