2015-12-22 12:45:09 +01:00
|
|
|
//
|
2017-02-02 23:28:41 +01:00
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
2015-12-22 12:45:09 +01:00
|
|
|
//
|
|
|
|
|
2016-10-10 22:02:09 +02:00
|
|
|
#import "NotificationsManager.h"
|
|
|
|
#import "Environment.h"
|
2016-11-12 18:22:29 +01:00
|
|
|
#import "OWSContactsManager.h"
|
2017-09-14 21:45:04 +02:00
|
|
|
#import "OWSPreferences.h"
|
2016-10-10 22:02:09 +02:00
|
|
|
#import "PushManager.h"
|
2016-11-12 18:22:29 +01:00
|
|
|
#import "Signal-Swift.h"
|
2015-12-22 12:45:09 +01:00
|
|
|
#import <AudioToolbox/AudioServices.h>
|
2016-04-08 09:47:41 +02:00
|
|
|
#import <SignalServiceKit/TSCall.h>
|
|
|
|
#import <SignalServiceKit/TSContactThread.h>
|
|
|
|
#import <SignalServiceKit/TSErrorMessage.h>
|
|
|
|
#import <SignalServiceKit/TSIncomingMessage.h>
|
|
|
|
#import <SignalServiceKit/TextSecureKitEnv.h>
|
2017-05-17 21:45:00 +02:00
|
|
|
#import <SignalServiceKit/Threading.h>
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-07-05 18:45:31 +02:00
|
|
|
NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
@interface NotificationsManager ()
|
|
|
|
|
2017-04-01 05:25:30 +02:00
|
|
|
@property (nonatomic) SystemSoundID newMessageSound;
|
2017-02-02 23:28:41 +01:00
|
|
|
@property (nonatomic, readonly) NSMutableDictionary<NSString *, UILocalNotification *> *currentNotifications;
|
2016-11-12 18:22:29 +01:00
|
|
|
@property (nonatomic, readonly) NotificationType notificationPreviewType;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-06-12 23:13:34 +02:00
|
|
|
@property (nonatomic, readonly) NSMutableArray<NSDate *> *notificationHistory;
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
@end
|
|
|
|
|
2017-04-01 05:25:30 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
@implementation NotificationsManager
|
|
|
|
|
2016-07-19 18:12:18 +02:00
|
|
|
- (instancetype)init
|
|
|
|
{
|
2015-12-22 12:45:09 +01:00
|
|
|
self = [super init];
|
2016-07-19 18:12:18 +02:00
|
|
|
if (!self) {
|
|
|
|
return self;
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
_currentNotifications = [NSMutableDictionary new];
|
2016-09-11 22:53:12 +02:00
|
|
|
|
2016-07-19 18:12:18 +02:00
|
|
|
NSURL *newMessageURL = [[NSBundle mainBundle] URLForResource:@"NewMessage" withExtension:@"aifc"];
|
|
|
|
AudioServicesCreateSystemSoundID((__bridge CFURLRef)newMessageURL, &_newMessageSound);
|
|
|
|
|
2017-06-12 23:13:34 +02:00
|
|
|
_notificationHistory = [NSMutableArray new];
|
|
|
|
|
2017-04-01 00:45:51 +02:00
|
|
|
OWSSingletonAssert();
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
#pragma mark - Signal Calls
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify user for incoming WebRTC Call
|
|
|
|
*/
|
|
|
|
- (void)presentIncomingCall:(SignalCall *)call callerName:(NSString *)callerName
|
|
|
|
{
|
|
|
|
DDLogDebug(@"%@ incoming call from: %@", self.tag, call.remotePhoneNumber);
|
|
|
|
|
|
|
|
UILocalNotification *notification = [UILocalNotification new];
|
|
|
|
notification.category = PushManagerCategoriesIncomingCall;
|
2017-01-18 15:50:22 +01:00
|
|
|
// Rather than using notification sounds, we control the ringtone and repeat vibrations with the CallAudioManager.
|
2017-07-03 15:42:30 +02:00
|
|
|
notification.soundName = @"r.caf";
|
2016-11-12 18:22:29 +01:00
|
|
|
NSString *localCallId = call.localId.UUIDString;
|
|
|
|
notification.userInfo = @{ PushManagerUserInfoKeysLocalCallId : localCallId };
|
|
|
|
|
|
|
|
NSString *alertMessage;
|
|
|
|
switch (self.notificationPreviewType) {
|
|
|
|
case NotificationNoNameNoPreview: {
|
|
|
|
alertMessage = NSLocalizedString(@"INCOMING_CALL", @"notification body");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NotificationNameNoPreview:
|
|
|
|
case NotificationNamePreview: {
|
|
|
|
alertMessage =
|
|
|
|
[NSString stringWithFormat:NSLocalizedString(@"INCOMING_CALL_FROM", @"notification body"), callerName];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notification.alertBody = [NSString stringWithFormat:@"☎️ %@", alertMessage];
|
|
|
|
|
|
|
|
[self presentNotification:notification identifier:localCallId];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify user for missed WebRTC Call
|
|
|
|
*/
|
|
|
|
- (void)presentMissedCall:(SignalCall *)call callerName:(NSString *)callerName
|
|
|
|
{
|
|
|
|
UILocalNotification *notification = [UILocalNotification new];
|
|
|
|
notification.category = PushManagerCategoriesMissedCall;
|
|
|
|
NSString *localCallId = call.localId.UUIDString;
|
|
|
|
notification.userInfo = @{
|
|
|
|
PushManagerUserInfoKeysLocalCallId : localCallId,
|
|
|
|
PushManagerUserInfoKeysCallBackSignalRecipientId : call.remotePhoneNumber
|
|
|
|
};
|
|
|
|
|
2017-07-01 19:29:15 +02:00
|
|
|
if ([self shouldPlaySoundForNotification]) {
|
2017-07-05 18:45:31 +02:00
|
|
|
notification.soundName = kNotificationsManagerNewMesssageSoundName;
|
2017-07-01 19:29:15 +02:00
|
|
|
}
|
2016-11-12 18:22:29 +01:00
|
|
|
|
|
|
|
NSString *alertMessage;
|
|
|
|
switch (self.notificationPreviewType) {
|
|
|
|
case NotificationNoNameNoPreview: {
|
2017-06-07 00:59:38 +02:00
|
|
|
alertMessage = [CallStrings missedCallNotificationBodyWithoutCallerName];
|
2016-11-12 18:22:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NotificationNameNoPreview:
|
|
|
|
case NotificationNamePreview: {
|
2017-03-26 15:13:06 +02:00
|
|
|
alertMessage = (([UIDevice currentDevice].supportsCallKit &&
|
|
|
|
[[Environment getCurrent].preferences isCallKitPrivacyEnabled])
|
2017-03-21 18:21:31 +01:00
|
|
|
? [CallStrings missedCallNotificationBodyWithoutCallerName]
|
|
|
|
: [NSString stringWithFormat:[CallStrings missedCallNotificationBodyWithCallerName], callerName]);
|
2016-11-12 18:22:29 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notification.alertBody = [NSString stringWithFormat:@"☎️ %@", alertMessage];
|
2017-05-31 17:49:30 +02:00
|
|
|
|
|
|
|
[self presentNotification:notification identifier:localCallId];
|
|
|
|
}
|
|
|
|
|
2017-06-07 00:59:38 +02:00
|
|
|
|
|
|
|
- (void)presentMissedCallBecauseOfNewIdentity:(SignalCall *)call callerName:(NSString *)callerName
|
|
|
|
{
|
|
|
|
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:call.remotePhoneNumber];
|
|
|
|
OWSAssert(thread != nil);
|
|
|
|
|
|
|
|
UILocalNotification *notification = [UILocalNotification new];
|
|
|
|
// Use category which allows call back
|
|
|
|
notification.category = PushManagerCategoriesMissedCall;
|
|
|
|
NSString *localCallId = call.localId.UUIDString;
|
|
|
|
notification.userInfo = @{
|
|
|
|
PushManagerUserInfoKeysLocalCallId : localCallId,
|
|
|
|
PushManagerUserInfoKeysCallBackSignalRecipientId : call.remotePhoneNumber,
|
|
|
|
Signal_Thread_UserInfo_Key : thread.uniqueId
|
|
|
|
};
|
2017-07-01 19:29:15 +02:00
|
|
|
if ([self shouldPlaySoundForNotification]) {
|
2017-07-05 18:45:31 +02:00
|
|
|
notification.soundName = kNotificationsManagerNewMesssageSoundName;
|
2017-07-01 19:29:15 +02:00
|
|
|
}
|
2017-06-07 00:59:38 +02:00
|
|
|
|
|
|
|
NSString *alertMessage;
|
|
|
|
switch (self.notificationPreviewType) {
|
|
|
|
case NotificationNoNameNoPreview: {
|
|
|
|
alertMessage = [CallStrings missedCallWithIdentityChangeNotificationBodyWithoutCallerName];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NotificationNameNoPreview:
|
|
|
|
case NotificationNamePreview: {
|
|
|
|
alertMessage = (([UIDevice currentDevice].supportsCallKit &&
|
|
|
|
[[Environment getCurrent].preferences isCallKitPrivacyEnabled])
|
|
|
|
? [CallStrings missedCallWithIdentityChangeNotificationBodyWithoutCallerName]
|
|
|
|
: [NSString
|
|
|
|
stringWithFormat:[CallStrings missedCallWithIdentityChangeNotificationBodyWithCallerName],
|
|
|
|
callerName]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notification.alertBody = [NSString stringWithFormat:@"☎️ %@", alertMessage];
|
|
|
|
|
|
|
|
[self presentNotification:notification identifier:localCallId];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)presentMissedCallBecauseOfNoLongerVerifiedIdentity:(SignalCall *)call callerName:(NSString *)callerName
|
2017-05-31 17:49:30 +02:00
|
|
|
{
|
|
|
|
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:call.remotePhoneNumber];
|
|
|
|
OWSAssert(thread != nil);
|
|
|
|
|
|
|
|
UILocalNotification *notification = [UILocalNotification new];
|
2017-06-07 00:59:38 +02:00
|
|
|
// Use category which does not allow call back
|
|
|
|
notification.category = PushManagerCategoriesMissedCallFromNoLongerVerifiedIdentity;
|
2017-05-31 17:49:30 +02:00
|
|
|
NSString *localCallId = call.localId.UUIDString;
|
|
|
|
notification.userInfo = @{
|
|
|
|
PushManagerUserInfoKeysLocalCallId : localCallId,
|
|
|
|
PushManagerUserInfoKeysCallBackSignalRecipientId : call.remotePhoneNumber,
|
|
|
|
Signal_Thread_UserInfo_Key : thread.uniqueId
|
|
|
|
};
|
2017-07-01 19:29:15 +02:00
|
|
|
if ([self shouldPlaySoundForNotification]) {
|
2017-07-05 18:45:31 +02:00
|
|
|
notification.soundName = kNotificationsManagerNewMesssageSoundName;
|
2017-07-01 19:29:15 +02:00
|
|
|
}
|
2017-05-31 17:49:30 +02:00
|
|
|
|
|
|
|
NSString *alertMessage;
|
|
|
|
switch (self.notificationPreviewType) {
|
|
|
|
case NotificationNoNameNoPreview: {
|
2017-06-07 00:59:38 +02:00
|
|
|
alertMessage = [CallStrings missedCallWithIdentityChangeNotificationBodyWithoutCallerName];
|
2017-05-31 17:49:30 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NotificationNameNoPreview:
|
|
|
|
case NotificationNamePreview: {
|
|
|
|
alertMessage = (([UIDevice currentDevice].supportsCallKit &&
|
|
|
|
[[Environment getCurrent].preferences isCallKitPrivacyEnabled])
|
2017-06-07 00:59:38 +02:00
|
|
|
? [CallStrings missedCallWithIdentityChangeNotificationBodyWithoutCallerName]
|
2017-05-31 17:49:30 +02:00
|
|
|
: [NSString
|
2017-06-07 00:59:38 +02:00
|
|
|
stringWithFormat:[CallStrings missedCallWithIdentityChangeNotificationBodyWithCallerName],
|
2017-05-31 17:49:30 +02:00
|
|
|
callerName]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
notification.alertBody = [NSString stringWithFormat:@"☎️ %@", alertMessage];
|
2016-11-12 18:22:29 +01:00
|
|
|
|
|
|
|
[self presentNotification:notification identifier:localCallId];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Signal Messages
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
- (void)notifyUserForErrorMessage:(TSErrorMessage *)message inThread:(TSThread *)thread {
|
2017-06-02 21:49:34 +02:00
|
|
|
OWSAssert(message);
|
|
|
|
OWSAssert(thread);
|
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
if (thread.isMuted) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-12 23:13:34 +02:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
BOOL shouldPlaySound = [self shouldPlaySoundForNotification];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
NSString *messageDescription = message.description;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
if (([UIApplication sharedApplication].applicationState != UIApplicationStateActive) && messageDescription) {
|
|
|
|
UILocalNotification *notification = [[UILocalNotification alloc] init];
|
|
|
|
notification.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId };
|
|
|
|
if (shouldPlaySound) {
|
|
|
|
notification.soundName = kNotificationsManagerNewMesssageSoundName;
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
NSString *alertBodyString = @"";
|
|
|
|
|
|
|
|
NSString *authorName = [thread name];
|
|
|
|
switch (self.notificationPreviewType) {
|
|
|
|
case NotificationNamePreview:
|
|
|
|
case NotificationNameNoPreview:
|
|
|
|
alertBodyString = [NSString stringWithFormat:@"%@: %@", authorName, messageDescription];
|
|
|
|
break;
|
|
|
|
case NotificationNoNameNoPreview:
|
|
|
|
alertBodyString = messageDescription;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
notification.alertBody = alertBodyString;
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
[[PushManager sharedManager] presentNotification:notification checkForCancel:NO];
|
|
|
|
} else {
|
|
|
|
if (shouldPlaySound && [Environment.preferences soundInForeground]) {
|
|
|
|
AudioServicesPlayAlertSound(_newMessageSound);
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
2017-09-28 16:01:17 +02:00
|
|
|
});
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
- (void)notifyUserForIncomingMessage:(TSIncomingMessage *)message
|
|
|
|
inThread:(TSThread *)thread
|
|
|
|
contactsManager:(id<ContactsManagerProtocol>)contactsManager
|
2017-09-29 21:49:03 +02:00
|
|
|
transaction:(YapDatabaseReadTransaction *)transaction
|
2016-11-12 18:22:29 +01:00
|
|
|
{
|
2017-06-02 21:49:34 +02:00
|
|
|
OWSAssert(message);
|
|
|
|
OWSAssert(thread);
|
|
|
|
OWSAssert(contactsManager);
|
|
|
|
|
2017-09-29 21:49:03 +02:00
|
|
|
// While batch processing, some of the necessary changes have not been commited.
|
|
|
|
NSString *messageDescription = [message previewTextWithTransaction:transaction];
|
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
if (thread.isMuted) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-12 23:13:34 +02:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
BOOL shouldPlaySound = [self shouldPlaySoundForNotification];
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
NSString *senderName = [contactsManager displayNameForPhoneIdentifier:message.authorId];
|
|
|
|
NSString *groupName = [thread.name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
|
|
|
if (groupName.length < 1) {
|
|
|
|
groupName = NSLocalizedString(@"NEW_GROUP_DEFAULT_TITLE", @"");
|
2017-06-12 23:13:34 +02:00
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive && messageDescription) {
|
|
|
|
UILocalNotification *notification = [[UILocalNotification alloc] init];
|
|
|
|
if (shouldPlaySound) {
|
|
|
|
notification.soundName = kNotificationsManagerNewMesssageSoundName;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (self.notificationPreviewType) {
|
|
|
|
case NotificationNamePreview: {
|
|
|
|
|
|
|
|
// Don't reply from lockscreen if anyone in this conversation is
|
|
|
|
// "no longer verified".
|
|
|
|
BOOL isNoLongerVerified = NO;
|
|
|
|
for (NSString *recipientId in thread.recipientIdentifiers) {
|
|
|
|
if ([OWSIdentityManager.sharedManager verificationStateForRecipientId:recipientId]
|
|
|
|
== OWSVerificationStateNoLongerVerified) {
|
|
|
|
isNoLongerVerified = YES;
|
|
|
|
break;
|
|
|
|
}
|
2017-06-23 20:18:17 +02:00
|
|
|
}
|
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
notification.category = (isNoLongerVerified ? Signal_Full_New_Message_Category_No_Longer_Verified
|
|
|
|
: Signal_Full_New_Message_Category);
|
|
|
|
notification.userInfo = @{
|
|
|
|
Signal_Thread_UserInfo_Key : thread.uniqueId,
|
|
|
|
Signal_Message_UserInfo_Key : message.uniqueId
|
|
|
|
};
|
|
|
|
|
|
|
|
if ([thread isGroupThread]) {
|
|
|
|
NSString *threadName = [NSString stringWithFormat:@"\"%@\"", groupName];
|
|
|
|
// TODO: Format parameters might change order in l10n. We should use named parameters.
|
|
|
|
notification.alertBody =
|
|
|
|
[NSString stringWithFormat:NSLocalizedString(@"APN_MESSAGE_IN_GROUP_DETAILED", nil),
|
|
|
|
senderName,
|
|
|
|
threadName,
|
|
|
|
messageDescription];
|
|
|
|
} else {
|
|
|
|
notification.alertBody = [NSString stringWithFormat:@"%@: %@", senderName, messageDescription];
|
|
|
|
}
|
|
|
|
break;
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
2017-09-28 16:01:17 +02:00
|
|
|
case NotificationNameNoPreview: {
|
|
|
|
notification.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId };
|
|
|
|
if ([thread isGroupThread]) {
|
|
|
|
notification.alertBody = [NSString
|
|
|
|
stringWithFormat:@"%@ \"%@\"", NSLocalizedString(@"APN_MESSAGE_IN_GROUP", nil), groupName];
|
|
|
|
} else {
|
|
|
|
notification.alertBody = [NSString
|
|
|
|
stringWithFormat:@"%@ %@", NSLocalizedString(@"APN_MESSAGE_FROM", nil), senderName];
|
|
|
|
}
|
|
|
|
break;
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
2017-09-28 16:01:17 +02:00
|
|
|
case NotificationNoNameNoPreview:
|
|
|
|
notification.alertBody = NSLocalizedString(@"APN_Message", nil);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DDLogWarn(@"unknown notification preview type: %lu", (unsigned long)self.notificationPreviewType);
|
|
|
|
notification.alertBody = NSLocalizedString(@"APN_Message", nil);
|
|
|
|
break;
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
[[PushManager sharedManager] presentNotification:notification checkForCancel:YES];
|
|
|
|
} else {
|
|
|
|
if (shouldPlaySound && [Environment.preferences soundInForeground]) {
|
|
|
|
AudioServicesPlayAlertSound(_newMessageSound);
|
|
|
|
}
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
2017-09-28 16:01:17 +02:00
|
|
|
});
|
2015-12-22 12:45:09 +01:00
|
|
|
}
|
|
|
|
|
2017-06-12 23:13:34 +02:00
|
|
|
- (BOOL)shouldPlaySoundForNotification
|
|
|
|
{
|
2017-06-13 19:45:07 +02:00
|
|
|
@synchronized(self)
|
|
|
|
{
|
|
|
|
// Play no more than 2 notification sounds in a given
|
|
|
|
// five-second window.
|
|
|
|
const CGFloat kNotificationWindowSeconds = 5.f;
|
|
|
|
const NSUInteger kMaxNotificationRate = 2;
|
|
|
|
|
|
|
|
// Cull obsolete notification timestamps from the thread's notification history.
|
|
|
|
while (self.notificationHistory.count > 0) {
|
|
|
|
NSDate *notificationTimestamp = self.notificationHistory[0];
|
|
|
|
CGFloat notificationAgeSeconds = fabs(notificationTimestamp.timeIntervalSinceNow);
|
|
|
|
if (notificationAgeSeconds > kNotificationWindowSeconds) {
|
|
|
|
[self.notificationHistory removeObjectAtIndex:0];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2017-06-12 23:13:34 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 19:45:07 +02:00
|
|
|
// Ignore notifications if necessary.
|
|
|
|
BOOL shouldPlaySound = self.notificationHistory.count < kMaxNotificationRate;
|
2017-06-12 23:13:34 +02:00
|
|
|
|
2017-06-13 19:45:07 +02:00
|
|
|
if (shouldPlaySound) {
|
|
|
|
// Add new notification timestamp to the thread's notification history.
|
|
|
|
NSDate *newNotificationTimestamp = [NSDate new];
|
|
|
|
[self.notificationHistory addObject:newNotificationTimestamp];
|
2017-06-12 23:13:34 +02:00
|
|
|
|
2017-06-13 19:45:07 +02:00
|
|
|
return YES;
|
|
|
|
} else {
|
|
|
|
DDLogDebug(@"Skipping sound for notification");
|
|
|
|
return NO;
|
|
|
|
}
|
2017-06-12 23:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
#pragma mark - Util
|
|
|
|
|
|
|
|
- (NotificationType)notificationPreviewType
|
|
|
|
{
|
2017-09-14 21:45:04 +02:00
|
|
|
OWSPreferences *prefs = [Environment getCurrent].preferences;
|
2016-11-12 18:22:29 +01:00
|
|
|
return prefs.notificationPreviewType;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)presentNotification:(UILocalNotification *)notification identifier:(NSString *)identifier
|
|
|
|
{
|
2017-05-17 21:45:00 +02:00
|
|
|
DispatchMainThreadSafe(^{
|
2017-04-14 21:27:31 +02:00
|
|
|
// Replace any existing notification
|
|
|
|
// e.g. when an "Incoming Call" notification gets replaced with a "Missed Call" notification.
|
|
|
|
if (self.currentNotifications[identifier]) {
|
2017-05-17 21:45:00 +02:00
|
|
|
[self cancelNotificationWithIdentifier:identifier];
|
2017-04-14 21:27:31 +02:00
|
|
|
}
|
2016-11-12 18:22:29 +01:00
|
|
|
|
2017-04-14 21:27:31 +02:00
|
|
|
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
|
|
|
|
DDLogDebug(@"%@ presenting notification with identifier: %@", self.tag, identifier);
|
2016-11-12 18:22:29 +01:00
|
|
|
|
2017-04-14 21:27:31 +02:00
|
|
|
self.currentNotifications[identifier] = notification;
|
|
|
|
});
|
2016-11-12 18:22:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)cancelNotificationWithIdentifier:(NSString *)identifier
|
|
|
|
{
|
2017-05-17 21:45:00 +02:00
|
|
|
DispatchMainThreadSafe(^{
|
|
|
|
UILocalNotification *notification = self.currentNotifications[identifier];
|
|
|
|
if (!notification) {
|
|
|
|
DDLogWarn(
|
|
|
|
@"%@ Couldn't cancel notification because none was found with identifier: %@", self.tag, identifier);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[self.currentNotifications removeObjectForKey:identifier];
|
2017-05-17 21:44:49 +02:00
|
|
|
|
2017-05-17 21:45:00 +02:00
|
|
|
[[UIApplication sharedApplication] cancelLocalNotification:notification];
|
|
|
|
});
|
2017-05-17 21:44:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-12 18:22:29 +01:00
|
|
|
#pragma mark - Logging
|
|
|
|
|
|
|
|
+ (NSString *)tag
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tag
|
|
|
|
{
|
|
|
|
return self.class.tag;
|
|
|
|
}
|
|
|
|
|
2015-12-22 12:45:09 +01:00
|
|
|
@end
|