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.
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.
It's the same process as setting up for Notification Service Extension. Go to xcode select File > New > Target > Notification Content Extension > Next > Product Name( name it as CustomUI) > Finish > Active scheme popUp(if comes then select cancel). Every Custom UI must have its unique category Identifier.
Most notification settings can be customized for each app. You can turn app notifications on or off, have notifications play a sound, choose how and where you want app notifications to appear when your device is unlocked, and more. Go to Settings > Notifications.
A local notification is sent locally on the device, so it doesn't need an internet connection.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With