|
|
|
@ -1,157 +1,74 @@
|
|
|
|
|
import UserNotifications
|
|
|
|
|
import SessionMessagingKit
|
|
|
|
|
import SignalUtilitiesKit
|
|
|
|
|
|
|
|
|
|
final class NotificationServiceExtension : UNNotificationServiceExtension {
|
|
|
|
|
static let isFromRemoteKey = "remote"
|
|
|
|
|
static let threadIdKey = "Signal.AppNotificationsUserInfoKey.threadId"
|
|
|
|
|
// TODO: Group notifications
|
|
|
|
|
|
|
|
|
|
final class NotificationServiceExtension : UNNotificationServiceExtension {
|
|
|
|
|
private var didPerformSetup = false
|
|
|
|
|
private var areVersionMigrationsComplete = false
|
|
|
|
|
private var contentHandler: ((UNNotificationContent) -> Void)?
|
|
|
|
|
private var notificationContent: UNMutableNotificationContent?
|
|
|
|
|
|
|
|
|
|
var areVersionMigrationsComplete = false
|
|
|
|
|
var contentHandler: ((UNNotificationContent) -> Void)?
|
|
|
|
|
var notificationContent: UNMutableNotificationContent?
|
|
|
|
|
private static let isFromRemoteKey = "remote"
|
|
|
|
|
private static let threadIdKey = "Signal.AppNotificationsUserInfoKey.threadId"
|
|
|
|
|
|
|
|
|
|
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
|
|
|
|
|
self.contentHandler = contentHandler
|
|
|
|
|
notificationContent = request.content.mutableCopy() as? UNMutableNotificationContent
|
|
|
|
|
|
|
|
|
|
self.notificationContent = request.content.mutableCopy() as? UNMutableNotificationContent
|
|
|
|
|
|
|
|
|
|
// Abort if the main app is running
|
|
|
|
|
var isMainAppActive = false
|
|
|
|
|
if let sharedUserDefaults = UserDefaults(suiteName: "group.com.loki-project.loki-messenger") {
|
|
|
|
|
isMainAppActive = sharedUserDefaults.bool(forKey: "isMainAppActive")
|
|
|
|
|
}
|
|
|
|
|
// If the main app is running, skip the whole process
|
|
|
|
|
guard !isMainAppActive else { return self.completeWithFailure(content: notificationContent!) }
|
|
|
|
|
|
|
|
|
|
// The code using DispatchQueue.main.async { self.setUpIfNecessary() { Modify the notification content } } will somehow cause a freeze when a second PN comes
|
|
|
|
|
|
|
|
|
|
DispatchQueue.main.sync { self.setUpIfNecessary() {} }
|
|
|
|
|
|
|
|
|
|
guard !isMainAppActive else { return self.handleFailure(for: notificationContent!) }
|
|
|
|
|
|
|
|
|
|
// Perform main setup
|
|
|
|
|
DispatchQueue.main.sync { self.setUpIfNecessary() { } }
|
|
|
|
|
|
|
|
|
|
// Handle the push notification
|
|
|
|
|
AppReadiness.runNowOrWhenAppDidBecomeReady {
|
|
|
|
|
if let notificationContent = self.notificationContent {
|
|
|
|
|
// Modify the notification content here...
|
|
|
|
|
let base64EncodedData = notificationContent.userInfo["ENCRYPTED_DATA"] as! String
|
|
|
|
|
let data = Data(base64Encoded: base64EncodedData)!
|
|
|
|
|
if let envelope = try? MessageWrapper.unwrap(data: data), let data = try? envelope.serializedData() {
|
|
|
|
|
// TODO TODO TODO
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
decrypter.decryptEnvelope(envelope,
|
|
|
|
|
envelopeData: data,
|
|
|
|
|
successBlock: { result, transaction in
|
|
|
|
|
if let envelope = try? SNProtoEnvelope.parseData(result.envelopeData) {
|
|
|
|
|
messageManager.throws_processEnvelope(envelope, plaintextData: result.plaintextData, wasReceivedByUD: wasReceivedByUD, transaction: transaction, serverID: 0)
|
|
|
|
|
self.handleDecryptionResult(result: result, notificationContent: notificationContent, transaction: transaction)
|
|
|
|
|
} else {
|
|
|
|
|
self.completeWithFailure(content: notificationContent)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
failureBlock: {
|
|
|
|
|
self.completeWithFailure(content: notificationContent)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
*/
|
|
|
|
|
} else {
|
|
|
|
|
self.completeWithFailure(content: notificationContent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
func handleDecryptionResult(result: OWSMessageDecryptResult, notificationContent: UNMutableNotificationContent, transaction: YapDatabaseReadWriteTransaction) {
|
|
|
|
|
let contentProto = try? SNProtoContent.parseData(result.plaintextData!)
|
|
|
|
|
var thread: TSThread
|
|
|
|
|
var newNotificationBody = ""
|
|
|
|
|
let masterPublicKey = OWSPrimaryStorage.shared().getMasterHexEncodedPublicKey(for: result.source, in: transaction) ?? result.source
|
|
|
|
|
var displayName = OWSUserProfile.fetch(uniqueId: masterPublicKey, transaction: transaction)?.profileName ?? SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: masterPublicKey)
|
|
|
|
|
if let groupID = contentProto?.dataMessage?.group?.id {
|
|
|
|
|
thread = TSGroupThread.getOrCreateThread(withGroupId: groupID, groupType: .closedGroup, transaction: transaction)
|
|
|
|
|
var groupName = thread.name()
|
|
|
|
|
if groupName.count < 1 {
|
|
|
|
|
groupName = MessageStrings.newGroupDefaultTitle
|
|
|
|
|
}
|
|
|
|
|
let senderName = OWSUserProfile.fetch(uniqueId: masterPublicKey, transaction: transaction)?.profileName ?? SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: masterPublicKey)
|
|
|
|
|
displayName = String(format: NotificationStrings.incomingGroupMessageTitleFormat, senderName, groupName)
|
|
|
|
|
let group: SNProtoGroupContext = contentProto!.dataMessage!.group!
|
|
|
|
|
let oldGroupModel = (thread as! TSGroupThread).groupModel
|
|
|
|
|
let removedMembers = NSMutableSet(array: oldGroupModel.groupMemberIds)
|
|
|
|
|
let newGroupModel = TSGroupModel.init(title: group.name,
|
|
|
|
|
memberIds:group.members,
|
|
|
|
|
image: oldGroupModel.groupImage,
|
|
|
|
|
groupId: group.id,
|
|
|
|
|
groupType: oldGroupModel.groupType,
|
|
|
|
|
adminIds: group.admins)
|
|
|
|
|
removedMembers.minus(Set(newGroupModel.groupMemberIds))
|
|
|
|
|
newGroupModel.removedMembers = removedMembers
|
|
|
|
|
switch contentProto?.dataMessage?.group?.type {
|
|
|
|
|
case .update:
|
|
|
|
|
newNotificationBody = oldGroupModel.getInfoStringAboutUpdate(to: newGroupModel, contactsManager: SSKEnvironment.shared.contactsManager)
|
|
|
|
|
break
|
|
|
|
|
case .quit:
|
|
|
|
|
let nameString = SSKEnvironment.shared.contactsManager.displayName(forPhoneIdentifier: masterPublicKey, transaction: transaction)
|
|
|
|
|
newNotificationBody = NSLocalizedString("GROUP_MEMBER_LEFT", comment: nameString)
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
thread = TSContactThread.getOrCreateThread(withContactId: result.source, transaction: transaction)
|
|
|
|
|
}
|
|
|
|
|
let userInfo: [String:Any] = [ NotificationServiceExtension.threadIdKey : thread.uniqueId!, NotificationServiceExtension.isFromRemoteKey : true ]
|
|
|
|
|
notificationContent.title = displayName
|
|
|
|
|
notificationContent.userInfo = userInfo
|
|
|
|
|
notificationContent.badge = 1
|
|
|
|
|
if let attachment = contentProto?.dataMessage?.attachments.last {
|
|
|
|
|
newNotificationBody = TSAttachment.emoji(forMimeType: attachment.contentType!) + "Attachment"
|
|
|
|
|
if let rawMessageBody = contentProto?.dataMessage?.body, rawMessageBody.count > 0 {
|
|
|
|
|
newNotificationBody += ": \(rawMessageBody)"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if newNotificationBody.count < 1 {
|
|
|
|
|
newNotificationBody = contentProto?.dataMessage?.body ?? "You've got a new message"
|
|
|
|
|
}
|
|
|
|
|
newNotificationBody = handleMentionIfNecessary(rawMessageBody: newNotificationBody, threadID: thread.uniqueId!, transaction: transaction)
|
|
|
|
|
|
|
|
|
|
let notificationPreference = Environment.shared.preferences
|
|
|
|
|
if let notificationType = notificationPreference?.notificationPreviewType() {
|
|
|
|
|
switch notificationType {
|
|
|
|
|
case .nameNoPreview:
|
|
|
|
|
notificationContent.body = "New Message"
|
|
|
|
|
case .noNameNoPreview:
|
|
|
|
|
notificationContent.title = ""
|
|
|
|
|
notificationContent.body = "New Message"
|
|
|
|
|
default:
|
|
|
|
|
notificationContent.body = newNotificationBody
|
|
|
|
|
let notificationContent = self.notificationContent!
|
|
|
|
|
guard let base64EncodedData = notificationContent.userInfo["ENCRYPTED_DATA"] as! String?, let data = Data(base64Encoded: base64EncodedData),
|
|
|
|
|
let envelope = try? MessageWrapper.unwrap(data: data), let envelopeAsData = try? envelope.serializedData() else {
|
|
|
|
|
return self.handleFailure(for: notificationContent)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
notificationContent.body = newNotificationBody
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if notificationContent.body.count < 1 {
|
|
|
|
|
self.completeWithFailure(content: notificationContent)
|
|
|
|
|
} else {
|
|
|
|
|
self.contentHandler!(notificationContent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
func handleMentionIfNecessary(rawMessageBody: String, threadID: String, transaction: YapDatabaseReadWriteTransaction) -> String {
|
|
|
|
|
var string = rawMessageBody
|
|
|
|
|
let regex = try! NSRegularExpression(pattern: "@[0-9a-fA-F]*", options: [])
|
|
|
|
|
var outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: string.utf16.count))
|
|
|
|
|
while let match = outerMatch {
|
|
|
|
|
let publicKey = String((string as NSString).substring(with: match.range).dropFirst()) // Drop the @
|
|
|
|
|
let matchEnd: Int
|
|
|
|
|
let displayName: String? = OWSProfileManager.shared().profileNameForRecipient(withID: publicKey, transaction: transaction)
|
|
|
|
|
if let displayName = displayName {
|
|
|
|
|
string = (string as NSString).replacingCharacters(in: match.range, with: "@\(displayName)")
|
|
|
|
|
matchEnd = match.range.location + displayName.utf16.count
|
|
|
|
|
} else {
|
|
|
|
|
matchEnd = match.range.location + match.range.length
|
|
|
|
|
Storage.write { transaction in // Intentionally capture self
|
|
|
|
|
do {
|
|
|
|
|
let (message, proto) = try MessageReceiver.parse(envelopeAsData, openGroupMessageServerID: nil, using: transaction)
|
|
|
|
|
guard let visibleMessage = message as? VisibleMessage else {
|
|
|
|
|
return self.handleFailure(for: notificationContent)
|
|
|
|
|
}
|
|
|
|
|
let tsIncomingMessageID = try MessageReceiver.handleVisibleMessage(visibleMessage, associatedWithProto: proto, openGroupID: nil, using: transaction)
|
|
|
|
|
guard let tsIncomingMessage = TSIncomingMessage.fetch(uniqueId: tsIncomingMessageID, transaction: transaction) else {
|
|
|
|
|
return self.handleFailure(for: notificationContent)
|
|
|
|
|
}
|
|
|
|
|
let snippet = tsIncomingMessage.previewText(with: transaction).filterForDisplay
|
|
|
|
|
let userInfo: [String:Any] = [ NotificationServiceExtension.threadIdKey : tsIncomingMessage.thread.uniqueId!, NotificationServiceExtension.isFromRemoteKey : true ]
|
|
|
|
|
let senderPublicKey = message.sender!
|
|
|
|
|
let senderDisplayName = OWSProfileManager.shared().profileNameForRecipient(withID: senderPublicKey, avoidWriteTransaction: true) ?? senderPublicKey
|
|
|
|
|
notificationContent.userInfo = userInfo
|
|
|
|
|
notificationContent.badge = 1
|
|
|
|
|
let notificationsPreference = Environment.shared.preferences!.notificationPreviewType()
|
|
|
|
|
switch notificationsPreference {
|
|
|
|
|
case .namePreview:
|
|
|
|
|
notificationContent.title = senderDisplayName
|
|
|
|
|
notificationContent.body = snippet!
|
|
|
|
|
case .nameNoPreview:
|
|
|
|
|
notificationContent.title = senderDisplayName
|
|
|
|
|
notificationContent.body = "New Message"
|
|
|
|
|
case .noNameNoPreview:
|
|
|
|
|
notificationContent.title = "Session"
|
|
|
|
|
notificationContent.body = "New Message"
|
|
|
|
|
default: break
|
|
|
|
|
}
|
|
|
|
|
self.handleSuccess(for: notificationContent)
|
|
|
|
|
} catch {
|
|
|
|
|
self.handleFailure(for: notificationContent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: matchEnd, length: string.utf16.count - matchEnd))
|
|
|
|
|
}
|
|
|
|
|
return string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setUpIfNecessary(completion: @escaping () -> Void) {
|
|
|
|
@ -193,22 +110,19 @@ final class NotificationServiceExtension : UNNotificationServiceExtension {
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self,
|
|
|
|
|
selector: #selector(storageIsReady),
|
|
|
|
|
name: .StorageIsReady,
|
|
|
|
|
object: nil)
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(storageIsReady), name: .StorageIsReady, object: nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func serviceExtensionTimeWillExpire() {
|
|
|
|
|
// Called just before the extension will be terminated by the system.
|
|
|
|
|
// Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used.
|
|
|
|
|
if let contentHandler = contentHandler, let notificationContent = notificationContent {
|
|
|
|
|
contentHandler(notificationContent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func wasReceivedByUD(envelope: SNProtoEnvelope) -> Bool {
|
|
|
|
|
return (envelope.type == .unidentifiedSender && (!envelope.hasSource || envelope.source!.count < 1))
|
|
|
|
|
let userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ]
|
|
|
|
|
let notificationContent = self.notificationContent!
|
|
|
|
|
notificationContent.userInfo = userInfo
|
|
|
|
|
notificationContent.badge = 1
|
|
|
|
|
notificationContent.title = "Session"
|
|
|
|
|
notificationContent.body = "New Message"
|
|
|
|
|
handleSuccess(for: notificationContent)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc
|
|
|
|
@ -242,14 +156,18 @@ final class NotificationServiceExtension : UNNotificationServiceExtension {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func completeSilenty() {
|
|
|
|
|
contentHandler?(.init())
|
|
|
|
|
contentHandler!(.init())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func completeWithFailure(content: UNMutableNotificationContent) {
|
|
|
|
|
content.body = "You've got a new message"
|
|
|
|
|
|
|
|
|
|
func handleSuccess(for content: UNMutableNotificationContent) {
|
|
|
|
|
contentHandler!(content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleFailure(for content: UNMutableNotificationContent) {
|
|
|
|
|
content.body = "New Message"
|
|
|
|
|
content.title = "Session"
|
|
|
|
|
let userInfo: [String:Any] = [NotificationServiceExtension.isFromRemoteKey : true]
|
|
|
|
|
let userInfo: [String:Any] = [ NotificationServiceExtension.isFromRemoteKey : true ]
|
|
|
|
|
content.userInfo = userInfo
|
|
|
|
|
contentHandler?(content)
|
|
|
|
|
contentHandler!(content)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|