UILocalNotification not doing anything
This may seem like a silly question, but this is my first time using UILocalNotification and I can’t get it to work for a quick test. It just doesn’t do anything.
1. I’ve created 2 variables in the AppDelegate
let today = NSDate() let notification: UILocalNotification = UILocalNotification()
2. Then in the applicationDidEnterBackground function, I have the following
notification.fireDate = today.dateByAddingTimeInterval(10) notification.alertTitle = "My App Test" notification.alertBody = "Testing Notification \n :)" UIApplication.sharedApplication().presentLocalNotificationNow(notification) UIApplication.sharedApplication().scheduleLocalNotification(notification)
3. Also added this to the applicationDidBecomeActive function
- IOS Cancelling Local Notifications
- UILocalNotification does not fire after 10 minutes in background
- UILocalNotification - repeatInterval reset (no repeat)
- Repeating UILocalNotifications with Gaps
- UIApplicationLaunchOptionsLocalNotificationKey always null - iOS
- Launch a local notification at a specific time in iOS
Solutions Collect From Internet About “UILocalNotification not doing anything”
After reading the documentation again, I realized I missed a crucial first step which is to register my App first for user notifications. The Apple doc was written in OBJ-C, but I was able to figure it out in order to convert it to swift. This is what I did:
1. I added this to my AppDelegate didFinishLaunchingWithOptions function and it now works
var types: UIUserNotificationType = UIUserNotificationType() types.insert(UIUserNotificationType.Alert) types.insert(UIUserNotificationType.Badge) let settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil) UIApplication.sharedApplication().registerUserNotificationSettings(settings)
- How can I do Key Value Observing and get a KVO callback on a UIView's frame?
- Best Practice using an Array of Strings with NSUserDefault
- Crash on Xcode 8.0 GM iPhone Simulator (iOS 10)
- search in grouped uitableview
- iOS: UIPageViewController – Use button to jump to next page
- How to Convert duration form youtube api in swift?
- Does objective c have a strip tags function?
- Adding “using namespace…” causes compilation error in xcode 4
- Set the background color of selected text in NSTextView?
- MVC, can model save/load its data?
- Make Swift Cocoa app launch on startup on OS X 10.11
- Binding an NSTreeController to an NSOutlineView in code
- should I commit .xcodeproj file in SVN?
- Static Table Cells in NIB File
- Transient sectionNameKeyPath & NSSortDescriptor NSFetchedResultsController