I want to be able to hit a button that says "1 minute from now" or "1 hour from now" and have a notification go off (push / banner). How do I go about doing this?
Asked
Active
Viewed 134 times
0
-
post the notification using performSelector after delay! – Teja Nandamuri Apr 20 '16 at 17:49
-
1@Teja Nandamuri - but what if the app is not in the foreground in a hour from now? Then the performSelector would not execute. noobprogrammer, I'm sure you can easily find the documentation for how to create a notification or a past question on it. It is possible to set the time that the notification fires. – Gruntcakes Apr 20 '16 at 17:52
-
1Possible duplicate of [Schedule a local notification for a specific time in Swift 2](http://stackoverflow.com/questions/31043458/schedule-a-local-notification-for-a-specific-time-in-swift-2) – ZGski Apr 20 '16 at 17:52
2 Answers
1
set your timer according to button click if 1 min or 1 hr using the following command
NSTimer.scheduledTimerWithTimeInterval(time, target: self, selector: #selector(functionName), userInfo: nil, repeats: true)
then broadcast the notification in that function like shown below
NSNotificationCenter.defaultCenter().postNotificationName("youFunctionName", object: nil)
addobserver in viewcontroller in which you want to receive notification
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(youFunctionName), name: "youFunctionName", object: nil)

fahad khan
- 89
- 1
- 6
0
Here you go, have written code to schedule local notification :
//First register for User Settings if iOS8 or above
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
if #available(iOS 8, *) {
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [.Sound, .Alert, .Badge], categories: nil))
}
...
return true
}
//Function to schedule the local notification
func scheduleNotificationWithInterval(secondsAfter:Double, notificationTitle title:String, notificationBody body:String) {
let fireDate = NSDate()
let localNotif = UILocalNotification()
localNotif.fireDate = fireDate.dateByAddingTimeInterval(secondsAfter)
localNotif.alertBody = body
if #available(iOS 8.2, *) {
localNotif.alertTitle = title
}
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = 1;
UIApplication.sharedApplication().scheduleLocalNotification(localNotif)
}
Hope it helps :]

Chengappa C D
- 1,841
- 1
- 12
- 19