Rework push registration

== Account Registration ==

Not complete until push tokens are uploaded

== Remote Notifications Registration ==

Extracted from PushManager

- wait for notification-settings registration to complete before
  requesting push tokens, otherwise it's possible token requests will
  be ignored.

- Less state required for push notification callbacks, specifically, we
  no longer need to ensure we've created a promise before the
  registration delegate methods get called.

- no more TOCFuture in Signal-iOS (still in SSK for now). It's not in
  cases of inexplicable behavior - one a recently, push notification
  premature free, in redphone, and more popular use, and I've seen two
  futures inexplicably being nil.  Instead, let's consolidate around
  PromiseKit for popularly used, maintained, strongly-typed futures.

- separate logic for registering for vanilla push/voip notifications
  (few dependencies) from responding to UILocalNotifications (lots of
  dependencies). Ultimately I'd like to consolidate the remaining
  UILocalNotifications logic with the existing NotificationsManager

== Misc ==

more debug logging
more uniform logging
remove stale logic around newly registered user

// FREEBIE
This commit is contained in:
Michael Kirk 2017-09-20 17:47:12 -04:00
parent b916e14abc
commit df15c904bc
18 changed files with 335 additions and 256 deletions

View File

@ -156,6 +156,8 @@
452ECA4E1E087E7200E2F016 /* MessageFetcherJob.swift in Sources */ = {isa = PBXBuildFile; fileRef = 452ECA4C1E087E7200E2F016 /* MessageFetcherJob.swift */; };
4531C9C41DD8E6D800F08304 /* JSQMessagesCollectionViewCell+OWS.m in Sources */ = {isa = PBXBuildFile; fileRef = 4531C9C31DD8E6D800F08304 /* JSQMessagesCollectionViewCell+OWS.m */; };
45387B041E36D650005D00B3 /* OWS102MoveLoggingPreferenceToUserDefaults.m in Sources */ = {isa = PBXBuildFile; fileRef = 45387B031E36D650005D00B3 /* OWS102MoveLoggingPreferenceToUserDefaults.m */; };
4539B5861F79348F007141FF /* PushRegistrationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4539B5851F79348F007141FF /* PushRegistrationManager.swift */; };
4539B5871F79348F007141FF /* PushRegistrationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4539B5851F79348F007141FF /* PushRegistrationManager.swift */; };
453D28BA1D332DB100D523F0 /* OWSMessagesBubblesSizeCalculator.m in Sources */ = {isa = PBXBuildFile; fileRef = 453D28B91D332DB100D523F0 /* OWSMessagesBubblesSizeCalculator.m */; };
453D28BB1D332DB100D523F0 /* OWSMessagesBubblesSizeCalculator.m in Sources */ = {isa = PBXBuildFile; fileRef = 453D28B91D332DB100D523F0 /* OWSMessagesBubblesSizeCalculator.m */; };
4542F0941EB9372700C7EE92 /* SystemContactsFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4542F0931EB9372700C7EE92 /* SystemContactsFetcher.swift */; };
@ -303,7 +305,6 @@
B6258B331C29E2E60014138E /* NotificationsManager.m in Sources */ = {isa = PBXBuildFile; fileRef = B6258B321C29E2E60014138E /* NotificationsManager.m */; };
B625CD561ABB589C00E8B23C /* NewMessage.aifc in Resources */ = {isa = PBXBuildFile; fileRef = B625CD551ABB589C00E8B23C /* NewMessage.aifc */; };
B62D53F71A23CCAD009AAF82 /* TSMessageAdapter.m in Sources */ = {isa = PBXBuildFile; fileRef = B62D53F61A23CCAD009AAF82 /* TSMessageAdapter.m */; };
B62F5E101C2980B4000D370C /* NSData+ows_StripToken.m in Sources */ = {isa = PBXBuildFile; fileRef = B62F5E0F1C2980B4000D370C /* NSData+ows_StripToken.m */; };
B633C5861A1D190B0059AC12 /* call@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B633C5041A1D190B0059AC12 /* call@2x.png */; };
B633C58D1A1D190B0059AC12 /* contact_default_feed.png in Resources */ = {isa = PBXBuildFile; fileRef = B633C50B1A1D190B0059AC12 /* contact_default_feed.png */; };
B633C59D1A1D190B0059AC12 /* endcall@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B633C51B1A1D190B0059AC12 /* endcall@2x.png */; };
@ -625,6 +626,7 @@
4531C9C31DD8E6D800F08304 /* JSQMessagesCollectionViewCell+OWS.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "JSQMessagesCollectionViewCell+OWS.m"; sourceTree = "<group>"; };
45387B021E36D650005D00B3 /* OWS102MoveLoggingPreferenceToUserDefaults.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = OWS102MoveLoggingPreferenceToUserDefaults.h; path = Migrations/OWS102MoveLoggingPreferenceToUserDefaults.h; sourceTree = "<group>"; };
45387B031E36D650005D00B3 /* OWS102MoveLoggingPreferenceToUserDefaults.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = OWS102MoveLoggingPreferenceToUserDefaults.m; path = Migrations/OWS102MoveLoggingPreferenceToUserDefaults.m; sourceTree = "<group>"; };
4539B5851F79348F007141FF /* PushRegistrationManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PushRegistrationManager.swift; sourceTree = "<group>"; };
453CC0361D08E1A60040EBA3 /* sn */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = sn; path = translations/sn.lproj/Localizable.strings; sourceTree = "<group>"; };
453D28B81D332DB100D523F0 /* OWSMessagesBubblesSizeCalculator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OWSMessagesBubblesSizeCalculator.h; sourceTree = "<group>"; };
453D28B91D332DB100D523F0 /* OWSMessagesBubblesSizeCalculator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = OWSMessagesBubblesSizeCalculator.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
@ -786,8 +788,6 @@
B625CD551ABB589C00E8B23C /* NewMessage.aifc */ = {isa = PBXFileReference; lastKnownFileType = file; path = NewMessage.aifc; sourceTree = "<group>"; };
B62D53F51A23CCAD009AAF82 /* TSMessageAdapter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TSMessageAdapter.h; sourceTree = "<group>"; };
B62D53F61A23CCAD009AAF82 /* TSMessageAdapter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TSMessageAdapter.m; sourceTree = "<group>"; };
B62F5E0E1C2980B4000D370C /* NSData+ows_StripToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSData+ows_StripToken.h"; sourceTree = "<group>"; };
B62F5E0F1C2980B4000D370C /* NSData+ows_StripToken.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSData+ows_StripToken.m"; sourceTree = "<group>"; };
B633C5041A1D190B0059AC12 /* call@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "call@2x.png"; sourceTree = "<group>"; };
B633C50B1A1D190B0059AC12 /* contact_default_feed.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = contact_default_feed.png; sourceTree = "<group>"; };
B633C51B1A1D190B0059AC12 /* endcall@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "endcall@2x.png"; sourceTree = "<group>"; };
@ -1415,6 +1415,7 @@
B6B1013B196D213F007E3930 /* SignalKeyingStorage.m */,
B60C16631988999D00E97A6C /* VersionMigrations.h */,
B60C16641988999D00E97A6C /* VersionMigrations.m */,
4539B5851F79348F007141FF /* PushRegistrationManager.swift */,
);
path = environment;
sourceTree = "<group>";
@ -1446,8 +1447,6 @@
455AC69A1F4F79E500134004 /* ImageCache.swift */,
34CA1C231F706B5400E51C51 /* NSAttributedString+OWS.h */,
34CA1C241F706B5400E51C51 /* NSAttributedString+OWS.m */,
B62F5E0E1C2980B4000D370C /* NSData+ows_StripToken.h */,
B62F5E0F1C2980B4000D370C /* NSData+ows_StripToken.m */,
76EB04EC18170B33006006FC /* NumberUtil.h */,
76EB04ED18170B33006006FC /* NumberUtil.m */,
76EB04EE18170B33006006FC /* Operation.h */,
@ -2351,6 +2350,7 @@
7038632718F70C0700D4A43F /* CryptoTools.m in Sources */,
45FBC5C81DF8575700E9B410 /* CallKitCallManager.swift in Sources */,
34B3F8911E8DF1710035BE1A /* ShowGroupMembersViewController.m in Sources */,
4539B5861F79348F007141FF /* PushRegistrationManager.swift in Sources */,
45FBC5D11DF8592E00E9B410 /* SignalCall.swift in Sources */,
452C468F1E427E200087B011 /* OutboundCallInitiator.swift in Sources */,
45F170BB1E2FC5D3003FC1F2 /* CallAudioService.swift in Sources */,
@ -2390,7 +2390,6 @@
34B3F87E1E8DF1700035BE1A /* InboxTableViewCell.m in Sources */,
34B3F8731E8DF1700035BE1A /* AttachmentApprovalViewController.swift in Sources */,
B6B1013C196D213F007E3930 /* SignalKeyingStorage.m in Sources */,
B62F5E101C2980B4000D370C /* NSData+ows_StripToken.m in Sources */,
34D5CC961EA6AFAD005515DB /* OWSContactsSyncing.m in Sources */,
45E2E9201E153B3D00457AA0 /* Strings.swift in Sources */,
34B3F88B1E8DF1700035BE1A /* OWSLinkedDevicesTableViewController.m in Sources */,
@ -2448,6 +2447,7 @@
45F170AF1E2F0393003FC1F2 /* CallAudioSessionTest.swift in Sources */,
34B3F8991E8DF1B90035BE1A /* TSMessageAdapterTest.m in Sources */,
456F6E231E24133500FD2210 /* Platform.swift in Sources */,
4539B5871F79348F007141FF /* PushRegistrationManager.swift in Sources */,
4504493A1F45EE7D002D1ADA /* NSString+OWS.m in Sources */,
45843D201D2236B30013E85A /* OWSContactsSearcher.m in Sources */,
45AE48521E0732D6004D96C2 /* TurnServerInfo.swift in Sources */,

View File

@ -124,7 +124,6 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
[self setupEnvironment];
[UIUtil applySignalAppearence];
[[PushManager sharedManager] registerPushKitNotificationFuture];
if (getenv("runningTests_dontStartApp")) {
return YES;
@ -272,7 +271,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
DDLogInfo(@"%@ registered vanilla push token: %@", self.tag, deviceToken);
[PushManager.sharedManager.pushNotificationFutureSource trySetResult:deviceToken];
[PushRegistrationManager.sharedManager didReceiveVanillaPushToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
@ -280,10 +279,10 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
DDLogError(@"%@ failed to register vanilla push token with error: %@", self.tag, error);
#ifdef DEBUG
DDLogWarn(@"%@ We're in debug mode. Faking success for remote registration with a fake push identifier", self.tag);
[PushManager.sharedManager.pushNotificationFutureSource trySetResult:[[NSMutableData dataWithLength:32] copy]];
[PushRegistrationManager.sharedManager didReceiveVanillaPushToken:[[NSMutableData dataWithLength:32] copy]];
#else
OWSProdError([OWSAnalyticsEvents appDelegateErrorFailedToRegisterForRemoteNotifications]);
[PushManager.sharedManager.pushNotificationFutureSource trySetFailure:error];
[PushRegistrationManager.sharedManager didFailToReceiveVanillaPushTokenWithError:error];
#endif
}
@ -291,7 +290,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
DDLogInfo(@"%@ registered user notification settings", self.tag);
[PushManager.sharedManager.userNotificationFutureSource trySetResult:notificationSettings];
[PushRegistrationManager.sharedManager didRegisterUserNotificationSettings];
}
- (BOOL)application:(UIApplication *)application
@ -813,7 +812,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
- (void)databaseViewRegistrationComplete
{
DDLogInfo(@"databaseViewRegistrationComplete");
DDLogInfo(@"%@ databaseViewRegistrationComplete", self.tag);
if ([TSAccountManager isRegistered]) {
DDLogInfo(@"localNumber: %@", [TSAccountManager localNumber]);
@ -824,9 +823,8 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
[[Environment getCurrent].messageFetcherJob runAsync];
// This should happen at any launch, background or foreground.
__unused AnyPromise *promise = [OWSSyncPushTokensJob runWithPushManager:[PushManager sharedManager]
accountManager:[Environment getCurrent].accountManager
preferences:[Environment preferences]];
__unused AnyPromise *promise = [OWSSyncPushTokensJob runWithAccountManager:[Environment getCurrent].accountManager
preferences:[Environment preferences]];
}
[DeviceSleepManager.sharedInstance removeBlockWithBlockObject:self];
@ -873,7 +871,7 @@ static NSString *const kURLHostVerifyPrefix = @"verify";
- (void)ensureRootViewController
{
DDLogInfo(@"ensureRootViewController");
DDLogInfo(@"%@ ensureRootViewController", self.tag);
if ([TSDatabaseView hasPendingViewRegistrations] || self.hasInitialRootViewController) {
return;

View File

@ -11,6 +11,7 @@ import PromiseKit
*/
class AccountManager: NSObject {
let TAG = "[AccountManager]"
let textSecureAccountManager: TSAccountManager
let networkManager: TSNetworkManager
let preferences: OWSPreferences
@ -33,24 +34,22 @@ class AccountManager: NSObject {
}
func register(verificationCode: String) -> Promise<Void> {
let registrationPromise = firstly {
Promise { fulfill, reject in
if verificationCode.characters.count == 0 {
let error = OWSErrorWithCodeDescription(.userError,
NSLocalizedString("REGISTRATION_ERROR_BLANK_VERIFICATION_CODE",
comment: "alert body during registration"))
reject(error)
}
fulfill()
}
}.then {
Logger.debug("\(self.TAG) verification code looks well formed.")
return self.registerForTextSecure(verificationCode: verificationCode)
}.then {
return SyncPushTokensJob.run(pushManager: self.pushManager, accountManager: self, preferences: self.preferences)
}.then {
Logger.debug("\(self.TAG) successfully registered for TextSecure")
guard verificationCode.characters.count > 0 else {
let error = OWSErrorWithCodeDescription(.userError,
NSLocalizedString("REGISTRATION_ERROR_BLANK_VERIFICATION_CODE",
comment: "alert body during registration"))
return Promise(error: error)
}
Logger.debug("\(self.TAG) registering with signal server")
let registrationPromise: Promise<Void> = firstly {
self.registerForTextSecure(verificationCode: verificationCode)
}.then {
self.syncPushTokens()
}.then {
self.completeRegistration()
}
registrationPromise.retainUntilComplete()
return registrationPromise
@ -64,6 +63,16 @@ class AccountManager: NSObject {
}
}
private func syncPushTokens() -> Promise<Void> {
Logger.info("\(self.TAG) in \(#function)")
return SyncPushTokensJob.run(accountManager: self, preferences: self.preferences)
}
private func completeRegistration() {
Logger.info("\(self.TAG) in \(#function)")
self.textSecureAccountManager.didRegister()
}
// MARK: Push Tokens
func updatePushTokens(pushToken: String, voipToken: String) -> Promise<Void> {

View File

@ -2,25 +2,28 @@
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
import Foundation
import PromiseKit
@objc(OWSSyncPushTokensJob)
class SyncPushTokensJob: NSObject {
let TAG = "[SyncPushTokensJob]"
let pushManager: PushManager
// MARK: Dependencies
let accountManager: AccountManager
let preferences: OWSPreferences
var pushRegistrationManager: PushRegistrationManager {
return PushRegistrationManager.shared
}
var uploadOnlyIfStale = true
required init(pushManager: PushManager, accountManager: AccountManager, preferences: OWSPreferences) {
self.pushManager = pushManager
required init(accountManager: AccountManager, preferences: OWSPreferences) {
self.accountManager = accountManager
self.preferences = preferences
}
class func run(pushManager: PushManager, accountManager: AccountManager, preferences: OWSPreferences) -> Promise<Void> {
let job = self.init(pushManager: pushManager, accountManager: accountManager, preferences: preferences)
class func run(accountManager: AccountManager, preferences: OWSPreferences) -> Promise<Void> {
let job = self.init(accountManager: accountManager, preferences: preferences)
return job.run()
}
@ -28,42 +31,43 @@ class SyncPushTokensJob: NSObject {
Logger.info("\(TAG) Starting.")
let runPromise: Promise<Void> = DispatchQueue.main.promise {
// Required to potentially prompt user for notifications settings
// before `requestPushTokens` will return.
self.pushManager.validateUserNotificationSettings()
// HACK: no-op dispatch to work around a bug in PromiseKit/Swift which won't compile
// when dispatching complex Promise types. We should eventually be able to delete the
// following two lines, skipping this no-op dispatch.
return
}.then {
self.requestPushTokens()
return self.pushRegistrationManager.requestPushTokens()
}.then { (pushToken: String, voipToken: String) in
Logger.info("\(self.TAG) finished: requesting push tokens")
var shouldUploadTokens = false
if self.preferences.getPushToken() != pushToken || self.preferences.getVoipToken() != voipToken {
Logger.debug("\(self.TAG) Push tokens changed.")
shouldUploadTokens = true
} else if !self.uploadOnlyIfStale {
Logger.debug("\(self.TAG) Uploading even though tokens didn't change.")
Logger.debug("\(self.TAG) Forced uploading, even though tokens didn't change.")
shouldUploadTokens = true
}
Logger.warn("\(self.TAG) lastAppVersion: \(AppVersion.instance().lastAppVersion), currentAppVersion: \(AppVersion.instance().currentAppVersion)")
if AppVersion.instance().lastAppVersion != AppVersion.instance().currentAppVersion {
Logger.debug("\(self.TAG) Fresh install or app upgrade.")
Logger.info("\(self.TAG) Uploading due to fresh install or app upgrade.")
shouldUploadTokens = true
}
guard shouldUploadTokens else {
Logger.warn("\(self.TAG) Skipping push token upload. pushToken: \(pushToken), voipToken: \(voipToken)")
Logger.info("\(self.TAG) No reason to upload pushToken: \(pushToken), voipToken: \(voipToken)")
return Promise(value: ())
}
Logger.warn("\(self.TAG) Sending new tokens to account servers. pushToken: \(pushToken), voipToken: \(voipToken)")
Logger.warn("\(self.TAG) uploading tokens to account servers. pushToken: \(pushToken), voipToken: \(voipToken)")
return self.accountManager.updatePushTokens(pushToken:pushToken, voipToken:voipToken).then {
return self.recordNewPushTokens(pushToken:pushToken, voipToken:voipToken)
}.then {
Logger.debug("\(self.TAG) Successfully ran syncPushTokensJob.")
}.catch { error in
Logger.error("\(self.TAG) Failed to run syncPushTokensJob with error: \(error).")
Logger.info("\(self.TAG) successfully updated push tokens on server")
return self.recordPushTokensLocally(pushToken:pushToken, voipToken:voipToken)
}
}.then {
Logger.info("\(self.TAG) in \(#function): succeeded")
}.catch { error in
Logger.error("\(self.TAG) in \(#function): Failed with error: \(error).")
}
runPromise.retainUntilComplete()
@ -73,8 +77,8 @@ class SyncPushTokensJob: NSObject {
// MARK - objc wrappers, since objc can't use swift parameterized types
@objc class func run(pushManager: PushManager, accountManager: AccountManager, preferences: OWSPreferences) -> AnyPromise {
let promise: Promise<Void> = self.run(pushManager: pushManager, accountManager: accountManager, preferences: preferences)
@objc class func run(accountManager: AccountManager, preferences: OWSPreferences) -> AnyPromise {
let promise: Promise<Void> = self.run(accountManager: accountManager, preferences: preferences)
return AnyPromise(promise)
}
@ -83,21 +87,8 @@ class SyncPushTokensJob: NSObject {
return AnyPromise(promise)
}
// MARK - private helpers
private func requestPushTokens() -> Promise<(pushToken: String, voipToken: String)> {
return Promise { fulfill, reject in
self.pushManager.requestPushToken(
success: { (pushToken: String, voipToken: String) in
fulfill((pushToken:pushToken, voipToken:voipToken))
},
failure: reject
)
}
}
private func recordNewPushTokens(pushToken: String, voipToken: String) -> Promise<Void> {
Logger.warn("\(TAG) Recording new push tokens. pushToken: \(pushToken), voipToken: \(voipToken)")
private func recordPushTokensLocally(pushToken: String, voipToken: String) -> Promise<Void> {
Logger.warn("\(TAG) Recording push tokens locally. pushToken: \(pushToken), voipToken: \(voipToken)")
if (pushToken != self.preferences.getPushToken()) {
Logger.info("\(TAG) Recording new plain push token")

View File

@ -241,9 +241,8 @@ NS_ASSUME_NONNULL_BEGIN
- (void)syncPushTokens
{
OWSSyncPushTokensJob *job =
[[OWSSyncPushTokensJob alloc] initWithPushManager:[PushManager sharedManager]
accountManager:[Environment getCurrent].accountManager
preferences:[Environment preferences]];
[[OWSSyncPushTokensJob alloc] initWithAccountManager:[Environment getCurrent].accountManager
preferences:[Environment preferences]];
job.uploadOnlyIfStale = NO;
[job run]
.then(^{

View File

@ -9,9 +9,6 @@
@interface HomeViewController : OWSViewController
// TODO: Remove this property.
@property (nonatomic) BOOL newlyRegisteredUser;
- (void)presentThread:(TSThread *)thread
keyboardOnViewAppearing:(BOOL)keyboardOnViewAppearing
callOnViewAppearing:(BOOL)callOnViewAppearing;

View File

@ -23,8 +23,6 @@
#import <PromiseKit/AnyPromise.h>
#import <SignalServiceKit/NSDate+OWS.h>
#import <SignalServiceKit/OWSBlockingManager.h>
#import <SignalServiceKit/OWSDisappearingMessagesJob.h>
#import <SignalServiceKit/OWSMessageManager.h>
#import <SignalServiceKit/OWSMessageSender.h>
#import <SignalServiceKit/TSOutgoingMessage.h>
#import <SignalServiceKit/Threading.h>
@ -490,13 +488,10 @@ typedef NS_ENUM(NSInteger, CellState) { kArchiveState, kInboxState };
{
[super viewDidAppear:animated];
if (self.newlyRegisteredUser) {
[self markAllUpgradeExperiencesAsSeen];
} else if (!self.viewHasEverAppeared) {
if (!self.viewHasEverAppeared) {
self.viewHasEverAppeared = YES;
[self displayAnyUnseenUpgradeExperience];
}
self.viewHasEverAppeared = YES;
}
#pragma mark - startup

View File

@ -437,7 +437,6 @@ NSString *const kProfileView_LastPresentedDate = @"kProfileView_LastPresentedDat
- (void)showHomeView
{
HomeViewController *homeView = [HomeViewController new];
homeView.newlyRegisteredUser = YES;
SignalsNavigationController *navigationController =
[[SignalsNavigationController alloc] initWithRootViewController:homeView];
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

View File

@ -27,8 +27,6 @@ NSString *const kTSStorageManager_lastKnownContactRecipientIds = @"lastKnownCont
@interface OWSContactsManager () <SystemContactsFetcherDelegate>
@property (atomic) id addressBookReference;
@property (atomic) TOCFuture *futureAddressBook;
@property (nonatomic) BOOL isContactsUpdateInFlight;
// This reflects the contents of the device phone book and includes
// contacts that do not correspond to any signal account.

View File

@ -0,0 +1,207 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
import Foundation
import PromiseKit
import PushKit
/**
* Singleton used to integrate with push notification services - registration and routing received remote notifications.
*/
@objc class PushRegistrationManager: NSObject, PKPushRegistryDelegate {
let TAG = "[PushRegistrationManager]"
// MARK - Dependencies
private var pushManager: PushManager {
return PushManager.shared()
}
// MARK - Singleton class
@objc(sharedManager)
static let shared = PushRegistrationManager()
private override init() {
super.init()
}
private enum PushRegistrationManagerError: Error {
case assertionError(description: String)
}
private var voipRegistry: PKPushRegistry?
private var fulfillVanillaTokenPromise: ((Data) -> Void)?
private var rejectVanillaTokenPromise: ((Error) -> Void)?
private var fulfillVoipTokenPromise: ((Data) -> Void)?
private var fulfillRegisterUserNotificationSettingsPromise: (() -> Void)?
// MARK: Public interface
public func requestPushTokens() -> Promise<(pushToken: String, voipToken: String)> {
Logger.info("\(self.TAG) in \(#function)")
return self.registerUserNotificationSettings().then {
guard !Platform.isSimulator else {
Logger.warn("\(self.TAG) Using fake push tokens for simulator")
return Promise(value: (pushToken: "fakePushToken", voipToken: "fakeVoipToken"))
}
return self.registerForVanillaPushToken().then { vanillaPushToken in
self.registerForVoipPushToken().then { voipPushToken in
(pushToken: vanillaPushToken, voipToken: voipPushToken)
}
}
}
}
// Notification registration is confirmed via AppDelegate
// Before this occurs, it is not safe to assume push token requests will be acknowledged.
//
// e.g. in the case that Background Fetch is disabled, token requests will be ignored until
// we register user notification settings.
@objc
public func didRegisterUserNotificationSettings() {
guard let fulfillRegisterUserNotificationSettingsPromise = self.fulfillRegisterUserNotificationSettingsPromise else {
owsFail("\(TAG) promise completion in \(#function) unexpectedly nil")
return
}
fulfillRegisterUserNotificationSettingsPromise()
}
// MARK: Vanilla push token
// Vanilla push token is obtained from the system via AppDelegate
@objc
public func didReceiveVanillaPushToken(_ tokenData: Data) {
guard let fulfillVanillaTokenPromise = self.fulfillVanillaTokenPromise else {
owsFail("\(TAG) promise completion in \(#function) unexpectedly nil")
return
}
fulfillVanillaTokenPromise(tokenData)
}
// Vanilla push token is obtained from the system via AppDelegate
@objc
public func didFailToReceiveVanillaPushToken(error: Error) {
guard let rejectVanillaTokenPromise = self.rejectVanillaTokenPromise else {
owsFail("\(TAG) promise completion in \(#function) unexpectedly nil")
return
}
rejectVanillaTokenPromise(error)
}
// MARK: PKPushRegistryDelegate - voIP Push Token
public func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, forType type: PKPushType) {
Logger.info("\(self.TAG) in \(#function)")
assert(type == .voIP)
self.pushManager.application(UIApplication.shared, didReceiveRemoteNotification: payload.dictionaryPayload)
}
public func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, forType type: PKPushType) {
Logger.info("\(self.TAG) in \(#function)")
assert(type == .voIP)
assert(credentials.type == .voIP)
guard let fulfillVoipTokenPromise = self.fulfillVoipTokenPromise else {
owsFail("\(TAG) fulfillVoipTokenPromise was unexpectedly nil")
return
}
fulfillVoipTokenPromise(credentials.token)
}
public func pushRegistry(_ registry: PKPushRegistry, didInvalidatePushTokenForType type: PKPushType) {
// It's not clear when this would happen. We've never previously handled it, but we should at
// least start learning if it happens.
owsFail("\(TAG) in \(#function)")
}
// MARK: helpers
// User notification settings must be registered *before* AppDelegate will
// return any requested push tokens. We don't consider the notifications settings registration
// *complete* until AppDelegate#didRegisterUserNotificationSettings is called.
private func registerUserNotificationSettings() -> Promise<Void> {
AssertIsOnMainThread()
guard fulfillRegisterUserNotificationSettingsPromise == nil else {
Logger.info("\(TAG) already registered user notification settings")
return Promise(value: ())
}
let (promise, fulfill, _) = Promise<Void>.pending()
self.fulfillRegisterUserNotificationSettingsPromise = fulfill
Logger.info("\(TAG) registering user notification settings")
UIApplication.shared.registerUserNotificationSettings(self.pushManager.userNotificationSettings)
return promise
}
private func registerForVanillaPushToken() -> Promise<String> {
Logger.info("\(self.TAG) in \(#function)")
AssertIsOnMainThread()
let (promise, fulfill, reject) = Promise<Data>.pending()
self.fulfillVanillaTokenPromise = fulfill
self.rejectVanillaTokenPromise = reject
UIApplication.shared.registerForRemoteNotifications()
return promise.then { (pushTokenData: Data) -> String in
Logger.info("\(self.TAG) successfully registered for vanilla push notifications")
return pushTokenData.hexEncodedString()
}
}
private func registerForVoipPushToken() -> Promise<String> {
AssertIsOnMainThread()
Logger.info("\(self.TAG) in \(#function)")
// Voip token not yet registered, assign promise.
let (promise, fulfill, reject) = Promise<Data>.pending()
self.fulfillVoipTokenPromise = fulfill
if self.voipRegistry == nil {
// We don't create the voip registry in init, because it immediately requests the voip token,
// potentially before we're ready to handle it.
let voipRegistry = PKPushRegistry(queue: nil)
self.voipRegistry = voipRegistry
voipRegistry.desiredPushTypes = [.voIP]
voipRegistry.delegate = self
}
guard let voipRegistry = self.voipRegistry else {
owsFail("\(TAG) failed to initialize voipRegistry in \(#function)")
reject(PushRegistrationManagerError.assertionError(description: "\(TAG) failed to initialize voipRegistry in \(#function)"))
return promise.then { _ in
// coerce expected type of returned promise - we don't really care about the value, since this promise has been rejected.
// in practice this shouldn't happen
String()
}
}
// If we've already completed registering for a voip token, resolve it immediately,
// rather than waiting for the delegate method to be called.
if let voipTokenData = voipRegistry.pushToken(forType: .voIP) {
Logger.info("\(self.TAG) using pre-registered voIP token")
fulfill(voipTokenData)
}
return promise.then { (voipTokenData: Data) -> String in
Logger.info("\(self.TAG) successfully registered for voip push notifications")
return voipTokenData.hexEncodedString()
}
}
}
// We transmit pushToken data as hex encoded string to the server
fileprivate extension Data {
func hexEncodedString() -> String {
return map { String(format: "%02hhx", $0) }.joined()
}
}

View File

@ -2,10 +2,6 @@
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
#import <CollapsingFutures.h>
#import <PushKit/PushKit.h>
#import <UIKit/UIApplication.h>
NS_ASSUME_NONNULL_BEGIN
@class UILocalNotification;
@ -37,38 +33,19 @@ typedef void (^failedPushRegistrationBlock)(NSError *error);
typedef void (^pushTokensSuccessBlock)(NSString *pushToken, NSString *voipToken);
/**
* The Push Manager is responsible for registering the device for Signal push notifications.
* The Push Manager is responsible for handling received push notifications.
*/
@interface PushManager : NSObject <PKPushRegistryDelegate>
@interface PushManager : NSObject
- (instancetype)init NS_UNAVAILABLE;
+ (PushManager *)sharedManager;
/**
* Returns the Push Notification Token of this device
*
* @param success Completion block that is passed the token as a parameter
* @param failure Failure block, executed when failed to get push token
* Settings required for the notification categories we use.
*/
- (void)requestPushTokenWithSuccess:(pushTokensSuccessBlock)success failure:(void (^)(NSError *))failure;
@property (nonatomic, readonly) UIUserNotificationSettings *userNotificationSettings;
/**
* Registers for Users Notifications. By doing this on launch, we are sure that the correct categories of user
* notifications is registered.
*/
- (void)validateUserNotificationSettings;
/**
* The pushNotification and userNotificationFutureSource are accessed by the App Delegate after requested permissions.
*/
@property (nullable, atomic, readwrite, strong) TOCFutureSource *pushNotificationFutureSource;
@property (nullable, atomic, readwrite, strong) TOCFutureSource *userNotificationFutureSource;
@property (nullable, atomic, readwrite, strong) TOCFutureSource *pushKitNotificationFutureSource;
- (TOCFuture *)registerPushKitNotificationFuture;
- (BOOL)supportsVOIPPush;
// If checkForCancel is set, the notification will be delayed for
// a moment. If a relevant cancel notification is received in that window,
// the notification will not be displayed.

View File

@ -4,7 +4,6 @@
#import "PushManager.h"
#import "AppDelegate.h"
#import "NSData+ows_StripToken.h"
#import "OWSContactsManager.h"
#import "Signal-Swift.h"
#import "ThreadUtil.h"
@ -30,7 +29,6 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe
@interface PushManager ()
@property (nonatomic) TOCFutureSource *registerWithServerFutureSource;
@property (nonatomic) NSMutableArray *currentNotifications;
@property (nonatomic) UIBackgroundTaskIdentifier callBackgroundTask;
@property (nonatomic, readonly) OWSMessageSender *messageSender;
@ -72,6 +70,7 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe
_messageSender = messageSender;
_messageFetcherJob = messageFetcherJob;
_callBackgroundTask = UIBackgroundTaskInvalid;
// TODO: consolidate notification tracking with NotificationsManager, which also maintains a list of notifications.
_currentNotifications = [NSMutableArray array];
OWSSingletonAssert();
@ -98,8 +97,9 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe
#pragma mark Manage Incoming Push
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
DDLogInfo(@"received: %s", __PRETTY_FUNCTION__);
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
DDLogInfo(@"%@ received remote notification", self.tag);
[self.messageFetcherJob runAsync];
}
@ -113,19 +113,23 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe
* "content-available:1" pushes if there is no "voip" token registered
*
*/
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
DDLogInfo(@"received: %s", __PRETTY_FUNCTION__);
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
DDLogInfo(@"%@ received content-available push", self.tag);
// If we want to re-introduce silent pushes we can remove this assert.
OWSFail(@"Unexpected content-available push.");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
completionHandler(UIBackgroundFetchResultNewData);
});
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
DDLogInfo(@"received: %s", __PRETTY_FUNCTION__);
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
DDLogInfo(@"%@ launched from local notification", self.tag);
NSString *_Nullable threadId = notification.userInfo[Signal_Thread_UserInfo_Key];
@ -139,8 +143,9 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe
- (void)application:(UIApplication *)application
handleActionWithIdentifier:(NSString *)identifier
forLocalNotification:(UILocalNotification *)notification
completionHandler:(void (^)())completionHandler {
DDLogInfo(@"received: %s", __PRETTY_FUNCTION__);
completionHandler:(void (^)())completionHandler
{
DDLogInfo(@"%@ in %s", self.tag, __FUNCTION__);
[self application:application
handleActionWithIdentifier:identifier
@ -264,82 +269,6 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe
}];
}
#pragma mark PushKit
- (void)pushRegistry:(PKPushRegistry *)registry
didUpdatePushCredentials:(PKPushCredentials *)credentials
forType:(NSString *)type {
[[PushManager sharedManager].pushKitNotificationFutureSource trySetResult:[credentials.token ows_tripToken]];
}
- (void)pushRegistry:(PKPushRegistry *)registry
didReceiveIncomingPushWithPayload:(PKPushPayload *)payload
forType:(NSString *)type {
DDLogInfo(@"received: %s", __PRETTY_FUNCTION__);
[self application:[UIApplication sharedApplication] didReceiveRemoteNotification:payload.dictionaryPayload];
}
- (TOCFuture *)registerPushKitNotificationFuture {
if ([self supportsVOIPPush]) {
self.pushKitNotificationFutureSource = [TOCFutureSource new];
PKPushRegistry *voipRegistry = [[PKPushRegistry alloc] initWithQueue:dispatch_get_main_queue()];
voipRegistry.delegate = self;
voipRegistry.desiredPushTypes = [NSSet setWithObject:PKPushTypeVoIP];
return self.pushKitNotificationFutureSource.future;
} else {
TOCFutureSource *futureSource = [TOCFutureSource new];
[futureSource trySetResult:nil];
return futureSource.future;
}
}
- (BOOL)supportsVOIPPush {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(8, 2)) {
return YES;
} else {
return NO;
}
}
#pragma mark Register device for Push Notification locally
- (TOCFuture *)registerPushNotificationFuture {
self.pushNotificationFutureSource = [TOCFutureSource new];
[UIApplication.sharedApplication registerForRemoteNotifications];
return self.pushNotificationFutureSource.future;
}
- (void)requestPushTokenWithSuccess:(pushTokensSuccessBlock)success failure:(failedPushRegistrationBlock)failure {
AssertIsOnMainThread();
if (!self.wantRemoteNotifications) {
DDLogWarn(@"%@ Using fake push tokens", self.tag);
success(@"fakePushToken", @"fakeVoipToken");
return;
}
TOCFuture *requestPushTokenFuture = [self registerPushNotificationFuture];
[requestPushTokenFuture thenDo:^(NSData *pushTokenData) {
NSString *pushToken = [pushTokenData ows_tripToken];
TOCFuture *pushKit = [self registerPushKitNotificationFuture];
[pushKit thenDo:^(NSString *voipToken) {
success(pushToken, voipToken);
}];
[pushKit catchDo:^(NSError *error) {
failure(error);
}];
}];
[requestPushTokenFuture catchDo:^(NSError *error) {
failure(error);
}];
}
- (UIUserNotificationCategory *)fullNewMessageNotificationCategory {
UIMutableUserNotificationAction *action_markRead = [self markAsReadAction];
@ -464,20 +393,13 @@ NSString *const PushManagerUserInfoKeysCallBackSignalRecipientId = @"PushManager
#pragma mark Util
- (BOOL)wantRemoteNotifications {
#if TARGET_IPHONE_SIMULATOR
return NO;
#else
return YES;
#endif
}
- (int)allNotificationTypes {
return UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge;
}
- (void)validateUserNotificationSettings
- (UIUserNotificationSettings *)userNotificationSettings
{
DDLogDebug(@"%@ registering user notification settings", self.tag);
UIUserNotificationSettings *settings = [UIUserNotificationSettings
settingsForTypes:(UIUserNotificationType)[self allNotificationTypes]
categories:[NSSet setWithObjects:[self fullNewMessageNotificationCategory],
@ -487,7 +409,7 @@ NSString *const PushManagerUserInfoKeysCallBackSignalRecipientId = @"PushManager
[self signalMissedCallWithNoLongerVerifiedIdentityChangeCategory],
nil]];
[UIApplication.sharedApplication registerUserNotificationSettings:settings];
return settings;
}
- (BOOL)applicationIsActive {
@ -500,6 +422,7 @@ NSString *const PushManagerUserInfoKeysCallBackSignalRecipientId = @"PushManager
return NO;
}
// TODO: consolidate notification tracking with NotificationsManager, which also maintains a list of notifications.
- (void)presentNotification:(UILocalNotification *)notification checkForCancel:(BOOL)checkForCancel
{
dispatch_async(dispatch_get_main_queue(), ^{
@ -517,6 +440,7 @@ NSString *const PushManagerUserInfoKeysCallBackSignalRecipientId = @"PushManager
});
}
// TODO: consolidate notification tracking with NotificationsManager, which also maintains a list of notifications.
- (void)cancelNotificationsWithThreadId:(NSString *)threadId
{
dispatch_async(dispatch_get_main_queue(), ^{

View File

@ -1,9 +0,0 @@
//
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
//
@interface NSData (ows_StripToken)
- (NSString *)ows_tripToken;
@end

View File

@ -1,20 +0,0 @@
//
// NSData+ows_StripToken.m
// Signal
//
// Created by Frederic Jacobs on 14/04/15.
// Copyright (c) 2015 Open Whisper Systems. All rights reserved.
//
#import "NSData+ows_StripToken.h"
@implementation NSData (ows_StripToken)
- (NSString *)ows_tripToken {
return [[[NSString stringWithFormat:@"%@", self]
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
stringByReplacingOccurrencesOfString:@" "
withString:@""];
}
@end

View File

@ -65,6 +65,12 @@ extern NSString *const kNSNotificationName_LocalNumberDidChange;
success:(void (^)())successBlock
failure:(void (^)(NSError *error))failureBlock;
// Called once registration is complete - meaning the following have succeeded:
// - obtained signal server credentials
// - uploaded pre-keys
// - uploaded push tokens
- (void)didRegister;
#if TARGET_OS_IPHONE
/**

View File

@ -290,11 +290,6 @@ NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegist
forNumber:phoneNumber
signalingKey:signalingKey
authKey:authToken];
void (^completedRegistrationBlock)() = ^{
[self didRegister];
[TSSocketManager requestSocketOpen];
successBlock();
};
[self.networkManager makeRequest:request
success:^(NSURLSessionDataTask *task, id responseObject) {
@ -304,9 +299,10 @@ NSString *const TSAccountManager_LocalRegistrationIdKey = @"TSStorageLocalRegist
switch (statuscode) {
case 200:
case 204: {
DDLogInfo(@"%@ Verification code accepted.", self.tag);
[TSStorageManager storeServerToken:authToken signalingKey:signalingKey];
[TSPreKeyManager registerPreKeysWithMode:RefreshPreKeysMode_SignedAndOneTime
success:completedRegistrationBlock
success:successBlock
failure:failureBlock];
break;
}

View File

@ -164,7 +164,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
self.state = SocketManagerStateOpen;
return;
case SR_CONNECTING:
DDLogVerbose(@"WebSocket is already connecting");
DDLogVerbose(@"%@ WebSocket is already connecting", self.tag);
self.state = SocketManagerStateConnecting;
return;
default:
@ -172,7 +172,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
}
}
DDLogWarn(@"Creating new websocket");
DDLogWarn(@"%@ Creating new websocket", self.tag);
// If socket is not already open or connecting, connect now.
//
@ -373,7 +373,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
- (void)processWebSocketRequestMessage:(WebSocketRequestMessage *)message {
OWSAssert([NSThread isMainThread]);
DDLogInfo(@"Got message with verb: %@ and path: %@", message.verb, message.path);
DDLogInfo(@"%@ Got message with verb: %@ and path: %@", self.tag, message.verb, message.path);
// If we receive a message over the socket while the app is in the background,
// prolong how long the socket stays open.
@ -385,7 +385,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
[Cryptography decryptAppleMessagePayload:message.body withSignalingKey:TSStorageManager.signalingKey];
if (!decryptedPayload) {
DDLogWarn(@"Failed to decrypt incoming payload or bad HMAC");
DDLogWarn(@"%@ Failed to decrypt incoming payload or bad HMAC", self.tag);
[self sendWebSocketMessageAcknowledgement:message];
return;
}
@ -396,7 +396,7 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
[self sendWebSocketMessageAcknowledgement:message];
} else {
DDLogWarn(@"Unsupported WebSocket Request");
DDLogWarn(@"%@ Unsupported WebSocket Request", self.tag);
[self sendWebSocketMessageAcknowledgement:message];
}

View File

@ -55,15 +55,15 @@ NSString *const kNSUserDefaults_LastCompletedLaunchAppVersion = @"kNSUserDefault
forKey:kNSUserDefaults_LastAppVersion];
[[NSUserDefaults standardUserDefaults] synchronize];
DDLogInfo(@"firstAppVersion: %@", self.firstAppVersion);
DDLogInfo(@"lastAppVersion: %@", self.lastAppVersion);
DDLogInfo(@"currentAppVersion: %@", self.currentAppVersion);
DDLogInfo(@"lastCompletedLaunchAppVersion: %@", self.lastCompletedLaunchAppVersion);
DDLogInfo(@"%@ firstAppVersion: %@", self.tag, self.firstAppVersion);
DDLogInfo(@"%@ lastAppVersion: %@", self.tag, self.lastAppVersion);
DDLogInfo(@"%@ currentAppVersion: %@", self.tag, self.currentAppVersion);
DDLogInfo(@"%@ lastCompletedLaunchAppVersion: %@", self.tag, self.lastCompletedLaunchAppVersion);
}
- (void)appLaunchDidComplete
{
DDLogInfo(@"appLaunchDidComplete");
DDLogInfo(@"%@ appLaunchDidComplete", self.tag);
self.lastCompletedLaunchAppVersion = self.currentAppVersion;
@ -78,4 +78,16 @@ NSString *const kNSUserDefaults_LastCompletedLaunchAppVersion = @"kNSUserDefault
return self.firstAppVersion != nil;
}
#pragma mark - Logging
+ (NSString *)tag
{
return [NSString stringWithFormat:@"[%@]", self.class];
}
- (NSString *)tag
{
return self.class.tag;
}
@end