Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Custom Local Notification in ios10 - swift 3

I'm trying to add a custom local notification, but I'm only getting the stock notification with my action:

enter image description here

My storyboard looks like this (standard template):

enter image description here

I have a extension which has UNNotificationExtensionCategory set to awesomeNotification (in Info.plist). Also the base of this extension is the Notification Content template from iOS - Application Extension.

In my app delegate I have this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    let center = UNUserNotificationCenter.current()

    let actions = [UNNotificationAction.init(identifier: "Hey", title: "Yo", options: UNNotificationActionOptions.foreground)]

    let category = UNNotificationCategory(identifier: "awesomeNotification", actions: actions, minimalActions: actions, intentIdentifiers: [], options: [])
    center.setNotificationCategories([category])

    center.requestAuthorization([.alert, .sound]) { (granted, error) in
    }

    return true
}

In my viewcontroller in the main app I have the following action to trigger it:

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

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

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

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

Edit

So it works on a iPhone 6s/6s+, very strange behaviour: enter image description here

like image 915
Bjarte Avatar asked Jun 15 '16 07:06

Bjarte


1 Answers

Update: As of iOS 10 beta 2, rich notifications are also available on pre-3D touch devices. Pull down on the regular notification to see it.

Make sure you are testing on a iPhone6s/iPhone6s plus simulator/device, it doesn't seem to work on pre-3D touch devices.

On a iPhone6 simulator, try to click and drag down on the stock notification you get and you should see your custom UI appear.

like image 121
Eirik Berntsen Avatar answered Sep 28 '22 01:09

Eirik Berntsen