Merge pull request #99 from RyanRory/pn-fix

Resubmit Device Token Periodically
pull/101/head
gmbnt 5 years ago committed by GitHub
commit e3f4bc4839
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,7 +13,9 @@ final class LokiPushNotificationManager : NSObject {
func register(with token: Data) {
let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined()
let oldToken = UserDefaults.standard.string(forKey: "deviceToken")
if (hexEncodedToken == oldToken) {
let lastUploadTime = UserDefaults.standard.integer(forKey: "lastUploadTime")
let now = Int(Date().timeIntervalSince1970)
if hexEncodedToken == oldToken && now - lastUploadTime < 48 * 60 * 60 {
Logger.info("Token is not changed, no need to upload")
return
}
@ -28,6 +30,7 @@ final class LokiPushNotificationManager : NSObject {
return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").")
}
UserDefaults.standard.set(hexEncodedToken, forKey: "deviceToken")
UserDefaults.standard.set(now, forKey: "lastUploadTime")
}, failure: { _, error in
print("[Loki] Couldn't register device token.")
})

Loading…
Cancel
Save