5

I'm trying to use the new UNNotificationContentExtension to display a custom user interface for local notifications but only the default notification alert is shown.

I created the extension with the Xcode template and specified the UNNotificationExtensionCategory in the Info.plist. enter image description here

After this I'm registering for notifications and setting up the UNNotificationCategory in application(_:didFinishLaunchingWithOptions:)

    let center = UNUserNotificationCenter.current()
    center.requestAuthorization([.alert, .sound]) { (granted, error) in
        // Enable or disable features based on authorization.
    }

    let action = UNNotificationAction(identifier: "Delete", title: "Delete", options: [])

    let category = UNNotificationCategory(identifier: "notify-test", actions: [action], minimalActions: [], intentIdentifiers: [], options: [])
    UNUserNotificationCenter.current().setNotificationCategories([category])

I'm scheduling the notification to trigger five seconds after the App did enter the background with this code:

    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey: "Hello!", arguments: nil)
    content.body = NSString.localizedUserNotificationString(forKey: "Hello_message_body", arguments: nil)
    content.sound = UNNotificationSound.default()
    content.categoryIdentifier = "notify-test"

    let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: 5, repeats: false)
    let request = UNNotificationRequest.init(identifier: "notify-test", content: content, trigger: trigger)

    let center = UNUserNotificationCenter.current()
    center.add(request)

The notification fires as expected but on the iOS default style is shown, none of the custom UI defined in the default Storyboard file is shown. Maybe someone ran into the same problem before and can help me here.

mhaddl
  • 885
  • 1
  • 9
  • 18
  • 2
    Custom notification content is only available in conjunction with 3D Touch (I believe). Meaning your notification code will only get called if the user performs a 3D Touch on the notification alert. – pds Jun 24 '16 at 20:22
  • Will I be able to update the content of this kind of notification? or cancelling the previous one and showing a new one with updated content is the only option? – nr5 Jul 02 '18 at 14:54

4 Answers4

6

Rich notification content is available whether or not the device has 3D touch.

For devices without 3D touch it is possible to pull down a notification to get the rich interface. (Or as mentioned above swipe left in notification center.)

blackp
  • 1,752
  • 3
  • 13
  • 14
2

as I know, UNNotificationContentExtension also supports the devices which don't have 3DTouch, for those devices the system adds the swipe to left behavior for the notification on the lock screen, after swipping you can see the button named view and then you can see the content extension if you tap it

Henry
  • 35
  • 4
  • Can you please confirm UNNotificationContentExtension is working even without 3DTouch. On my side it did not work – fvisticot Oct 02 '16 at 20:52
  • It is working in my case for non 3D Touch phone, specifically iPhone 6 – mickeymoon Oct 13 '16 at 12:14
  • Will I be able to update the content of this kind of notification? or cancelling the previous one and showing a new one with updated content is the only option? – nr5 Jul 02 '18 at 14:54
1

Just experienced my UNNotificationContentExtension not displaying on an iPhone 7 running iOS 10.2.1.

Curiously the content extension would happily display on both the iOS simulator and an iPhone 6S running iOS 10.3.1.

Fix: Updating the iPhone 7 to the latest version of iOS (10.3.1) fixed the problem and everything displays as it should now. Looks like must have been a bug in iOS itself.

Simo
  • 2,172
  • 3
  • 19
  • 23
-1

There is no problem of your code.

As far as I know,UNNotificationContentExtension is only support device which support 3DTouch(Test environment iOS10 beta1). The same as UNTextInputNotificationAction.

Then Press or drop-down can show custom UIViewcontroller.

maquannene
  • 2,257
  • 1
  • 12
  • 10
  • Can you please explain more, and maybe give link? I need two custom actions in the notifications, does this this case fall under this 3D touch need? can you please help me with this: http://stackoverflow.com/questions/38954496/notification-actions-are-not-shown-unusernotificationcenter-ios10-swift-2-3 – Async- Aug 16 '16 at 10:34
  • @Async-, you do not need custom UI or 3D touch for notification actions. This all may be done with [UserNotifications framework.](https://developer.apple.com/documentation/usernotifications) – kelin Jul 12 '17 at 20:01
  • That’s not accurate. You can access the notification content extension when pulling down on a notification while your iPhone is unlocked, for example. 3D Touch is just one of the ways to access it. – junjie Oct 08 '17 at 07:25