# Windows (UWP/WSA)

## Module initialization

1. For the Messaging module to function you need the basic Analytics package. Before the notification initialization, you need to initialize the SDK. More about it you can read here: [Unity Integration](/integration/integration-of-sdk-v2/sdk-integration/unity.md).
2. Add the **`DTDAnalytics`** initialization block after the **`DTDMessaging`** initialization block.

```csharp
#if UNITY_WSA
  DTDMessaging.WSA.SetMessagingEnabling(true);
#endif#
```

{% hint style="info" %}
Attention! Use the `#if UNITY_WSA` define to surround any notification module code on the WSA platform.
{% endhint %}

```csharp
#if UNITY_WSA
  DTDMessaging.WSA.{AnyMethod}
#endif#
```

### Optional

#### Module status check

Use the following method to check current status:

```csharp
void GetMessagingEnabling(Action<bool> onGetMessagingEnabling)
```

The current module status will be sent to **`onGetMessagingEnabling`** callback.

#### Listening to events

You can listen to basic events of the Messaging module: create the class that implements the **`IDTDPushListener`** interface and send it to the **`DTDMessaging.WSA.SetPushListener`** method.

Class example:

```csharp
public class MyPushListener : IDTDPushListener
{
    public void OnPushServiceRegistrationSuccessful(string deviceId)
    {
        Debug.Log(deviceId);
    }

    public void OnPushServiceRegistrationFailed(string error)
    {
        Debug.Log(error);

    }

    public void OnPushNotificationReceived(DTDPushMessage message)
    {
        Debug.Log(message);
    }

    public void OnInvisibleNotificationReceived(DTDPushMessage message)
    {
        //IOS only.
    }

    public void OnPushNotificationOpened(DTDPushMessage pushMessage, DTDActionButton actionButton)
    {
        Debug.Log(pushMessage.ToString());
        Debug.Log(actionButton.ToString());
    }
}
```

A complete example of notification module initialization:

```csharp
public class MyPushListener : IDTDPushListener
{
    public void OnPushServiceRegistrationSuccessful(string deviceId)
    {
        Debug.Log(deviceId);
    }

    public void OnPushServiceRegistrationFailed(string error)
    {
        Debug.Log(error);
    }

    public void OnPushNotificationReceived(DTDPushMessage message)
    {
        Debug.Log(message);
    }

    public void OnInvisibleNotificationReceived(DTDPushMessage message)
    {
        Debug.Log(message);
    }

    public void OnPushNotificationOpened(DTDPushMessage pushMessage, DTDActionButton actionButton)
    {
        Debug.Log(pushMessage.ToString());
        Debug.Log(actionButton.ToString());
    }
}

public class NotificationExample : MonoBehaviour
{
    private const string APP_KEY = "***************"
    void Start()
    {
        DTDAnalytics.Initialize(APP_KEY);
#if UNITY_WSA
        DTDMessaging.WSA.SetPushListener(new PushListener());
        DTDMessaging.WSA.SetMessagingEnabling(true);
#endif
    }
}
```

### External interface of the DTDMessaging module

| Method                                                                                | Description                                                                                              |
| ------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
| **`void DTDMessaging.WSA.SetMessagingEnabling(bool value)`**                          | The method responsible for enabling or disabling of the push notification module                         |
| **`void DTDMessaging.WSA.GetMessagingEnabling(Action<bool> onGetMessagingEnabling)`** | The method that returns current state of the push notification module to onGetMessagingEnabling callback |
| **`void DTDMessaging.WSA.SetPushListener (IDTDPushListener pushListener)`**           | It sets a listener for push notification event trapping                                                  |

### Build a Windows Store App in Unity <a href="#build-a-windows-store-app-in-unity" id="build-a-windows-store-app-in-unity"></a>

Build a Windows Store App in Unity. After the app is built, a Visual Studio project will be created. Proceed with the following changes.

There is a difference in the implementation of the elements mentioned below for **different types of projects**:

#### IL2CPP + XAML

Put the following source in your App class (usually it is ***`App.xaml.cpp`*** file). Add several lines of code in a generated **`App.xaml.cpp`** class. After defining headers:

```csharp
//...headers

extern "C" __declspec(dllimport) void __stdcall AddActivatedEventArgs(IInspectable* activatedEventArgs);
```

And at the end of of the **`App::OnLaunched(LaunchActivatedEventArgs^ e)`** and **`App::OnActivated(IActivatedEventArgs^ args)`** functions.

For Example:

```csharp
void App::OnActivated(IActivatedEventArgs^ args) 
{
 //...other code
 AddActivatedEventArgs(reinterpret_cast<IInspectable*>(static_cast<Platform::Object^>(args)));
}

void App::OnLaunched(LaunchActivatedEventArgs^ e) 
{
 auto args = static_cast<IActivatedEventArgs^>(e);
 AddActivatedEventArgs(reinterpret_cast<IInspectable*>(static_cast<Platform::Object^>(args)));
}
```

#### **IL2CPP + D3D**

Put the following source in your App class (usually it is App.cpp file). Add several lines of code in a generated **App.cpp** class. After defining headers:

```csharp
//...headers
extern "C" __declspec(dllimport) void __stdcall AddActivatedEventArgs(IInspectable* activatedEventArgs);
```

```csharp
void App::OnActivated(CoreApplicationView^ sender, IActivatedEventArgs^ args) 
{
 //...other code
 AddActivatedEventArgs(reinterpret_cast<IInspectable*>(static_cast<Platform::Object^>(args)));
}
```

And at the end of of the **`App::OnActivated(CoreApplicationView^ sender, IActivatedEventArgs^ args`)** function.

Besides, in the UI editor of the ***`Package.appxmanifest`*** file you need to do the following:

1. Add Background Tasks in the Declarations tab and mark it as ***`System Event`***. After that, add **`DevToDev.Background.ToastNotificationBackgroundTask`** to the ***`Entry Point field`***.
2. Add Background Tasks in the Declarations tab and mark it as ***`Push Notification`***. After that, add **`DevToDev.Background.RawNotificationBackgroundTask`** to the ***`Entry Point field`***.

### Disabling

To disable notifications, call the following method:

```csharp
DTDMessaging.WSA.SetMessagingEnabling(false);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.devtodev.com/integration/integration-of-sdk-v2/push-notifications/unity/windows-uwp-wsa.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
