From ad8ae2f2c59b79ebf829ac14cc664ca1e7d532dd Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 25 Mar 2020 11:49:43 +1100 Subject: [PATCH 1/4] hide notification when app is foreground --- .../UserInterface/Notifications/AppNotifications.swift | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Signal/src/UserInterface/Notifications/AppNotifications.swift b/Signal/src/UserInterface/Notifications/AppNotifications.swift index e22aa60c4..254720bc9 100644 --- a/Signal/src/UserInterface/Notifications/AppNotifications.swift +++ b/Signal/src/UserInterface/Notifications/AppNotifications.swift @@ -642,7 +642,8 @@ class NotificationActionHandler { func showThread(userInfo: [AnyHashable: Any]) throws -> Promise { 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 @@ -652,6 +653,11 @@ class NotificationActionHandler { signalApp.presentConversationAndScrollToFirstUnreadMessage(forThreadId: threadId, animated: shouldAnimate) return Promise.value(()) } + + func showHomePage() -> Promise { + signalApp.showHomeView() + return Promise.value(()) + } private func markAsRead(thread: TSThread) -> Promise { return dbConnection.readWritePromise { transaction in From 2e0ca402b3e2f377327fbbf97770a997e9600715 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 25 Mar 2020 11:50:09 +1100 Subject: [PATCH 2/4] modify ttl for typing messages --- SignalServiceKit/src/Messages/TypingIndicatorMessage.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/SignalServiceKit/src/Messages/TypingIndicatorMessage.swift b/SignalServiceKit/src/Messages/TypingIndicatorMessage.swift index c1306908c..fc2d6af3d 100644 --- a/SignalServiceKit/src/Messages/TypingIndicatorMessage.swift +++ b/SignalServiceKit/src/Messages/TypingIndicatorMessage.swift @@ -99,6 +99,11 @@ public class TypingIndicatorMessage: TSOutgoingMessage { public override func shouldBeSaved() -> Bool { return false } + + @objc + public override var ttl: UInt32 { + return UInt32(5 * kMinuteInMs) + } @objc public override var debugDescription: String { From dae9fd3c1f8670e45961e197f29d585f32180508 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 25 Mar 2020 11:50:46 +1100 Subject: [PATCH 3/4] push pubkey and token to server and store notification setting locally --- Signal/src/AppDelegate.m | 8 ++++- .../LokiPushNotificationManager.swift | 34 +++++++++++++++++++ .../src/Loki/Utilities/LKUserDefaults.swift | 1 + 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Signal/src/AppDelegate.m b/Signal/src/AppDelegate.m index 19db0cfee..1d2df82c4 100644 --- a/Signal/src/AppDelegate.m +++ b/Signal/src/AppDelegate.m @@ -587,7 +587,9 @@ static BOOL isInternalTestVersion = NO; } 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]; } @@ -1552,6 +1554,10 @@ static BOOL isInternalTestVersion = NO; __IOS_AVAILABLE(10.0)__TVOS_AVAILABLE(10.0)__WATCHOS_AVAILABLE(3.0)__OSX_AVAILABLE(10.14) { OWSLogInfo(@""); + if (notification.request.content.userInfo[@"remote"]) { + OWSLogInfo(@"[Loki] Ignore remote notifications when app is foreground."); + return; + } [AppReadiness runNowOrWhenAppDidBecomeReady:^() { // 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 diff --git a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift index 9624c4323..f51effb8d 100644 --- a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift +++ b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift @@ -15,11 +15,16 @@ final class LokiPushNotificationManager : NSObject { let userDefaults = UserDefaults.standard let oldToken = userDefaults[.deviceToken] let lastUploadTime = userDefaults[.lastDeviceTokenUpload] + let applyNormalNotification = userDefaults[.applyNormalNotification] let now = Date().timeIntervalSince1970 if hexEncodedToken == oldToken && now - lastUploadTime < 2 * 24 * 60 * 60 { print("[Loki] Device token hasn't changed; no need to upload.") return } + if applyNormalNotification { + print("[Loki] Using normal notification; no need to upload.") + return + } // Send token to Loki server let parameters = [ "token" : hexEncodedToken ] #if DEBUG @@ -36,6 +41,35 @@ final class LokiPushNotificationManager : NSObject { } userDefaults[.deviceToken] = hexEncodedToken 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 print("[Loki] Couldn't register device token.") }) diff --git a/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift b/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift index 84921c642..4a21e975e 100644 --- a/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift +++ b/SignalServiceKit/src/Loki/Utilities/LKUserDefaults.swift @@ -8,6 +8,7 @@ public enum LKUserDefaults { case hasViewedSeed /// Whether the device was unlinked as a slave device (used to notify the user on the landing screen). case wasUnlinked + case applyNormalNotification } public enum Date : Swift.String { From 666f836c5dd0014236cb0a70cdf37de4ec368a85 Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 25 Mar 2020 13:00:58 +1100 Subject: [PATCH 4/4] fix typo --- Signal/src/Loki/Utilities/LokiPushNotificationManager.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift index f51effb8d..c5b65f8df 100644 --- a/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift +++ b/Signal/src/Loki/Utilities/LokiPushNotificationManager.swift @@ -54,7 +54,7 @@ final class LokiPushNotificationManager : NSObject { let now = Date().timeIntervalSince1970 // Send token to Loki server let parameters = [ "token" : hexEncodedToken, - "pubkey": pubkey] + "pubKey": pubkey] #if DEBUG let url = URL(string: "https://dev.apns.getsession.org/register")! #else