7

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

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98
Bjarte
  • 2,167
  • 5
  • 27
  • 37

1 Answers1

5

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.

  • That is so strange... I was expecting it to work on iPhone SE or even other devices, seems very strange to limit it to those. I was also expecting my content view to take over the view, but it still shows the stock below... oh well! – Bjarte Jun 16 '16 at 05:32
  • I was expecting the same and banged my head against the wall with this exact problem. Looks like it is coming to other models later: http://www.macrumors.com/2016/06/14/rich-notifications-without-3d-touch/ – Eirik Berntsen Jun 16 '16 at 05:49