Private/Pop-MacOSNotification.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# The function that handles macOS notifications
function Pop-MacOSNotification {
    [CmdletBinding(SupportsShouldProcess=$true)]
    param (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [string]
        $Body,

        [Parameter(Mandatory=$true)]
        [string]
        $Title,

        [Parameter()]
        [string]
        $Icon
    )

    # If the command requires advanced features, we should use alerter.
    # This if contains all the parameters that would need it.
    if ($Icon) {
        $splat = @{
            Message = $Body
            Title = $Title
            Timeout = 4
            Silent = $true
        }

        if ($Icon) {
            $splat.Add('AppIcon', $Icon)
        }
        if($PSCmdlet.ShouldProcess("running: Invoke-AlerterNotification $(ConvertTo-ParameterString $splat)")) {
            MacNotify\Invoke-AlerterNotification @splat
        }
    } else {
        $splat = @{
            Message = $Body
            Title = $Title
        }
        if($PSCmdlet.ShouldProcess("running: Invoke-MacNotification $(ConvertTo-ParameterString $splat)")) {
            MacNotify\Invoke-MacNotification @splat
        }
    }
}