Local notification while app not running
You can easily schedule local notifications, and they will be presented at the scheduled date and time regardless of the app's state.
First you need to get permission from the user to present notifications, like this:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound|UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))
return true
}
Then you create the notification like this:
var localNotification:UILocalNotification = UILocalNotification()
localNotification.alertAction = "This is"
localNotification.alertBody = "A notification"
localNotification.fireDate = NSDate(timeIntervalSinceNow: 15)
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
Have a look at the Local and Remote Notification Programming Guide.
Handle tap on local notification when app is terminated - iOS - Swift
When you tap on Push Notification on Notification Center.
OS launches your app (terminated before) then STILL delivers the action to this (Function 1)
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
)
You can get the notification
object from response like what you did.
But you may need to save temporary this data from the selected push notification, then handle it later when app is completely active with user.
Remember to call completionHandler()
when you finish your logic. And, you must set UNUserNotificationCenter.current().delegate = self
, before you return true
in
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool`.
self
here is your AppDelegate
, which means you set UserNotification's delegate to a live object or initialized object.
It will be able to handle the above delegate function call (Function 1) to get your selected push notification's data.
Local Notifications in iOS without running the app
There is no direct way For doing this.
If you want to do it anyhow(not proper solution, just a patch), then just go via following way.
- wake up the app in background - which can be done by using starting location manager, which will wake up your app in background when location get updated, at that time you can do whatever you like with local notification or any other things.
Before applying this method - make sure that - this is too much battery consuming way + not proper way. Your app might get rejected from apple if it is using too much battery.
Read following details(copied from other question from stackoverflow):
An app can be woken by a significant location change, if the app has indicated that it wants to monitor such events.
See: [CLLocationManager Docs][1]
Look for a method called startMonitoringSignificantLocationChanges
. If a significant location change occurs while your app is not in the foreground or isn't running at all, your application will be launched in the background, allowing the app to perform background-only operations (e.g. no view code will run).
iOS local notification and calendar event does not appear while app is not running
I would like to share with someone who's stuck this like me,
here's Apple's troubleshooting, and it's really helpful:
https://developer.apple.com/library/archive/technotes/tn2265/_index.html
my finally solution is the trigger time beyond 60s.
Scheduling a local notification when the app is terminated
You are pretty close to the strategy I would use. Rather than schedule the notification when you enter the background, clear and schedule every time the app becomes active.
func applicationDidBecomeActive(_ application: UIApplication) {
UNUserNotificationCenter.current().removePendingNotificationRequests( withIdentifiers: ["notification"]
NotificationManager.shared.schedule(notification)
}
Every time you launch, you should setup a notification for 2 weeks in the future. If they launch tomorrow, delete the old notification, and setup a new one for two week after that.
Related Topics
Core Data: Could Not Cast Value of Type 'Mytype_Mytype_2' to Mytype
How to Make a Https Request to a Server in Swift
Evaluate Bool Property of Optional Object in If Statement
Spritekit - Create at Random Position Without Overlapping
Swift: Loop Over Array Elements and Access Previous and Next Elements
My Data Changes in Uitableviewcell When I Scroll Down and Get Back
"Cannot Inherit from Non-Open Class" Swift
Why 'There Cannot Be More Than One Conformance, Even with Different Conditional Bounds'
Using Swift Library in Xamarin
Which Swift Character Count Should I Use When Interacting with Nsstring APIs
Use of Undeclared Type 'Viewcontroller' When Unit Testing My Own Viewcontroller in Swift
Convert Timestamp String with Epochal Time and Timezone into Nsdate
How to Get Date and Time to Show a Clock in Uilabel
Spawning a Spritekit Node at a Random Time
Masking Uiview/Uiimageview to Cutout Transparent Text
How to List Swift Types That Conform to Protocol Using Reflection