Skip to content

Commit 42f2cd1

Browse files
authored
Merge pull request #213 from Unity-Technologies/ios-sound-type-support
Add sound type support for iOS
2 parents c1d6cc1 + 434daef commit 42f2cd1

File tree

8 files changed

+134
-3
lines changed

8 files changed

+134
-3
lines changed

TestProjects/Main/Assets/Resources/iOSNotifications/TimeIntervalTrigger/Only Body.asset

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ MonoBehaviour:
1212
m_Script: {fileID: 11500000, guid: 4fc6c74f6df364b36b9afcbfd8cfbca3, type: 3}
1313
m_Name: Only Body
1414
m_EditorClassIdentifier:
15-
ButtonName: Empty notification + body
15+
ButtonName: Empty notification + body (silent)
1616
Identifier:
1717
CategoryIdentifier:
1818
ThreadIdentifier:
@@ -25,3 +25,4 @@ MonoBehaviour:
2525
Data:
2626
TimeTriggerInterval: 1
2727
Repeats: 0
28+
SoundType: 4

TestProjects/Main/Assets/Scripts/ScriptableObjects/iOSNotificationTemplateTimeTrigger.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class iOSNotificationTemplateTimeTrigger : ScriptableObject
3636
[Header("Time Trigger")]
3737
public Int32 TimeTriggerInterval;
3838
public bool Repeats = false;
39+
public NotificationSoundType SoundType;
3940

4041
[Serializable]
4142
public struct UserDataItem

TestProjects/Main/Assets/Scripts/iOSTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ private void InstantiateAllTestButtons()
226226
ForegroundPresentationOption = template.PresentationOptions,
227227
Badge = template.Badge,
228228
Data = template.Data,
229+
SoundType = template.SoundType,
229230
Trigger = new iOSNotificationTimeIntervalTrigger()
230231
{
231232
TimeInterval = TimeSpan.FromSeconds(template.TimeTriggerInterval),

com.unity.mobile.notifications/Documentation~/iOS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,3 +225,9 @@ Sometimes, you might want to set custom data on the payload for a remote notific
225225
```
226226

227227
You have to use the exact `data` as the key of your custom data, because this is what the package looks for.
228+
229+
#### Images, videos and sounds
230+
231+
By default notifications use the default system sound. Sound can be disabled by changing the [iOSNotificationCenter.SoundType](../api/Unity.Notifications.iOS.iOSNotification.html#Unity_Notifications_iOS_iOSNotification_SoundType). A custom sound can be used by using Default sound type and assigning sound file name to [iOSNotificationCenter.SoundName](../api/Unity.Notifications.iOS.iOSNotification.html#Unity_Notifications_iOS_iOSNotification_SoundName). The sound file itself has to be manually added to XCode project. For more information regarding file placement and supported formats, see [Apple documentation](https://developer.apple.com/documentation/usernotifications/unnotificationsound?language=objc).
232+
233+
Images or video can added to notifications by using [attachments](../api/Unity.Notifications.iOS.iOSNotificationAttachment.html).

com.unity.mobile.notifications/Runtime/iOS/Plugins/UnityNotificationData.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ enum triggerType
1717
UNKNOWN_TRIGGER = -1,
1818
};
1919

20+
enum UnitySoundType
21+
{
22+
kSoundTypeDefault = 0,
23+
kSoundTypeCritical = 1,
24+
kSoundTypeRingtone = 2,
25+
kSoundTypeNone = 4,
26+
};
27+
2028
typedef struct iOSNotificationData
2129
{
2230
char* identifier;
@@ -26,6 +34,9 @@ typedef struct iOSNotificationData
2634
char* subtitle;
2735
char* categoryIdentifier;
2836
char* threadIdentifier;
37+
int soundType;
38+
float soundVolume;
39+
char* soundName;
2940

3041
void* userInfo;
3142
void* attachments;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ void initiOSNotificationData(iOSNotificationData* notificationData)
7878
notificationData->subtitle = NULL;
7979
notificationData->categoryIdentifier = NULL;
8080
notificationData->threadIdentifier = NULL;
81+
notificationData->soundType = kSoundTypeDefault;
82+
notificationData->soundVolume = -1.0f;
83+
notificationData->soundName = NULL;
8184
notificationData->triggerType = PUSH_TRIGGER;
8285
notificationData->userInfo = NULL;
8386
}
@@ -201,6 +204,9 @@ void freeiOSNotificationData(iOSNotificationData* notificationData)
201204
if (notificationData->threadIdentifier != NULL)
202205
free(notificationData->threadIdentifier);
203206

207+
if (notificationData->soundName != NULL)
208+
free(notificationData->soundName);
209+
204210
if (notificationData->userInfo != NULL)
205211
{
206212
NSDictionary* userInfo = (__bridge_transfer NSDictionary*)notificationData->userInfo;

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];
384+
return [UNNotificationSound criticalSoundNamed: soundName withAudioVolume: data->soundVolume];
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

com.unity.mobile.notifications/Runtime/iOS/iOSNotification.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,34 @@ public enum PresentationOption
3232
Alert = 1 << 2,
3333
}
3434

35+
/// <summary>
36+
/// The type of sound to use for the notification.
37+
/// See Apple documentation for details.
38+
/// </summary>
39+
/// <see href="https://developer.apple.com/documentation/usernotifications/unnotificationsound?language=objc"/>
40+
public enum NotificationSoundType
41+
{
42+
/// <summary>
43+
/// Play the default sound.
44+
/// </summary>
45+
Default = 0,
46+
47+
/// <summary>
48+
/// Critical sound (bypass Do Not Disturb)
49+
/// </summary>
50+
Critical = 1,
51+
52+
/// <summary>
53+
/// Ringtone sound.
54+
/// </summary>
55+
Ringtone = 2,
56+
57+
/// <summary>
58+
/// No sound.
59+
/// </summary>
60+
None = 4,
61+
}
62+
3563
[StructLayout(LayoutKind.Sequential)]
3664
internal struct TimeTriggerData
3765
{
@@ -82,6 +110,9 @@ internal struct iOSNotificationData
82110
public string subtitle;
83111
public string categoryIdentifier;
84112
public string threadIdentifier;
113+
public Int32 soundType;
114+
public float soundVolume;
115+
public string soundName;
85116

86117
public IntPtr userInfo;
87118
public IntPtr attachments;
@@ -213,6 +244,32 @@ public int Badge
213244
set { data.badge = value; }
214245
}
215246

247+
/// <summary>
248+
/// The type of sound to be played.
249+
/// </summary>
250+
public NotificationSoundType SoundType
251+
{
252+
get { return (NotificationSoundType)data.soundType; }
253+
set { data.soundType = (int)value; }
254+
}
255+
256+
/// <summary>
257+
/// The name of the sound to be played. Use null for system default sound.
258+
/// See Apple documentation for named sounds and sound file placement.
259+
/// </summary>
260+
public string SoundName
261+
{
262+
get { return data.soundName; }
263+
set { data.soundName = value; }
264+
}
265+
266+
/// <summary>
267+
/// The volume for the sound. Use null to use the default volume.
268+
/// See Apple documentation for supported values.
269+
/// </summary>
270+
/// <see href="https://developer.apple.com/documentation/usernotifications/unnotificationsound/2963118-defaultcriticalsoundwithaudiovol?language=objc"/>
271+
public float? SoundVolume { get; set; }
272+
216273
/// <summary>
217274
/// Arbitrary string data which can be retrieved when the notification is used to open the app or is received while the app is running.
218275
/// </summary>
@@ -400,6 +457,11 @@ internal iOSNotificationWithUserInfo GetDataForSending()
400457
{
401458
if (data.identifier == null)
402459
data.identifier = GenerateUniqueID();
460+
if (SoundVolume.HasValue)
461+
data.soundVolume = SoundVolume.Value;
462+
else
463+
data.soundVolume = -1.0f;
464+
403465
iOSNotificationWithUserInfo ret;
404466
ret.data = data;
405467
ret.userInfo = userInfo;

0 commit comments

Comments
 (0)