返回
技术直击:iOS 15 动态语音播报解决方案,让推送更智慧
IOS
2023-12-01 20:50:19
前言
在当今这个快节奏的时代,智能手机已经成为人们不可或缺的工具,而推送通知更是成为我们获取信息的重要途径。但是,在iOS 15中,推送多条语音却会产生多条横幅,这对于动态金额语音来说,是非常不合适的。为了解决这个问题,我们需要对推送方案进行调整,在iOS 15以上的版本中,使用新的解决方案来实现,而在iOS 15以下的版本中,仍然沿用旧的推送方案。
新方案:N个通知合并
在新方案中,我们将采用N个通知合并的技术来解决这个问题。N个通知合并,是指将多条通知合并成一条横幅,从而避免了多条横幅的出现。这种技术在iOS 15中已经得到了支持,因此我们可以轻松地实现这一解决方案。
具体来说,我们可以通过以下步骤来实现N个通知合并:
- 在推送服务器端,对推送的内容进行处理,将多条语音合并成一条语音,并将其发送给客户端。
- 在客户端,收到推送通知后,对推送的内容进行解析,并将其显示在横幅中。
- 如果有多条推送通知同时到达,则将这些推送通知合并成一条横幅,并显示在屏幕上。
通过这种方式,我们可以轻松地实现N个通知合并,从而避免了多条横幅的出现。
旧方案:沿用旧的推送方案
对于iOS 15以下的版本,我们仍然沿用旧的推送方案。旧的推送方案是指,当收到推送通知时,会直接在屏幕上显示横幅。这种方案虽然简单,但对于动态金额语音来说,却非常不合适。因为多条横幅会让用户感到烦躁,甚至会错过重要的信息。
技术指南
1. iOS 15以上版本的解决方案
1.1 前提条件
- Xcode 13或更高版本
- iOS 15或更高版本
1.2 实现步骤
- 在Xcode中创建一个新的项目,并选择“App”模板。
- 在“Product Name”字段中,输入项目的名称。
- 在“Organization Name”字段中,输入您的组织名称。
- 在“Organization Identifier”字段中,输入您的组织标识符。
- 在“Bundle Identifier”字段中,输入项目的包标识符。
- 在“Device”字段中,选择要构建项目的设备。
- 在“Language”字段中,选择要使用的语言。
- 点击“Create”按钮创建项目。
- 在项目导航器中,选择“AppDelegate.swift”文件。
- 在“AppDelegate.swift”文件中,添加以下代码:
import UserNotifications
@UIApplicationDelegate
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 注册推送通知
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if granted {
// 注册推送通知成功
center.delegate = self
} else {
// 注册推送通知失败
print("注册推送通知失败")
}
}
return true
}
// 处理推送通知
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive notification: UNNotificationRequest, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// 合并推送通知
var notifications = [notification]
for pendingNotification in center.pendingNotificationRequests {
if pendingNotification.content.body == notification.content.body {
notifications.append(pendingNotification)
}
}
// 取消所有待处理的推送通知
center.removePendingNotificationRequests(withIdentifiers: notifications.map { $0.identifier })
// 发送合并后的推送通知
let content = UNMutableNotificationContent()
content.title = notification.content.title
content.body = notifications.map { $0.content.body }.joined(separator: "\n")
content.sound = notification.content.sound
content.badge = notification.content.badge
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request)
// 调用完成处理程序
completionHandler([.alert, .sound, .badge])
}
}
1.3 运行项目
- 在Xcode中,点击“运行”按钮运行项目。
- 在模拟器或真机上,收到推送通知后,您将看到一条合并后的横幅。
2. iOS 15以下版本的解决方案
2.1 前提条件
- Xcode 12或更低版本
- iOS 14或更低版本
2.2 实现步骤
- 在Xcode中创建一个新的项目,并选择“App”模板。
- 在“Product Name”字段中,输入项目的名称。
- 在“Organization Name”字段中,输入您的组织名称。
- 在“Organization Identifier”字段中,输入您的组织标识符。
- 在“Bundle Identifier”字段中,输入项目的包标识符。
- 在“Device”字段中,选择要构建项目的设备。
- 在“Language”字段中,选择要使用的语言。
- 点击“Create”按钮创建项目。
- 在项目导航器中,选择“AppDelegate.swift”文件。
- 在“AppDelegate.swift”文件中,添加以下代码:
import UserNotifications
@UIApplicationDelegate
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// 注册推送通知
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
if granted {
// 注册推送通知成功
center.delegate = self
} else {
// 注册推送通知失败
print("注册推送通知失败")
}
}
return true
}
// 处理推送通知
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
// 直接显示横幅
let content = UNMutableNotificationContent()
content.title = userInfo["aps"]["alert"]["title"] as? String
content.body = userInfo["aps"]["alert"]["body"] as? String
content.sound = UNNotificationSound.default
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)
}
}
2.3 运行项目
- 在Xcode中,点击“运行”按钮运行项目。
- 在模拟器或真机上,收到推送通知后,您将看到一条直接显示的横幅。
结语
通过本文,我们介绍了在iOS 15中推送动态语音播报解决方案。我们首先分析了问题所在,然后提出了两种解决方案:一种是针对iOS 15以上版本的解决方案,另一种是针对iOS 15以下版本的解决方案。最后,我们还提供了详细的技术指南,帮助您轻松实现这一解决方案。希望本文能够对您有所帮助。