Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local notification while app not running

Is it possible to show some kind of a local notification from an app, even if the app isn't running(also not in background)?

For example a daily reminder or something like that. I know that it is possible with push-notifications, but it doesn't fit for my app.

like image 356
Christian Avatar asked Jan 01 '15 17:01

Christian


1 Answers

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.

like image 104
Emil Avatar answered Nov 11 '22 20:11

Emil