2020-06-23 18:11:47 +03:00
|
|
|
//
|
|
|
|
// NotificationService.m
|
|
|
|
// NotificationServiceExtension
|
|
|
|
//
|
|
|
|
// Created by Ivan Sein on 23.06.20.
|
|
|
|
// Copyright © 2020 struktur AG. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "NotificationService.h"
|
|
|
|
|
2020-07-01 10:32:01 +03:00
|
|
|
#import "NCAPIController.h"
|
2020-06-24 19:11:02 +03:00
|
|
|
#import "NCDatabaseManager.h"
|
2020-07-01 10:32:01 +03:00
|
|
|
#import "NCNotification.h"
|
2020-06-24 19:11:02 +03:00
|
|
|
#import "NCPushNotification.h"
|
|
|
|
#import "NCSettingsController.h"
|
|
|
|
|
2020-06-23 18:11:47 +03:00
|
|
|
@interface NotificationService ()
|
|
|
|
|
|
|
|
@property (nonatomic, strong) void (^contentHandler)(UNNotificationContent *contentToDeliver);
|
|
|
|
@property (nonatomic, strong) UNMutableNotificationContent *bestAttemptContent;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NotificationService
|
|
|
|
|
|
|
|
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
|
|
|
|
self.contentHandler = contentHandler;
|
|
|
|
self.bestAttemptContent = [request.content mutableCopy];
|
|
|
|
|
2020-06-24 19:11:02 +03:00
|
|
|
self.bestAttemptContent.title = @"";
|
2020-10-13 20:33:09 +03:00
|
|
|
self.bestAttemptContent.body = NSLocalizedString(@"You received a new notification", nil);
|
2020-06-24 19:11:02 +03:00
|
|
|
|
|
|
|
// Configure database
|
|
|
|
NSString *path = [[[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.com.nextcloud.Talk"] URLByAppendingPathComponent:kTalkDatabaseFolder] path];
|
|
|
|
RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
|
|
|
|
NSURL *databaseURL = [[NSURL fileURLWithPath:path] URLByAppendingPathComponent:kTalkDatabaseFileName];
|
|
|
|
configuration.fileURL = databaseURL;
|
|
|
|
configuration.schemaVersion= kTalkDatabaseSchemaVersion;
|
|
|
|
configuration.objectClasses = @[TalkAccount.class];
|
|
|
|
NSError *error = nil;
|
|
|
|
RLMRealm *realm = [RLMRealm realmWithConfiguration:configuration error:&error];
|
|
|
|
|
|
|
|
// Decrypt message
|
|
|
|
NSString *message = [self.bestAttemptContent.userInfo objectForKey:@"subject"];
|
|
|
|
for (TalkAccount *talkAccount in [TalkAccount allObjectsInRealm:realm]) {
|
|
|
|
TalkAccount *account = [[TalkAccount alloc] initWithValue:talkAccount];
|
|
|
|
NSData *pushNotificationPrivateKey = [[NCSettingsController sharedInstance] pushNotificationPrivateKeyForAccountId:account.accountId];
|
|
|
|
if (message && pushNotificationPrivateKey) {
|
|
|
|
@try {
|
|
|
|
NSString *decryptedMessage = [[NCSettingsController sharedInstance] decryptPushNotification:message withDevicePrivateKey:pushNotificationPrivateKey];
|
|
|
|
if (decryptedMessage) {
|
|
|
|
NCPushNotification *pushNotification = [NCPushNotification pushNotificationFromDecryptedString:decryptedMessage withAccountId:account.accountId];
|
2020-07-02 15:46:43 +03:00
|
|
|
|
|
|
|
// Update unread notifications counter for push notification account
|
|
|
|
[realm beginWriteTransaction];
|
|
|
|
NSPredicate *query = [NSPredicate predicateWithFormat:@"accountId = %@", account.accountId];
|
|
|
|
TalkAccount *managedAccount = [TalkAccount objectsWithPredicate:query].firstObject;
|
|
|
|
managedAccount.unreadBadgeNumber += 1;
|
|
|
|
managedAccount.unreadNotification = (managedAccount.active) ? NO : YES;
|
|
|
|
[realm commitWriteTransaction];
|
|
|
|
|
|
|
|
// Get the total number of unread notifications
|
|
|
|
NSInteger unreadNotifications = 0;
|
|
|
|
for (TalkAccount *user in [TalkAccount allObjectsInRealm:realm]) {
|
|
|
|
unreadNotifications += user.unreadBadgeNumber;
|
|
|
|
}
|
|
|
|
|
2020-07-01 10:34:47 +03:00
|
|
|
self.bestAttemptContent.body = pushNotification.bodyForRemoteAlerts;
|
2020-07-01 10:48:06 +03:00
|
|
|
self.bestAttemptContent.threadIdentifier = pushNotification.roomToken;
|
|
|
|
self.bestAttemptContent.sound = [UNNotificationSound defaultSound];
|
2020-07-02 15:46:43 +03:00
|
|
|
self.bestAttemptContent.badge = @(unreadNotifications);
|
2020-07-01 10:48:06 +03:00
|
|
|
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
|
|
|
|
[userInfo setObject:pushNotification.jsonString forKey:@"pushNotification"];
|
|
|
|
[userInfo setObject:pushNotification.accountId forKey:@"accountId"];
|
|
|
|
self.bestAttemptContent.userInfo = userInfo;
|
2020-06-26 19:24:38 +03:00
|
|
|
// Create title and body structure if there is a new line in the subject
|
|
|
|
NSArray* components = [pushNotification.subject componentsSeparatedByString:@"\n"];
|
|
|
|
if (components.count > 1) {
|
|
|
|
NSString *title = [components objectAtIndex:0];
|
|
|
|
NSMutableArray *mutableComponents = [[NSMutableArray alloc] initWithArray:components];
|
|
|
|
[mutableComponents removeObjectAtIndex:0];
|
|
|
|
NSString *body = [mutableComponents componentsJoinedByString:@"\n"];
|
|
|
|
self.bestAttemptContent.title = title;
|
|
|
|
self.bestAttemptContent.body = body;
|
|
|
|
}
|
2020-07-01 10:32:01 +03:00
|
|
|
// Try to get the notification from the server
|
|
|
|
[[NCAPIController sharedInstance] getServerNotification:pushNotification.notificationId forAccount:account withCompletionBlock:^(NSDictionary *notification, NSError *error, NSInteger statusCode) {
|
|
|
|
if (!error) {
|
|
|
|
NCNotification *serverNotification = [NCNotification notificationWithDictionary:notification];
|
|
|
|
if (serverNotification && serverNotification.notificationType == kNCNotificationTypeChat) {
|
|
|
|
self.bestAttemptContent.title = serverNotification.chatMessageTitle;
|
|
|
|
self.bestAttemptContent.body = serverNotification.message;
|
|
|
|
if (@available(iOS 12.0, *)) {
|
|
|
|
self.bestAttemptContent.summaryArgument = serverNotification.chatMessageAuthor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.contentHandler(self.bestAttemptContent);
|
|
|
|
}];
|
2020-06-24 19:11:02 +03:00
|
|
|
}
|
|
|
|
} @catch (NSException *exception) {
|
|
|
|
continue;
|
|
|
|
NSLog(@"An error ocurred decrypting the message. %@", exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-23 18:11:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)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.
|
2020-06-24 19:11:02 +03:00
|
|
|
self.bestAttemptContent.title = @"";
|
2020-10-13 20:33:09 +03:00
|
|
|
self.bestAttemptContent.body = NSLocalizedString(@"You received a new notification", nil);
|
2020-06-24 19:11:02 +03:00
|
|
|
|
2020-06-23 18:11:47 +03:00
|
|
|
self.contentHandler(self.bestAttemptContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|