oncall-mobile-ios/ntfyNSE/NotificationService.swift

97 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-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":
2022-06-05 19:14:05 -04:00
let baseUrl = userInfo["base_url"] as? String ?? Config.appBaseUrl // messages only come for the main server
2022-05-27 23:07:19 -04:00
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) {
2026-04-09 23:42:23 -04:00
let subscription = store?.getSubscriptions()?.first { subscription in
// Poll requests usually target the hashed topic URL, but tolerate raw topic payloads too
subscription.urlHash() == pollRequest.topic || subscription.topic == pollRequest.topic
}
2022-05-27 23:07:19 -04:00
let baseUrl = subscription?.baseUrl
2026-04-09 23:42:23 -04:00
let pollId = pollRequest.pollId ?? pollRequest.id
2022-05-27 23:07:19 -04:00
guard
let subscription = subscription,
let baseUrl = baseUrl
else {
2026-04-09 23:50:36 -04:00
Log.w(tag, "Cannot find subscription for poll request topic=\(pollRequest.topic), pollId=\(pollRequest.pollId ?? "<nil>")")
2022-05-27 23:07:19 -04:00
contentHandler(request.content)
return
}
// Poll original server
2022-06-03 22:49:04 -04:00
let user = store?.getUser(baseUrl: baseUrl)?.toBasicUser()
// The extension only needs contentHandler to be called from the async callback
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 {
2026-04-09 23:50:36 -04:00
Log.w(self.tag, "Error fetching poll request message topic=\(pollRequest.topic), pollId=\(pollId), subscription=\(subscription.urlString())", error)
2022-05-27 23:07:19 -04:00
contentHandler(request.content)
return
}
self.handleMessage(request, content, baseUrl, message, contentHandler)
}
2022-05-27 20:45:17 -04:00
}
2022-05-14 19:01:40 -04:00
}