Merge branch 'PN-without-preview' of https://github.com/RyanRory/loki-messenger-ios into PN-with-preview

pull/149/head
ryanzhao 6 years ago
commit 48113969c1

@ -587,7 +587,9 @@ static BOOL isInternalTestVersion = NO;
} }
OWSLogInfo(@"Registered for push notifications with token: %@.", deviceToken); OWSLogInfo(@"Registered for push notifications with token: %@.", deviceToken);
[LKPushNotificationManager.shared registerWithToken:deviceToken]; //TODO: For normal push notification test
[LKPushNotificationManager.shared registerWithToken:deviceToken pubkey:self.tsAccountManager.localNumber];
// [LKPushNotificationManager.shared registerWithToken:deviceToken];
// [self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken]; // [self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken];
} }
@ -1552,6 +1554,10 @@ static BOOL isInternalTestVersion = NO;
__IOS_AVAILABLE(10.0)__TVOS_AVAILABLE(10.0)__WATCHOS_AVAILABLE(3.0)__OSX_AVAILABLE(10.14) __IOS_AVAILABLE(10.0)__TVOS_AVAILABLE(10.0)__WATCHOS_AVAILABLE(3.0)__OSX_AVAILABLE(10.14)
{ {
OWSLogInfo(@""); OWSLogInfo(@"");
if (notification.request.content.userInfo[@"remote"]) {
OWSLogInfo(@"[Loki] Ignore remote notifications when app is foreground.");
return;
}
[AppReadiness runNowOrWhenAppDidBecomeReady:^() { [AppReadiness runNowOrWhenAppDidBecomeReady:^() {
// We need to respect the in-app notification sound preference. This method, which is called // We need to respect the in-app notification sound preference. This method, which is called
// for modern UNUserNotification users, could be a place to do that, but since we'd still // for modern UNUserNotification users, could be a place to do that, but since we'd still

@ -15,11 +15,16 @@ final class LokiPushNotificationManager : NSObject {
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
let oldToken = userDefaults[.deviceToken] let oldToken = userDefaults[.deviceToken]
let lastUploadTime = userDefaults[.lastDeviceTokenUpload] let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
let applyNormalNotification = userDefaults[.applyNormalNotification]
let now = Date().timeIntervalSince1970 let now = Date().timeIntervalSince1970
if hexEncodedToken == oldToken && now - lastUploadTime < 2 * 24 * 60 * 60 { if hexEncodedToken == oldToken && now - lastUploadTime < 2 * 24 * 60 * 60 {
print("[Loki] Device token hasn't changed; no need to upload.") print("[Loki] Device token hasn't changed; no need to upload.")
return return
} }
if applyNormalNotification {
print("[Loki] Using normal notification; no need to upload.")
return
}
// Send token to Loki server // Send token to Loki server
let parameters = [ "token" : hexEncodedToken ] let parameters = [ "token" : hexEncodedToken ]
#if DEBUG #if DEBUG
@ -36,6 +41,35 @@ final class LokiPushNotificationManager : NSObject {
} }
userDefaults[.deviceToken] = hexEncodedToken userDefaults[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload] = now userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.applyNormalNotification] = false
}, failure: { _, error in
print("[Loki] Couldn't register device token.")
})
}
@objc(registerWithToken: pubkey:)
func register(with token: Data, pubkey: String) {
let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined()
let userDefaults = UserDefaults.standard
let now = Date().timeIntervalSince1970
// Send token to Loki server
let parameters = [ "token" : hexEncodedToken,
"pubKey": pubkey]
#if DEBUG
let url = URL(string: "https://dev.apns.getsession.org/register")!
#else
let url = URL(string: "https://live.apns.getsession.org/register")!
#endif
let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
TSNetworkManager.shared().makeRequest(request, success: { _, response in
guard let json = response as? JSON else { return }
guard json["code"] as? Int != 0 else {
return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").")
}
userDefaults[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.applyNormalNotification] = true
}, failure: { _, error in }, failure: { _, error in
print("[Loki] Couldn't register device token.") print("[Loki] Couldn't register device token.")
}) })

@ -642,7 +642,8 @@ class NotificationActionHandler {
func showThread(userInfo: [AnyHashable: Any]) throws -> Promise<Void> { func showThread(userInfo: [AnyHashable: Any]) throws -> Promise<Void> {
guard let threadId = userInfo[AppNotificationUserInfoKey.threadId] as? String else { guard let threadId = userInfo[AppNotificationUserInfoKey.threadId] as? String else {
throw NotificationError.failDebug("threadId was unexpectedly nil") return showHomePage()
// throw NotificationError.failDebug("threadId was unexpectedly nil")
} }
// If this happens when the the app is not, visible we skip the animation so the thread // If this happens when the the app is not, visible we skip the animation so the thread
@ -653,6 +654,11 @@ class NotificationActionHandler {
return Promise.value(()) return Promise.value(())
} }
func showHomePage() -> Promise<Void> {
signalApp.showHomeView()
return Promise.value(())
}
private func markAsRead(thread: TSThread) -> Promise<Void> { private func markAsRead(thread: TSThread) -> Promise<Void> {
return dbConnection.readWritePromise { transaction in return dbConnection.readWritePromise { transaction in
thread.markAllAsRead(with: transaction) thread.markAllAsRead(with: transaction)

@ -8,6 +8,7 @@ public enum LKUserDefaults {
case hasViewedSeed case hasViewedSeed
/// Whether the device was unlinked as a slave device (used to notify the user on the landing screen). /// Whether the device was unlinked as a slave device (used to notify the user on the landing screen).
case wasUnlinked case wasUnlinked
case applyNormalNotification
} }
public enum Date : Swift.String { public enum Date : Swift.String {

@ -105,6 +105,11 @@ public class TypingIndicatorMessage: TSOutgoingMessage {
return false return false
} }
@objc
public override var ttl: UInt32 {
return UInt32(5 * kMinuteInMs)
}
@objc @objc
public override var debugDescription: String { public override var debugDescription: String {
return "typingIndicatorMessage" return "typingIndicatorMessage"

Loading…
Cancel
Save