In my app, at some point I need to display some notification to the user, in case user didn't respond to them, I need to remove that notification. Now, if I call cancelAllLocalNotifications function it does remove notification from notification center (also removes the alert from Home-screen, when the device is unlocked).
But when device is locked and notification is displayed on the lock-screen. After sometime, my app calls cancelAllLocalNotifications function. Now, this doesn't remove Notification from Lock-screen, however there is no item in notification center.
Using cancelAllLocalNotifications won't remove the already showed ones. It cancels all notifications that are scheduled. I don't think there is a way to clear a notification from the lock screen.
I dont know if it was not possible that time, but now you can cancel notification from the lockscreen. If anybody needs this now.
If you send a notification you have to store it somewhere to be able to identify it when it should be canceled.
All you have to do is
UIApplication.sharedApplication().cancelLocalNotification(notification)
to identify the notification which should be canceled you can set information in the userinfo property
notification.userInfo = ["title": item.title, "UUID": item.UUID]
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.
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 perform an action(save Unique id coming with notification, in a text file) if notification comes on Notification tray but not viewed?
Technically application delegates for notification trigger when you tap on notification in tray(open or view). you can not do anything on notification receive until any user-action on it. But when the app is in foreground applicationDidReceiveRemoteNotification automatically triggered by ios on notification receive, at that point you can do what you want. But not when your app is in background or not running.
In case the user is busy and didn't check his notifications in the notification centre. I don't want my notifications to get stacked. that may lead to removing the app. by user.
Therefore, when a notification receive I want to clear all previous notifications my app. sent and keep only the new one.
Is that possible?
Note: I want to remove them before they got clicked. If one got
clicked I can remove them easily in didReceiveRemoteNotification.