2

I ran my app on my iphone. (Simply connected my phone and chose my iPhone as the target.) I built local notifications in my app which are scheduled and appear perfectly on time on the iPhone. However, they don't appear on the locked screen, even though under Settings -> Notifications -> [my app] -> Show on Lock Screen is turned on.

What is strange is that they even appear in the Notification Centre.

This is similar to UILocalNotification not showing in the lock screen however I found no answer there either.

Has anybody encountered this? I'm thinking it may be an iOS bug. I'm using the latest iOS, Xcode, OS X to date.

Community
  • 1
  • 1
Daniel
  • 3,758
  • 3
  • 22
  • 43

1 Answers1

3

You have to take permission for show the notification on lock screen ! once look at the code in Appdelegate.swift

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    let notificationCategory = UIMutableUserNotificationCategory()
    let categories = Set<UIUserNotificationCategory>(arrayLiteral: notificationCategory)
    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: categories)
    application.registerUserNotificationSettings(settings)
    return true
}

Swift 3

First add import UserNotifications then add a code in didFinishLaunchingWithOptions

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound , .badge]) {(accepted, error) in
            if !accepted {
                print("Notification access denied.")
            }
        }
Pushp
  • 1,064
  • 10
  • 18
  • The `Set(arrayLiteral: )` initializer is not intended to be used directly, it's intended to be used with an array literal. You can just write `[notificationCategory]`. Anyway it's mostly irrelevant now because of Swift 3 :) – jtbandes Jan 03 '17 at 05:16