Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display custom UI with UNNotificationContentExtension for local notifications

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.

like image 293
mhaddl Avatar asked Jun 24 '16 08:06

mhaddl


People also ask

How do I customize notifications in Swift?

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.

Can you customize iPhone notifications?

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.

What is local push notification?

A local notification is sent locally on the device, so it doesn't need an internet connection.


1 Answers

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.)

like image 131
blackp Avatar answered Oct 31 '22 09:10

blackp