WIP: background vibrate & refresh tasks

This commit is contained in:
ryanzhao 2021-10-12 16:43:30 +11:00
parent 9b9a5d7a39
commit c1e5511ed4
4 changed files with 81 additions and 5 deletions

View File

@ -224,6 +224,10 @@ static NSTimeInterval launchStartedAt;
OWSLogInfo(@"application: didFinishLaunchingWithOptions completed.");
[self setUpCallHandling];
if (@available(iOS 13.0, *)) {
[self registerBackgroundTasks];
}
return YES;
}

View File

@ -2,11 +2,13 @@ import PromiseKit
import WebRTC
import SessionUIKit
import UIKit
import BackgroundTasks
import SessionUtilitiesKit
extension AppDelegate {
@objc
func setUpCallHandling() {
// MARK: Call handling
@objc func setUpCallHandling() {
// Offer messages
MessageReceiver.handleOfferCallMessage = { message in
DispatchQueue.main.async {
@ -45,6 +47,7 @@ extension AppDelegate {
}
}
// MARK: Configuration message
@objc(syncConfigurationIfNeeded)
func syncConfigurationIfNeeded() {
guard Storage.shared.getUser()?.name != nil else { return }
@ -75,6 +78,7 @@ extension AppDelegate {
return promise
}
// MARK: Closed group poller
@objc func startClosedGroupPoller() {
guard OWSIdentityManager.shared().identityKeyPair() != nil else { return }
ClosedGroupPoller.shared.start()
@ -84,6 +88,7 @@ extension AppDelegate {
ClosedGroupPoller.shared.stop()
}
// MARK: Theme
@objc func getAppModeOrSystemDefault() -> AppMode {
let userDefaults = UserDefaults.standard
if userDefaults.dictionaryRepresentation().keys.contains("appMode") {
@ -98,4 +103,46 @@ extension AppDelegate {
}
}
// MARK: Background tasks
@available(iOS 13.0, *)
@objc func registerBackgroundTasks() {
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.loki-project.loki-messenger.refresh", using: nil) { task in
self.handleAppRefresh(task: task as! BGAppRefreshTask)
}
BGTaskScheduler.shared.register(forTaskWithIdentifier: "com.loki-project.loki-messenger.vibrate", using: nil) { task in
Vibration.shared.startVibration()
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 60, execute: {
Vibration.shared.stopVibrationIfPossible()
task.setTaskCompleted(success: true)
})
}
}
@available(iOS 13.0, *)
@objc func cancelAllPendingBGTask() {
BGTaskScheduler.shared.cancelAllTaskRequests()
}
@available(iOS 13.0, *)
@objc func scheduleAppRefresh() {
let request = BGAppRefreshTaskRequest(identifier: "com.loki-project.loki-messenger.refresh")
// Fetch no earlier than 15 minutes from now.
request.earliestBeginDate = Date(timeIntervalSinceNow: 15 * 60)
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
@available(iOS 13.0, *)
private func handleAppRefresh(task: BGAppRefreshTask) {
// Schedule a new refresh task.
scheduleAppRefresh()
}
}

View File

@ -2,6 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.loki-project.loki-messenger.vibrate</string>
<string>com.loki-project.loki-messenger.refresh</string>
</array>
<key>BuildDetails</key>
<dict>
<key>CarthageVersion</key>
@ -90,7 +95,7 @@
<key>NSContactsUsageDescription</key>
<string>Signal uses your contacts to find users you know. We do not store your contacts on the server.</string>
<key>NSFaceIDUsageDescription</key>
<string>Session's Screen Lock feature uses Face ID.</string>
<string>Session&apos;s Screen Lock feature uses Face ID.</string>
<key>NSMicrophoneUsageDescription</key>
<string>Session needs access to your microphone to record media.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
@ -119,6 +124,7 @@
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>processing</string>
<string>remote-notification</string>
</array>
<key>UILaunchStoryboardName</key>
@ -135,5 +141,7 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<true/>
<key>NSHumanReadableCopyright</key>
<string>com.loki-project.loki-messenger</string>
</dict>
</plist>

View File

@ -1,4 +1,5 @@
import UserNotifications
import BackgroundTasks
import SessionMessagingKit
import SignalUtilitiesKit
@ -93,7 +94,7 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
notificationContent.badge = 1
notificationContent.title = "Session"
notificationContent.body = "\(senderDisplayName) is calling..."
return self.handleSuccess(for: notificationContent)
return self.handleSuccessForIncomingCall(for: notificationContent)
default: return self.completeSilenty()
}
if (senderPublicKey == userPublicKey) {
@ -119,7 +120,10 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
}
self.handleSuccess(for: notificationContent)
} catch {
self.handleFailure(for: notificationContent)
if let error = error as? MessageReceiver.Error, error.isRetryable {
self.handleFailure(for: notificationContent)
}
self.completeSilenty()
}
}
}
@ -209,6 +213,19 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
private func completeSilenty() {
contentHandler!(.init())
}
private func handleSuccessForIncomingCall(for content: UNMutableNotificationContent) {
// TODO: poll for the real offer, play incoming call ring
if #available(iOSApplicationExtension 13.0, *) {
let request = BGAppRefreshTaskRequest(identifier: "com.loki-project.loki-messenger.refresh")
do {
try BGTaskScheduler.shared.submit(request)
} catch {
print("Could not schedule app refresh: \(error)")
}
}
contentHandler!(content)
}
private func handleSuccess(for content: UNMutableNotificationContent) {
contentHandler!(content)