Merge pull request #147 from loki-project/push-notifications

Improved Push Notifications Stage 1
pull/156/head
gmbnt 5 years ago committed by GitHub
commit 388f3e5471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4725,6 +4725,7 @@
LLVM_LTO = NO;
MARKETING_VERSION = 1.0.8;
OTHER_LDFLAGS = "$(inherited)";
OTHER_SWIFT_FLAGS = "$(inherited) \"-D\" \"COCOAPODS\" \"-DDEBUG\"";
PRODUCT_BUNDLE_IDENTIFIER = "com.loki-project.loki-messenger";
PRODUCT_NAME = Session;
PROVISIONING_PROFILE = "";

@ -59,7 +59,9 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
static NSTimeInterval launchStartedAt;
// Debug settings
static BOOL isInternalTestVersion = NO;
static BOOL isUsingFullAPNs = YES;
@interface AppDelegate () <UNUserNotificationCenterDelegate>
@ -587,7 +589,11 @@ static BOOL isInternalTestVersion = NO;
}
OWSLogInfo(@"Registered for push notifications with token: %@.", deviceToken);
[LKPushNotificationManager.shared registerWithToken:deviceToken];
if (isUsingFullAPNs) {
[LKPushNotificationManager registerWithToken:deviceToken hexEncodedPublicKey:self.tsAccountManager.localNumber];
} else {
[LKPushNotificationManager registerWithToken:deviceToken];
}
// [self.pushRegistrationManager didReceiveVanillaPushToken:deviceToken];
}
@ -1552,6 +1558,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] Ignoring remote notifications while the app is in the 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

@ -1,41 +1,71 @@
import UIKit
// Ideally this should be in SignalServiceKit, but somehow linking fails when it is.
@objc(LKPushNotificationManager)
final class LokiPushNotificationManager : NSObject {
@objc static let shared = LokiPushNotificationManager()
private override init() { super.init() }
// MARK: Settings
#if DEBUG
private static let url = URL(string: "https://dev.apns.getsession.org/register")!
#else
private static let url = URL(string: "https://live.apns.getsession.org/register")!
#endif
private static let tokenExpirationInterval: TimeInterval = 2 * 24 * 60 * 60
// MARK: Initialization
private override init() { }
// MARK: Registration
@objc(registerWithToken:)
func register(with token: Data) {
let hexEncodedToken = token.map { String(format: "%02.2hhx", $0) }.joined()
static func register(with token: Data) {
let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard
let oldToken = userDefaults[.deviceToken]
let lastUploadTime = userDefaults[.lastDeviceTokenUpload]
let isUsingFullAPNs = userDefaults[.isUsingFullAPNs]
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
guard hexEncodedToken != oldToken || now - lastUploadTime < tokenExpirationInterval else {
return print("[Loki] Device token hasn't changed; no need to re-upload.")
}
guard !isUsingFullAPNs else {
return print("[Loki] Using full APNs; no need to upload device token.")
}
// Send token to Loki server
let parameters = [ "token" : hexEncodedToken ]
#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 let json = response as? JSON else {
return print("[Loki] Couldn't register device token.")
}
guard json["code"] as? Int != 0 else {
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
}
userDefaults[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.isUsingFullAPNs] = false
}, failure: { _, error in
print("[Loki] Couldn't register device token.")
})
}
@objc(registerWithToken:hexEncodedPublicKey:)
static func register(with token: Data, hexEncodedPublicKey: String) {
let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard
let now = Date().timeIntervalSince1970
let parameters = [ "token" : hexEncodedToken, "pubKey" : hexEncodedPublicKey]
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 print("[Loki] Couldn't register device token.")
}
guard json["code"] as? Int != 0 else {
return print("[Loki] An error occured during device token registration: \(json["message"] as? String ?? "nil").")
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").")
}
userDefaults[.deviceToken] = hexEncodedToken
userDefaults[.lastDeviceTokenUpload] = now
userDefaults[.isUsingFullAPNs] = true
}, failure: { _, error in
print("[Loki] Couldn't register device token.")
})

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

@ -79,7 +79,7 @@
}
#pragma mark Settings
- (uint)ttl { return 2 * kMinuteInMs; }
- (uint)ttl { return 4 * kMinuteInMs; }
- (BOOL)shouldSyncTranscript { return NO; }
- (BOOL)shouldBeSaved { return NO; }

@ -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 isUsingFullAPNs
}
public enum Date : Swift.String {

@ -99,6 +99,9 @@ public class TypingIndicatorMessage: TSOutgoingMessage {
public override func shouldBeSaved() -> Bool {
return false
}
@objc
public override var ttl: UInt32 { return UInt32(2 * kMinuteInMs) }
@objc
public override var debugDescription: String {

Loading…
Cancel
Save