ToastNotification 클래스

정의

알림 메시지의 콘텐츠, 연결된 메타데이터 및 이벤트 및 만료 시간을 정의합니다.

public ref class ToastNotification sealed
/// [Windows.Foundation.Metadata.Activatable(Windows.UI.Notifications.IToastNotificationFactory, 65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
class ToastNotification final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
/// [Windows.Foundation.Metadata.Activatable(Windows.UI.Notifications.IToastNotificationFactory, 65536, "Windows.Foundation.UniversalApiContract")]
class ToastNotification final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
/// [Windows.Foundation.Metadata.Activatable(Windows.UI.Notifications.IToastNotificationFactory, 65536, "Windows.Foundation.UniversalApiContract")]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class ToastNotification final
[Windows.Foundation.Metadata.Activatable(typeof(Windows.UI.Notifications.IToastNotificationFactory), 65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
public sealed class ToastNotification
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
[Windows.Foundation.Metadata.Activatable(typeof(Windows.UI.Notifications.IToastNotificationFactory), 65536, "Windows.Foundation.UniversalApiContract")]
public sealed class ToastNotification
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.MTA)]
[Windows.Foundation.Metadata.Activatable(typeof(Windows.UI.Notifications.IToastNotificationFactory), 65536, "Windows.Foundation.UniversalApiContract")]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class ToastNotification
function ToastNotification(content)
Public NotInheritable Class ToastNotification
상속
Object IInspectable ToastNotification
특성

Windows 요구 사항

디바이스 패밀리
Windows 10 (10.0.10240.0 - for Xbox, see UWP features that aren't yet supported on Xbox에서 도입되었습니다.)
API contract
Windows.Foundation.UniversalApiContract (v1.0에서 도입되었습니다.)

예제

다음 예제에서는 생성자 사용을 ToastNotification 포함하여 텍스트 및 이미지가 포함된 알림 메시지를 만들고 보내는 방법을 보여 줍니다.

var notifications = Windows.UI.Notifications;

// Get the toast notification manager for the current app.
var notificationManager = notifications.ToastNotificationManager;

// The getTemplateContent method returns a Windows.Data.Xml.Dom.XmlDocument object
// that contains the toast notification XML content.
var template = notifications.toastTemplateType.toastImageAndText01;
var toastXml = notificationManager.getTemplateContent(notifications.ToastTemplateType[template]);

// You can use the methods from the XML document to specify the required elements for the toast.
var images = toastXml.getElementsByTagName("image");
images[0].setAttribute("src", "images/toastImageAndText.png");

var textNodes = toastXml.getElementsByTagName("text");
textNodes.forEach(function (value, index) {
    var textNumber = index + 1;
    var text = "";
    for (var j = 0; j < 10; j++) {
        text += "Text input " + /*@static_cast(String)*/textNumber + " ";
    }
    value.appendChild(toastXml.createTextNode(text));
});

// Create a toast notification from the XML, then create a ToastNotifier object
// to send the toast.
var toast = new notifications.ToastNotification(toastXml);

notificationManager.createToastNotifier().show(toast);

다음 예제에서는 이벤트를 수신 대기하고 처리하는 Dismissed 방법을 보여줍니다.

var notifications = Windows.UI.Notifications;

yourToastNotification.addEventListener("dismissed", function (e) {
    switch (e.reason) {
        case notifications.ToastDismissalReason.applicationHidden:
            // The application hid the toast using ToastNotifier.hide.
            break;
        case notifications.ToastDismissalReason.userCanceled:
            // The user dismissed the toast.
            break;
        case notifications.ToastDismissalReason.timedOut:
            // The toast has expired.
            break;
    }
}

이벤트 처리기 사용

다음 예제에서는 실행 중인 데스크톱 앱에서 알림 활성화를 위한 이벤트 처리기를 추가하는 방법을 보여줍니다. ToastNotifications는 이후 콜백에 대한 알림에 대한 참조를 유지하려면 목록에 유지되어야 합니다. 해제 및 만료된 알림 이벤트에 대해 유사한 패턴을 따를 수 있습니다.

중요

메모리 누수 방지를 위해 더 이상 필요하지 않은 경우 이벤트 처리기가 구독 취소되었는지 확인합니다.

class AppNotification
{
    protected List<ToastNotification> toastNotificationList = new List<ToastNotification>();

    public void SendToastNotification()
    {
        // Constructs the content
        ToastContent content = new ToastContentBuilder()
            .AddText("Firing Toast")
            .GetToastContent();

        // Creates the notification
        ToastNotification notification = new ToastNotification(content.GetXml());

        //Add an in memory event handler
        notification.Activated += ToastNotificationCallback_Activated;

        //Adds toast notification to list to persist toast
        toastNotificationList.Add(notification);

        //Sends the notification
        ToastNotificationManager.CreateToastNotifier().Show(notification);
    }

    private void ToastNotificationCallback_Activated(ToastNotification sender, object args)
    {
        //Handle Activation Here
    }

    ~AppNotification()
    {
        foreach(ToastNotification tn in toastNotificationList)
        {
            //Unsubscribe
            tn.Activated -= ToastNotificationCallback_Activated;
        }
        toastNotificationList.Clear();
    }
}

설명

  • 활성화 지침 처리
    • UWP 애플리케이션은 알림 활성화를 처리하는 데 사용해야 OnActivated 합니다.
    • WinRT 빌드 19041을 응시하면 MSIX 및 스파스 서명 패키지 애플리케이션이 활성화를 처리하는 데 사용할 ToastNotificationActionTrigger 수 있습니다.
    • 데스크톱 앱은 데스크톱 - 로컬 알림 보내기에 따라 COM 활성화를 사용할 수 있습니다.
    • 애플리케이션에 적합한 활성화 옵션이 없는 경우 이벤트 처리기를 제대로 사용하려면 이 문서의 예제를 따르세요.

버전 기록

Windows 버전 SDK 버전 추가된 값
1607 14393 NotificationMirroring
1607 14393 RemoteId
1703 15063 데이터
1703 15063 우선 순위
1903 18362 ExpiresOnReboot

생성자

ToastNotification(XmlDocument)

의 새 인스턴스 ToastNotification를 만들고 초기화합니다.

속성

Content

현재 알림 메시지를 정의하는 XML을 가져옵니다.

Data

알림 메시지의 상태에 대한 추가 정보를 가져오거나 설정합니다.

ExpirationTime

알림 메시지를 표시하지 않아야 하는 시간을 가져오거나 설정합니다.

ExpiresOnReboot

다시 부팅한 후 알림 센터에 알림 메시지가 남아 있는지 여부를 나타냅니다.

Group

알림에 대한 그룹 식별자를 가져오거나 설정합니다.

NotificationMirroring

알림 미러링이 허용되는지 여부를 지정하는 값을 가져오거나 설정합니다.

Priority

알림 메시지의 우선 순위를 가져오거나 설정합니다.

RemoteId

시스템에서 이 알림을 다른 디바이스에서 생성된 다른 알림과 상호 연결할 수 있도록 하는 알림의 원격 ID를 가져오거나 설정합니다.

SuppressPopup

알림의 팝업 UI가 사용자의 화면에 표시되는지 여부를 가져오거나 설정합니다.

Windows 8.x 디바이스로 전송된 알림에서 이 속성을 true 로 설정하지 마세요. 이렇게 하면 컴파일러 오류 또는 삭제된 알림이 발생합니다.

Tag

알림 Group내에서 이 알림의 고유 식별자를 가져오거나 설정합니다.

이벤트

Activated

사용자가 클릭 또는 터치를 통해 알림 메시지를 활성화할 때 발생합니다. 실행 중인 앱은 이 이벤트를 구독합니다.

Dismissed

알림 메시지가 만료되거나 사용자가 명시적으로 해제하여 화면을 떠날 때 발생합니다. 실행 중인 앱은 이 이벤트를 구독합니다.

Failed

Windows 알림 메시지를 발생시키려고 할 때 오류가 발생할 때 발생합니다. 실행 중인 앱은 이 이벤트를 구독합니다.

적용 대상

추가 정보