iOS如何通过代码设置持久化推送通知?交互推送常驻方案咨询
Hey there! Let's break down your two iOS push notification questions clearly, since both are about making notifications stick around for your users.
1. 如何通过代码设置持久化推送通知?
首先明确:「持久化推送通知」指的是通知会留在通知中心,直到用户手动清除,而非自动消失。实现方式分本地通知和远程推送两种场景:
针对本地通知(使用UNUserNotificationCenter)
本地通知默认就是持久化的(会留在通知中心,除非用户手动删除)。要确保功能正常,只需按以下步骤实现:
- 先请求必要的通知权限(弹窗、角标、声音)
- 创建
UNMutableNotificationContent填充内容,搭配触发条件(即时、定时、位置触发等)
Swift代码示例:
import UserNotifications // 步骤1:请求通知权限 UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in guard granted, error == nil else { print("权限申请失败或出错:\(error?.localizedDescription ?? "未知错误")") return } // 步骤2:创建持久化本地通知 let content = UNMutableNotificationContent() content.title = "持久化通知" content.body = "这条通知会一直留在通知中心,直到你手动清除它" content.sound = UNNotificationSound.default // 设置即时触发(也可使用时间间隔、日历等其他触发条件) let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false) let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) // 步骤3:添加通知请求 UNUserNotificationCenter.current().add(request) { error in if let error = error { print("添加通知失败:\(error.localizedDescription)") } } }
针对远程推送(APNs)
远程推送的持久化由APNs payload控制,只需注意两点:
- payload中必须包含
alert对象(不要只发送带content-available: 1的静默推送) - 若使用
mutable-content: 1通过通知服务扩展修改内容,确保不删除alert核心内容
示例APNs payload:
{ "aps": { "alert": { "title": "持久化远程通知", "body": "这条远程通知会留在通知中心,直到用户清除" }, "badge": 1, "sound": "default" } }
2. 交互式推送通知自动消失,如何通过代码设置为持久化横幅?
横幅的显示行为(临时/持久)默认是系统级设置,由用户控制,但不同iOS版本有不同的应对方式:
iOS 15+ 使用persistentBanner展示选项
苹果在iOS 15中新增了UNNotificationPresentationOption.persistentBanner,可以让横幅一直停留在屏幕上,直到用户交互(点击、滑动清除等)。只需在通知代理的willPresent方法中配置:
import UserNotifications class AppNotificationDelegate: NSObject, UNUserNotificationCenterDelegate { // 处理前台通知展示逻辑 func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { // iOS 15及以上,使用persistentBanner让横幅持久显示 if #available(iOS 15.0, *) { completionHandler([.persistentBanner, .sound, .badge]) } else { // 旧版本iOS fallback到标准弹窗 completionHandler([.alert, .sound, .badge]) } } // 处理交互式通知的用户操作 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { // 根据交互ID处理用户操作(比如按钮点击) switch response.actionIdentifier { case "ACCEPT_ACTION": print("用户点击了接受按钮") case "DECLINE_ACTION": print("用户点击了拒绝按钮") default: print("用户点击了通知本身") } completionHandler() } } // 在AppDelegate/SceneDelegate中设置代理 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { UNUserNotificationCenter.current().delegate = AppNotificationDelegate() return true }
iOS 14及更早版本:引导用户修改系统设置
iOS 14及以下,应用无法直接控制横幅持久化,只能引导用户手动修改系统设置:
func showBannerStyleGuideAlert() { let alert = UIAlertController( title: "优化通知显示", message: "若想让通知横幅持续显示直到你操作,请前往设置 > [你的应用名] > 通知,将横幅样式改为「持久」", preferredStyle: .alert ) alert.addAction(UIAlertAction(title: "去设置", style: .default) { _ in guard let settingsURL = URL(string: UIApplication.openSettingsURLString) else { return } UIApplication.shared.open(settingsURL) }) alert.addAction(UIAlertAction(title: "取消", style: .cancel)) // 从当前顶层ViewController弹出提示 if let topVC = UIApplication.shared.windows.first?.rootViewController { topVC.present(alert, animated: true) } }
内容的提问来源于stack exchange,提问作者HariNarasimhaRao.G
相关产品推荐
相关产品推荐

