Bjarte Bjarte - 4 months ago 463x
Swift Question

Add Local Notification in ios10 - swift 3

So putting the app in the background did the trick.


So I been trying to add a notification to the new UNUserNotificationCenter, but I don't seem to get it.

My view controller has an action:

@IBAction func sendPressed(_ sender: AnyObject) {
let content = UNMutableNotificationContent()

content.title = "Hello"
content.body = "What up?"
content.sound = UNNotificationSound.default()

// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
let request = UNNotificationRequest.init(identifier: "FiveSecond", content: content, trigger: trigger)

// Schedule the notification.
let center = UNUserNotificationCenter.current()
center.add(request) { (error) in
print("should have been added")

override func viewDidAppear(_ animated: Bool) {

let center = UNUserNotificationCenter.current()
center.requestAuthorization([.alert, .sound]) { (granted, error) in

And I have a
Notification Content Extension
in the project as well, but it does not seem to be triggered at all, any ideas what I'm missing? I'm trying the example from the user documentation, but it's not telling me much more or I have missed it.




You need to register for Notification...I tried and this works.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization([.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        return true

Here is a sample project on how to setup Notifications in iOS 10