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
Related
Is there a way to prevent a remote notification in the Notification Center from being removed by the OS after it is tapped by the user? The tap invokes the app and didReceiveNotificationResponse() is called. I am not using badges in the notifications.
Apple is not allowing to persist the notification once after you open it.
What’s next
You can create a local notification from data you get from push notification when user taps on it. I have not tried the implementation but it should work in your usecase.
User will not be knowing whether it is local or push notification.
Is there any way to set time duration for a local notification to be displayed in notification panel, so that all notifications will be eventually cleared from notification panel after certain time.
You can not clear the local notifications until your app is not launched. When launched you can clear existing notifications using
UIApplication.sharedApplication().cancelAllLocalNotifications()
UIApplication.sharedApplication().cancelLocalNotification(notification)
Where notification is the notification which you want to cancel. You can persist time when local notification was persisted. On next launch based on that time you can decide whether to cancel notification or not.
Suppose App is in foreground and user does not interact with app for 5 min, App should give alert.
Suppose App is in background and it remain in background for more than 5 min App should give alert as soon as app comes in foreground.
Is there any standard way to do that?
For the background part, basically you want to have a Counter. This Counter should do
Observe UIApplicationWillResignActiveNotification notification. And record the time when the selector is called. Let's say it's lastActiveTime.
Observe UIApplicationDidBecomeActiveNotification notification. And inside the selector, compare the current time with lastActiveTime. If it's more than 5 minutes, you'll pop up the alert.
For foreground, you could use some assumptions such as if the top most view controller is the same, assume the user hasn't interacted with the app. You can have a timer that keep checking the top most view controller.
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.
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.