ios 点击消息跳转实现
时间: 2024-05-10 19:19:00 浏览: 6
iOS 点击消息跳转实现需要以下几个步骤:
1. 在推送通知中添加自定义参数(如跳转链接、跳转类型等);
2. 在 App Delegate 中实现 UNUserNotificationCenterDelegate 协议的方法:
```swift
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if let userInfo = response.notification.request.content.userInfo as? [String: Any] {
if let link = userInfo["link"] as? String {
// 根据跳转类型进行相应的页面跳转
}
}
completionHandler()
}
```
3. 在 App 启动时注册通知:
```swift
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if granted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
```
其中,`self` 为当前类实例,需要遵守 `UNUserNotificationCenterDelegate` 协议。