iOS应用重装后推送通知失效问题咨询
Hey there! Let's dig into why your push notifications stop working after reinstalling the iOS app, even though they work fine on first install. This issue is rarely caused by the API itself—more likely it's related to how your app handles device tokens, permission states, or how your server associates tokens with devices.
先看客户端代码里的潜在问题
1. iOS 8-9的推送注册逻辑不完整
Looking at your permission request code, the iOS 8-9 fallback section creates pushNotificationSettings but doesn't actually register the settings or request remote notifications. That means users on older iOS versions won't get push notifications at all after reinstall (or even first install!). You need to add these two lines:
else { let notificationTypes: UIUserNotificationType = [.alert, .badge, .sound] let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil) // Add these two lines UIApplication.shared.registerUserNotificationSettings(pushNotificationSettings) UIApplication.shared.registerForRemoteNotifications() }
2. 重装后Token的上报逻辑可能有缺陷
When you reinstall an app, Apple generates a new device token even if the device ID (identifierForVendor) stays the same. Your current code calls createDevice() when it gets a new token—but if this API is only creating a new device record (not updating an existing one linked to the device ID), your server might still be sending notifications to the old, invalid token.
You need to modify your server integration: instead of creating a new device entry every time, use the device ID to update the existing record's token. This ensures your server always has the latest valid token for the device.
3. 主动触发推送注册
On iOS 13+, the system retains notification permission choices across app reinstalls. That means if a user previously granted permission, your requestAuthorization completion handler might not fire immediately when the app launches again. To make sure you register for remote notifications every time, add a call to UIApplication.shared.registerForRemoteNotifications() directly in applicationDidFinishLaunchingWithOptions (after checking that permissions are granted, or even alongside the permission request).
服务器端需要检查的点
- Confirm that your server is updating the device token instead of creating duplicate entries when it receives the
createDevicerequest. If duplicates exist, your server might be targeting the old token. - Verify that when sending push notifications, your server uses the most recently registered token for the device ID, not the first one stored.
总结
This issue is almost certainly not an API-level problem. Focus on:
- Fixing the incomplete iOS 8-9 push registration logic
- Ensuring your app reports the new token to the server on reinstall, and the server updates the existing device record
- Adding explicit calls to register for remote notifications on app launch to avoid relying solely on the permission request completion handler
内容的提问来源于stack exchange,提问作者Abhishek Master

