Skip to content

Commit 164f2c8

Browse files
committed
Implement different sound types
1 parent 9adf80d commit 164f2c8

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

com.unity.mobile.notifications/Runtime/iOS/Plugins/UnityNotificationManager.m

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,9 @@ - (void)scheduleLocalNotification:(iOSNotificationData*)data
297297
if (data->threadIdentifier != NULL)
298298
content.threadIdentifier = [NSString stringWithUTF8String: data->threadIdentifier];
299299

300-
// TODO add a way to specify custom sounds.
301-
content.sound = [UNNotificationSound defaultSound];
300+
UNNotificationSound* sound = [self soundForNotification: data];
301+
if (sound != nil)
302+
content.sound = sound;
302303

303304
content.attachments = (__bridge_transfer NSArray<UNNotificationAttachment*>*)data->attachments;
304305
data->attachments = NULL;
@@ -363,5 +364,47 @@ - (void)scheduleLocalNotification:(iOSNotificationData*)data
363364
}];
364365
}
365366

367+
- (UNNotificationSound*)soundForNotification:(const iOSNotificationData*)data
368+
{
369+
NSString* soundName = nil;
370+
if (data->soundName != NULL)
371+
soundName = [NSString stringWithUTF8String: data->soundName];
372+
373+
switch (data->soundType)
374+
{
375+
case kSoundTypeNone:
376+
return nil;
377+
case kSoundTypeCritical:
378+
if (@available(iOS 12.0, *))
379+
{
380+
if (soundName != nil)
381+
{
382+
if (data->soundVolume < 0)
383+
return [UNNotificationSound criticalSoundNamed: soundName withAudioVolume: data->soundVolume];
384+
return [UNNotificationSound criticalSoundNamed: soundName];
385+
}
386+
if (data->soundVolume >= 0)
387+
return [UNNotificationSound defaultCriticalSoundWithAudioVolume: data->soundVolume];
388+
return UNNotificationSound.defaultCriticalSound;
389+
}
390+
else
391+
goto default_fallback;
392+
case kSoundTypeRingtone:
393+
if (@available(iOS 15.2, *))
394+
{
395+
if (soundName != nil)
396+
return [UNNotificationSound ringtoneSoundNamed: soundName];
397+
return UNNotificationSound.defaultRingtoneSound;
398+
}
399+
// continue to default
400+
case kSoundTypeDefault:
401+
default:
402+
default_fallback:
403+
if (soundName != nil)
404+
return [UNNotificationSound soundNamed: soundName];
405+
return UNNotificationSound.defaultSound;
406+
}
407+
}
408+
366409
@end
367410
#endif

0 commit comments

Comments
 (0)