Signal-iOS/NotificationServiceExtension/NotificationServiceExtensionContext.swift
Nora Trapp 88ac10de05 Notification Service Extension
The NSE should only run on iOS 13.3 or later where the "filtering" entitlement
is available since our notifications don't contain any content and will often
not trigger any user visible content. We control this by setting the deployment
target to iOS 13.3.

This does not handle calls as it's currently impossible to wake the main app or
launch CallKit from within the NSE. Should we reach a point where we need to use
this extension in production the service will need to be able to differentiate
between call and non-call messages and deliver them as VOIP or Vanilla pushes as
appropriate. Alternatively, Apple may introduce some way for us to signal the
main app that a call message has been received.

This does not currently address the potential for the NSE and the main app to be
running and trying to process messages at the same time. As long as the
websocket is connected and the main app is processing messages in a timely
fashion the NSE will never be called since the service will not send pushes for
these messages, but censorship circumvention users and users where the websocket
is disconnected for some reason will legitimately receive pushes and we will
want to process those messages. How we will handle these cases requires further
thought since just terminating the NSE when the app launches is not sufficient.
We could potentially do something like terminate the NSE everytime the main app
runs the message fetcher job which should only happen if either the user
pulls-to-refresh on the conversation list or the websocket is actively connected
and receiving messsages. I plan to address this in a follow-up pull request.

Currently this code will not ever be run since the service never sends vanilla
push notifications. Eventually we will need to add logic into the main app to:
a) detect we're on iOS 13.3 or later and b) update a flag on the service telling
it to stop using VOIP pushes for non-call messages. The API for requesting this
from the service does not yet exist.
2020-01-30 11:44:41 -08:00

90 lines
3.2 KiB
Swift

//
// Copyright (c) 2020 Open Whisper Systems. All rights reserved.
//
import Foundation
import SignalServiceKit
import SignalMessaging
class NotificationServiceExtensionContext: NSObject, AppContext {
let isMainApp = false
let isMainAppAndActive = false
func isInBackground() -> Bool { true }
func isAppForegroundAndActive() -> Bool { false }
func mainApplicationStateOnLaunch() -> UIApplication.State { .inactive }
var shouldProcessIncomingMessages: Bool { true }
func canPresentNotifications() -> Bool { true }
let appLaunchTime = Date()
lazy var buildTime: Date = {
guard let buildTimestamp = Bundle.main.object(forInfoDictionaryKey: "BuildTimestamp") as? TimeInterval, buildTimestamp > 0 else {
Logger.debug("No build timestamp, assuming app never expires.")
return .distantFuture
}
return .init(timeIntervalSince1970: buildTimestamp)
}()
func keychainStorage() -> SSKKeychainStorage {
return SSKDefaultKeychainStorage.shared
}
func appDocumentDirectoryPath() -> String {
guard let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else {
owsFail("failed to query document directory")
}
return documentDirectoryURL.path
}
func appSharedDataDirectoryPath() -> String {
guard let groupContainerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: TSConstants.applicationGroup) else {
owsFail("failed to query group container")
}
return groupContainerURL.path
}
func appDatabaseBaseDirectoryPath() -> String {
return appSharedDataDirectoryPath()
}
func appUserDefaults() -> UserDefaults {
guard let userDefaults = UserDefaults(suiteName: TSConstants.applicationGroup) else {
owsFail("failed to initialize user defaults")
}
return userDefaults
}
override init() { super.init() }
// MARK: - Unused in this extension
let isRTL = false
let isRunningTests = false
var mainWindow: UIWindow?
let frame: CGRect = .zero
let interfaceOrientation: UIInterfaceOrientation = .unknown
let reportedApplicationState: UIApplication.State = .background
let statusBarHeight: CGFloat = .zero
func beginBackgroundTask(expirationHandler: @escaping BackgroundTaskExpirationHandler) -> UInt { 0 }
func endBackgroundTask(_ backgroundTaskIdentifier: UInt) {}
func beginBackgroundTask(expirationHandler: @escaping BackgroundTaskExpirationHandler) -> UIBackgroundTaskIdentifier { .invalid }
func endBackgroundTask(_ backgroundTaskIdentifier: UIBackgroundTaskIdentifier) {}
func ensureSleepBlocking(_ shouldBeBlocking: Bool, blockingObjectsDescription: String) {}
func setMainAppBadgeNumber(_ value: Int) {}
func setStatusBarHidden(_ isHidden: Bool, animated isAnimated: Bool) {}
func frontmostViewController() -> UIViewController? { nil }
func openSystemSettingsAction(completion: (() -> Void)? = nil) -> ActionSheetAction? { nil }
func setNetworkActivityIndicatorVisible(_ value: Bool) {}
func runNowOr(whenMainAppIsActive block: @escaping AppActiveBlock) {}
}