iOS 14 UNNotificationPresentationOptions.list、.banner 与警报

swiftIOS

1个回答

写回答

ss033

2025-07-07 09:40

+ 关注

IOS
IOS

IOS 14 引入了新的通知展示选项 UNNotificationPresentationOptions.list、.banner 和警报,这些选项可以让开发者更好地控制通知的展示方式。本文将介绍这些新选项,并提供案例代码进行演示。

UNNotificationPresentationOptions.list

UNNotificationPresentationOptions.list 是 IOS 14 中新增的一种通知展示选项。当我们将通知的展示选项设置为 .list 时,通知将以列表的形式显示在通知中心中。这种展示方式对于包含多个通知的应用来说非常有用,用户可以通过滑动来查看所有的通知,并选择感兴趣的通知进行查看。

下面是一个简单的示例代码,演示了如何将通知展示方式设置为 .list:

Swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// 设置通知展示选项为 .list

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

// 处理授权结果

}

return true

}

// 通知委托方法

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

// 设置通知展示选项为 .list

completionHandler(.list)

}

}

UNNotificationPresentationOptions.banner

UNNotificationPresentationOptions.banner 是另一种通知展示选项,它将通知以横幅的形式显示在屏幕上方,并在一段时间后自动消失。这种展示方式适用于需要及时提醒用户的通知,比如即时消息、新邮件等。

下面是一个简单的示例代码,演示了如何将通知展示方式设置为 .banner:

Swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// 设置通知展示选项为 .banner

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

// 处理授权结果

}

return true

}

// 通知委托方法

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

// 设置通知展示选项为 .banner

completionHandler(.banner)

}

}

警报

除了以上两种通知展示选项外,开发者还可以选择将通知以警报的形式展示给用户。警报具有最高的优先级,会立即弹出并打断用户的操作。这种展示方式适用于紧急情况下需要用户立即响应的通知。

下面是一个简单的示例代码,演示了如何将通知展示方式设置为警报:

Swift

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// 设置通知展示选项为警报

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

// 处理授权结果

}

return true

}

// 通知委托方法

extension AppDelegate: UNUserNotificationCenterDelegate {

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

// 设置通知展示选项为警报

completionHandler(.alert)

}

}

IOS 14 引入了新的通知展示选项,包括 UNNotificationPresentationOptions.list、.banner 和警报。开发者可以根据应用的需要来选择合适的展示方式,提供更好的用户体验。以上是关于这些新选项的简单介绍和示例代码,希望对大家有所帮助。

举报有用(4分享收藏

Copyright © 2025 IZhiDa.com All Rights Reserved.

知答 版权所有 粤ICP备2023042255号