跳到主要内容

推送通知优化

问题

推送到达率低、用户收不到通知,如何排查?

答案

排查流程

常见问题

问题原因解决
Token 无效沙盒/生产环境混用区分 APNs 环境
前台不展示未实现 willPresent 回调返回 .banner + .sound
通知分组混乱未配置 threadIdentifier设置合理的分组 ID
用户关闭了通知系统设置关闭In-App 引导开启

前台展示通知

class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// 前台也展示通知
completionHandler([.banner, .sound, .badge])
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
// 处理点击通知后的跳转
let userInfo = response.notification.request.content.userInfo
Router.shared.handle(userInfo)
completionHandler()
}
}

推送权限引导

func requestNotificationPermission() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
switch settings.authorizationStatus {
case .notDetermined:
// 首次请求,弹系统授权框
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { _, _ in }
case .denied:
// 已拒绝,引导去设置打开
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url)
}
default: break
}
}
}

常见面试问题

Q1: 静默推送和普通推送有什么区别?

答案:静默推送设置 content-available: 1,不展示任何 UI,直接唤醒 App 执行后台代码(如拉取新数据)。系统会限制频率,不保证每次都唤醒。普通推送展示横幅/声音/角标。

相关链接