Display custom UI with UNNotificationContentExtension for local notifications
Asked Answered
M

4

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.

Mahayana answered 24/6, 2016 at 8:52 Comment(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.Rickey
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?Boll
L
-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.

Leafy answered 28/6, 2016 at 2:42 Comment(3)
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: #38954996Hillock
@Async-, you do not need custom UI or 3D touch for notification actions. This all may be done with UserNotifications framework.Aludel
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.Cellar
K
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.)

Kaminski answered 7/10, 2016 at 20:22 Comment(0)
C
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

Chufa answered 9/9, 2016 at 6:41 Comment(3)
Can you please confirm UNNotificationContentExtension is working even without 3DTouch. On my side it did not workThirion
It is working in my case for non 3D Touch phone, specifically iPhone 6Canaan
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?Boll
L
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.

Luggage answered 4/5, 2017 at 14:50 Comment(0)
L
-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.

Leafy answered 28/6, 2016 at 2:42 Comment(3)
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: #38954996Hillock
@Async-, you do not need custom UI or 3D touch for notification actions. This all may be done with UserNotifications framework.Aludel
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.Cellar

© 2022 - 2024 — McMap. All rights reserved.