我在应用程序中遇到了一个奇怪的问题。在新设备上安装应用程序时推送通知工作正常,但重新安装应用程序我没有收到任何通知。是来自 API 的问题,还是在终止应用程序时必须执行某些操作?
注册设备代码:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let deviceTokenString = deviceToken.map { String(format: "%02.2hhx", $0) }.joined()
SHARED_MGR.deviceToken = deviceTokenString
let str = UIDevice.current.identifierForVendor!.uuidString as String
let characterSetForDeviceID: CharacterSet = CharacterSet( charactersIn: "-" )
deviceID = ( str as NSString )
.trimmingCharacters( in: characterSetForDeviceID )
.replacingOccurrences( of: " ", with: "" ) as String
SHARED_MGR.DeviceIDStr = deviceID
createDevice()
}
请求授权代码:
if( UIDevice.current.systemVersion >= "10.0"){
if #available(iOS 10.0, *) {
let center = UNUserNotificationCenter.current()
center.delegate = self
center.requestAuthorization(options: [.sound, .alert, .badge], completionHandler: { (granted, error) in
if granted{
UIApplication.shared.registerForRemoteNotifications()
} else {
PHPhotoLibrary.requestAuthorization { (status) in
print(status)
}
}
})
} else {
// Fallback on earlier versions
}
}
else {
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.alert, UIUserNotificationType.badge, UIUserNotificationType.sound]
let pushNotificationSettings = UIUserNotificationSettings(types: notificationTypes, categories: nil)
}
请检查您的设备令牌是否在每次新启动到服务器时更新。
如果使用静态设备令牌推送通知,这可能是问题所在。
您没有收到有关新启动的推送通知。
因为设备令牌在新安装时将始终更改。