oncall-mobile-ios/ntfyNSE/NotificationService.swift

101 lines
5 KiB
Swift
Raw Normal View History

2022-05-14 19:01:40 -04:00
import UserNotifications
2022-05-15 20:17:07 -04:00
import CoreData
2022-05-27 20:45:17 -04:00
import CryptoKit
2022-05-14 19:01:40 -04:00
/// This app extension is responsible for persisting the incoming notification to the data store (Core Data). It will eventually be the entity that
/// fetches notification content from selfhosted servers (when a "poll request" is received). This is not implemented yet.
///
/// Note that the app extension does not run as part of the main app, so log messages are not printed in the main Xcode window. To debug,
/// select Debug -> Attach to Process by PID or Name, and select the extension. Don't forget to set a breakpoint, or you're not gonna have a good time.
2022-05-15 20:17:07 -04:00
class NotificationService: UNNotificationServiceExtension {
2022-05-24 22:27:04 -04:00
private let tag = "NotificationService"
2022-05-27 23:07:19 -04:00
private var store: Store?
2022-05-14 19:01:40 -04:00
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
2022-05-20 09:53:10 -04:00
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
2022-05-27 23:07:19 -04:00
self.store = Store.shared
2022-05-14 19:01:40 -04:00
self.contentHandler = contentHandler
2022-05-20 09:53:10 -04:00
self.bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
Log.d(tag, "Notification received (in service)") // Logs from extensions are not printed in Xcode!
2022-05-14 19:01:40 -04:00
if let bestAttemptContent = bestAttemptContent {
2022-05-15 20:17:07 -04:00
let userInfo = bestAttemptContent.userInfo
2022-05-27 20:45:17 -04:00
let baseUrl = userInfo["base_url"] as? String ?? Config.appBaseUrl
2022-05-26 12:56:25 -04:00
guard let message = Message.from(userInfo: userInfo) else {
2022-05-27 23:07:19 -04:00
Log.w(tag, "Message cannot be parsed from userInfo", userInfo)
2022-05-26 12:56:25 -04:00
contentHandler(request.content)
return
}
2022-05-27 23:07:19 -04:00
switch message.event {
case "poll_request":
handlePollRequest(request, bestAttemptContent, message, contentHandler)
case "message":
handleMessage(request, bestAttemptContent, baseUrl, message, contentHandler)
default:
2022-05-26 12:56:25 -04:00
Log.w(tag, "Irrelevant message received", message)
contentHandler(request.content)
2022-05-25 16:59:25 -04:00
}
2022-05-14 19:01:40 -04:00
}
}
override func serviceExtensionTimeWillExpire() {
// Called just before the extension will be terminated by the system.
2022-05-20 09:53:10 -04:00
// Use this as an opportunity to deliver your "best attempt" at modified content,
// otherwise the original push payload will be used.
2022-05-14 19:01:40 -04:00
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
2022-05-27 20:45:17 -04:00
2022-05-27 23:07:19 -04:00
private func handleMessage(_ request: UNNotificationRequest, _ content: UNMutableNotificationContent, _ baseUrl: String, _ message: Message, _ contentHandler: @escaping (UNNotificationContent) -> Void) {
// Modify notification based on message
content.modify(message: message, baseUrl: baseUrl)
2022-05-27 20:45:17 -04:00
2022-05-27 23:07:19 -04:00
// Save notification to store, and display it
guard let subscription = store?.getSubscription(baseUrl: baseUrl, topic: message.topic) else {
Log.w(tag, "Subscription \(topicUrl(baseUrl: baseUrl, topic: message.topic)) unknown")
contentHandler(request.content)
return
}
Store.shared.save(notificationFromMessage: message, withSubscription: subscription)
contentHandler(content)
2022-05-27 20:45:17 -04:00
}
2022-05-27 23:07:19 -04:00
private func handlePollRequest(_ request: UNNotificationRequest, _ content: UNMutableNotificationContent, _ pollRequest: Message, _ contentHandler: @escaping (UNNotificationContent) -> Void) {
let subscription = store?.getSubscriptions()?.first { $0.urlHash() == pollRequest.topic }
let baseUrl = subscription?.baseUrl
guard
let subscription = subscription,
let pollId = pollRequest.pollId,
let baseUrl = baseUrl
else {
Log.w(tag, "Cannot find subscription", pollRequest)
contentHandler(request.content)
return
}
// Poll original server
2022-06-03 22:49:04 -04:00
let user = store?.getUser(baseUrl: baseUrl)?.toBasicUser()
2022-05-27 23:07:19 -04:00
let semaphore = DispatchSemaphore(value: 0)
2022-06-03 22:49:04 -04:00
ApiService.shared.poll(subscription: subscription, messageId: pollId, user: user) { message, error in
2022-05-27 23:07:19 -04:00
guard let message = message else {
Log.w(self.tag, "Error fetching message", error)
contentHandler(request.content)
return
}
self.handleMessage(request, content, baseUrl, message, contentHandler)
semaphore.signal()
}
// Note: If notifications only show up as "New message", it may be because the "return" statement
// happens before the contentHandler() is called. We add this semaphore here to synchronize the threads.
// I don't know if this is necessary, but it feels like the right thing to do.
2022-05-27 20:45:17 -04:00
2022-05-27 23:07:19 -04:00
_ = semaphore.wait(timeout: DispatchTime.now() + 25) // 30 seconds is the max for the entire extension
2022-05-27 20:45:17 -04:00
}
2022-05-14 19:01:40 -04:00
}