Custom notification sounds in local notifications.

This commit is contained in:
Matthew Chen 2018-02-20 22:19:33 -05:00
parent a837c5d41b
commit 9aa02489b8
5 changed files with 35 additions and 38 deletions

View File

@ -364,7 +364,6 @@
B10C9B621A7049EC00ECA2BF /* play_icon@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B10C9B5E1A7049EC00ECA2BF /* play_icon@2x.png */; };
B609597C1C2C0FC6004E8797 /* iRate.m in Sources */ = {isa = PBXBuildFile; fileRef = B609597B1C2C0FC6004E8797 /* iRate.m */; };
B60EDE041A05A01700D73516 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B60EDE031A05A01700D73516 /* AudioToolbox.framework */; };
B625CD561ABB589C00E8B23C /* NewMessage.aifc in Resources */ = {isa = PBXBuildFile; fileRef = B625CD551ABB589C00E8B23C /* NewMessage.aifc */; };
B633C5861A1D190B0059AC12 /* call@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B633C5041A1D190B0059AC12 /* call@2x.png */; };
B633C58D1A1D190B0059AC12 /* contact_default_feed.png in Resources */ = {isa = PBXBuildFile; fileRef = B633C50B1A1D190B0059AC12 /* contact_default_feed.png */; };
B633C59D1A1D190B0059AC12 /* endcall@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = B633C51B1A1D190B0059AC12 /* endcall@2x.png */; };
@ -939,7 +938,6 @@
B609597A1C2C0FC6004E8797 /* iRate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = iRate.h; path = Libraries/iRate/iRate.h; sourceTree = SOURCE_ROOT; };
B609597B1C2C0FC6004E8797 /* iRate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = iRate.m; path = Libraries/iRate/iRate.m; sourceTree = SOURCE_ROOT; };
B60EDE031A05A01700D73516 /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; };
B625CD551ABB589C00E8B23C /* NewMessage.aifc */ = {isa = PBXFileReference; lastKnownFileType = file; path = NewMessage.aifc; sourceTree = "<group>"; };
B633C5041A1D190B0059AC12 /* call@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "call@2x.png"; sourceTree = "<group>"; };
B633C50B1A1D190B0059AC12 /* contact_default_feed.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = contact_default_feed.png; sourceTree = "<group>"; };
B633C51B1A1D190B0059AC12 /* endcall@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "endcall@2x.png"; sourceTree = "<group>"; };
@ -2090,7 +2088,6 @@
E18AB40618A05754001A532A /* AudioFiles */ = {
isa = PBXGroup;
children = (
B625CD551ABB589C00E8B23C /* NewMessage.aifc */,
E18AB40718A05754001A532A /* busy.mp3 */,
E18AB40918A05754001A532A /* failure.mp3 */,
E18AB40B18A05754001A532A /* outring.mp3 */,
@ -2475,7 +2472,6 @@
34074FAE203D14D8004596AE /* aurora.m4r in Resources */,
E1370BE018A0686600826894 /* busy.mp3 in Resources */,
E1370BE218A0686C00826894 /* failure.mp3 in Resources */,
B625CD561ABB589C00E8B23C /* NewMessage.aifc in Resources */,
B633C58D1A1D190B0059AC12 /* contact_default_feed.png in Resources */,
B10C9B621A7049EC00ECA2BF /* play_icon@2x.png in Resources */,
B633C5861A1D190B0059AC12 /* call@2x.png in Resources */,

Binary file not shown.

View File

@ -19,8 +19,6 @@
#import <SignalServiceKit/TextSecureKitEnv.h>
#import <SignalServiceKit/Threading.h>
NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
@interface NotificationsManager ()
@property (nonatomic, readonly) NSMutableDictionary<NSString *, UILocalNotification *> *currentNotifications;
@ -102,7 +100,8 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
};
if ([self shouldPlaySoundForNotification]) {
notification.soundName = kNotificationsManagerNewMesssageSoundName;
NotificationSound notificationSound = [Environment preferences].globalNotificationSound;
notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound];
}
NSString *alertMessage;
@ -141,7 +140,8 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
Signal_Thread_UserInfo_Key : thread.uniqueId
};
if ([self shouldPlaySoundForNotification]) {
notification.soundName = kNotificationsManagerNewMesssageSoundName;
NotificationSound notificationSound = [Environment preferences].globalNotificationSound;
notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound];
}
NSString *alertMessage;
@ -181,7 +181,8 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
Signal_Thread_UserInfo_Key : thread.uniqueId
};
if ([self shouldPlaySoundForNotification]) {
notification.soundName = kNotificationsManagerNewMesssageSoundName;
NotificationSound notificationSound = [Environment preferences].globalNotificationSound;
notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound];
}
NSString *alertMessage;
@ -225,7 +226,8 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId };
if (shouldPlaySound) {
notification.soundName = kNotificationsManagerNewMesssageSoundName;
NotificationSound notificationSound = [Environment preferences].globalNotificationSound;
notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound];
}
NSString *alertBodyString = @"";
@ -287,7 +289,8 @@ NSString *const kNotificationsManagerNewMesssageSoundName = @"NewMessage.aifc";
if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive && messageText) {
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (shouldPlaySound) {
notification.soundName = kNotificationsManagerNewMesssageSoundName;
NotificationSound notificationSound = [Environment preferences].globalNotificationSound;
notification.soundName = [NotificationSounds filenameForNotificationSound:notificationSound];
}
switch (self.notificationPreviewType) {

View File

@ -28,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN
+ (NSString *)displayNameForNotificationSound:(NotificationSound)notificationSound;
+ (NSString *)filenameForNotificationSound:(NotificationSound)notificationSound;
+ (void)playNotificationSound:(NotificationSound)notificationSound;
@end

View File

@ -87,47 +87,43 @@
}
}
- (NSURL *)soundURLForNotificationSound:(NotificationSound)notificationSound
+ (NSString *)filenameForNotificationSound:(NotificationSound)notificationSound
{
NSURL *_Nullable url;
// TODO: Should we localize these sound names?
switch (notificationSound) {
case NotificationSound_Aurora:
url = [[NSBundle mainBundle] URLForResource:@"aurora" withExtension:@"m4r"];
break;
return @"aurora.m4r";
case NotificationSound_Bamboo:
url = [[NSBundle mainBundle] URLForResource:@"bamboo" withExtension:@"m4r"];
break;
return @"bamboo.m4r";
case NotificationSound_Chord:
url = [[NSBundle mainBundle] URLForResource:@"chord" withExtension:@"m4r"];
break;
return @"chord.m4r";
case NotificationSound_Circles:
url = [[NSBundle mainBundle] URLForResource:@"circles" withExtension:@"m4r"];
break;
return @"circles.m4r";
case NotificationSound_Complete:
url = [[NSBundle mainBundle] URLForResource:@"complete" withExtension:@"m4r"];
break;
return @"complete.m4r";
case NotificationSound_Hello:
url = [[NSBundle mainBundle] URLForResource:@"hello" withExtension:@"m4r"];
break;
return @"hello.m4r";
case NotificationSound_Input:
url = [[NSBundle mainBundle] URLForResource:@"input" withExtension:@"m4r"];
break;
return @"input.m4r";
case NotificationSound_Keys:
url = [[NSBundle mainBundle] URLForResource:@"keys" withExtension:@"m4r"];
break;
return @"keys.m4r";
case NotificationSound_Note:
url = [[NSBundle mainBundle] URLForResource:@"note" withExtension:@"m4r"];
break;
return @"note.m4r";
case NotificationSound_Popcorn:
url = [[NSBundle mainBundle] URLForResource:@"popcorn" withExtension:@"m4r"];
break;
return @"popcorn.m4r";
case NotificationSound_Pulse:
url = [[NSBundle mainBundle] URLForResource:@"pulse" withExtension:@"m4r"];
break;
return @"pulse.m4r";
case NotificationSound_Synth:
url = [[NSBundle mainBundle] URLForResource:@"synth" withExtension:@"m4r"];
break;
return @"synth.m4r";
}
}
+ (NSURL *)soundURLForNotificationSound:(NotificationSound)notificationSound
{
NSString *filename = [self filenameForNotificationSound:notificationSound];
NSURL *_Nullable url = [[NSBundle mainBundle] URLForResource:filename.stringByDeletingPathExtension
withExtension:filename.pathExtension];
OWSAssert(url);
return url;
}
@ -146,7 +142,7 @@
}
NSNumber *_Nullable systemSoundID = self.systemSoundIDMap[@(notificationSound)];
if (!systemSoundID) {
NSURL *soundURL = [self soundURLForNotificationSound:notificationSound];
NSURL *soundURL = [NotificationSounds soundURLForNotificationSound:notificationSound];
SystemSoundID newSystemSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &newSystemSoundID);
if (error) {