// Cotton Messages' notification service extension: a chat push says only// "New message", and this says who and what.//// The push carries the message's sealed box (the object put it there; a// stranger's request carries none). This opens it with the chat key the app// keeps in the shared keychain group (`ChatAlert`) and titles it with the name// the app last wrote into the app group. It reaches no server, lands nothing// and acks nothing: the app's core takes every item when it opens. Whatever it// cannot do (the phone locked since boot, no key, no box) leaves the alert as// it came.//// An opened alert is a COMMUNICATION notification, as a messenger's is: the// sender as an `INPerson` on an `INSendMessageIntent`, donated, and the// content updated from it, so the system draws the sender's name (and// picture, once one is kept where the extension can read it) and lets Focus// see who it is from. It is filed under the conversation: the thread the app// last said for the number (`ChatThreads`), else the number itself (or the// group), and carries `peer` (and `thread` when known) for the app's tap,// Reply and Mark as read (`ChatNoticeNames`).importCottonMailboximportFoundationimportIntentsimportUserNotificationsfinalclassNotificationService: UNNotificationServiceExtension {privatevar content: UNNotificationContent?privatevar deliver: ((UNNotificationContent) -> Void)?overridefunc didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { content = request.content deliver = contentHandlerlet info = request.content.userInfoguard info["chat"] != nil, let key = ChatKeychain().key() else { return finish(nil) } finish(ChatAlert.words(box: info["box"] as? String, privateKey: key, names: ChatNames.read())) }/// The system's last call: the alert as it came.overridefunc serviceExtensionTimeWillExpire() { finish(nil) }/// The alert with the sender and the words when there are some, else as it came; once.privatefunc finish(_ words: ChatAlertWords?) {guardlet deliver, let content else { return }self.deliver = nilguardlet words, let rewritten = content.mutableCopy() as? UNMutableNotificationContent else { return deliver(content) } rewritten.title = words.titleiflet body = words.body { rewritten.body = body }let thread = ChatThreads.read()[words.conversation] rewritten.threadIdentifier = thread ?? words.conversation rewritten.categoryIdentifier = ChatNoticeNames.categoryvar info = rewritten.userInfo info["peer"] = words.fromiflet group = words.group { info["group"] = group }iflet thread { info["thread"] = thread }// The box has been read; it need not sit in Notification Centre. info["box"] = nil rewritten.userInfo = info deliver(Self.communication(rewritten, words: words, conversation: thread ?? words.conversation)) }/// The content as a communication notification from the sender, or as/// it is when the system will not have it (no entitlement on this build).privatestaticfunc communication(_ content: UNMutableNotificationContent, words: ChatAlertWords, conversation: String) -> UNNotificationContent {let sender = INPerson( personHandle: INPersonHandle(value: words.from, type: .phoneNumber), nameComponents: nil, displayName: words.title, image: nil, contactIdentifier: nil, customIdentifier: words.from)let intent = INSendMessageIntent( recipients: nil, outgoingMessageType: .outgoingMessageText, content: words.body, speakableGroupName: nil, conversationIdentifier: conversation, serviceName: nil, sender: sender, attachments: nil)let interaction = INInteraction(intent: intent, response: nil) interaction.direction = .incoming interaction.donate(completion: nil)return (try? content.updating(from: intent)) ?? content }}