# Windows (UWP)

## Push Notifications (UWP)

The **`DevToDev.Messaging`** package necessary for notifications is available in the **`NuGet`** package manager.

### 1. NuGet Installation

{% embed url="<https://www.nuget.org/packages/DevToDev.Analytics.Uwp/>" %}

{% embed url="<https://www.nuget.org/packages/DevToDev.Messaging.Uwp/>" %}

**Package Manager UI**

Find the **`DevToDev.Messaging`** package using the package manager search engine and click **Install**. The latest version of the package is recommended.

### 2. Credentials

To integrate WNS, you need to get the Package SID and Application Secret Key from the [Microsoft Partner Center](https://partner.microsoft.com/en-us/dashboard/home) and specify them in the devtodev push notification settings (Project -> Settings-> Push notifications):

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2Fv4qcI4VDycCr0oemTIEn%2Fimage.png?alt=media\&token=0c0b6e45-4549-4900-ae0a-e2f8d7b46336)

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FpMSAcmASfoCjZIRaHxRs%2Fimage.png?alt=media\&token=68f9b601-3d77-48cc-926b-c50ae45e0bda)

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2F1EJXDpH0FDRIHmT9oUfR%2Fimage.png?alt=media\&token=6efb9220-8ca0-46c3-a3b4-e6f05116e633)

![](https://2105883905-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LnGcP_ZeRJ1ipj9O8dF%2Fuploads%2FwuJoD1fRvCZUw80oaTQC%2Fimage.png?alt=media\&token=19609975-fc21-4fca-8d57-ab602a94ea82)

### 3. Setup

For the correct package functioning, add handlers invoke to the **`Windows.UI.Xaml.Application`** class implementation.

```csharp
sealed partial class App : Application
{
  protected override void OnLaunched(LaunchActivatedEventArgs e)
  {
    // Your code...
    DTDMessaging.HandleActivatedEvent(e);
  }
  
  protected override void OnActivated(IActivatedEventArgs args)
  {
      // Your code...
      DTDMessaging.HandleActivatedEvent(args);
  }
}
```

Besides, in the UI editor of the ***Package.appxmanifest*** file do the following:

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

### 4. Initialization

For the **`DevToDev.Messaging`** package functioning you need to have the main **`DevToDev.Analytics`** package installed. Initialize the SDK before initializing messages. You can read about it in more detail in the [SDK initialization section](https://docs.devtodev.com/integration/sdk-integration/windows#id-2.-sdk-initialization).

After the SDK has been initialized you can move to initializing messages. To do this, call the method:

```csharp
DTDMessaging.SetMessagingEnabling(true);
```

### 5. Events

It is possible to listen to events from the **`DevToDev.Messaging`** package:

1\. Push Token - a string that allows to identify the client on a remote server for sending him customized notifications. For listening the unique Push Token ID issue event, it is necessary to be subscribed to the event:

```csharp
DTDMessaging.OnTokenReceived += token => { /* Your code... */ };
```

2\. To track the errors related to the unique Push Token ID issue, it is necessary to be subscribed to the event:

```csharp
DTDMessaging.OnTokenFailed += error => { /* Your code... */ };
```

Where error is a string value containing information about the error.

3\. To handle incoming message data, it is necessary to be subscribed to the following event:

```csharp
DTDMessaging.OnMessageReceived += messageData => { /* Your code... */ };
```

Where **`messageData`** belongs to the **`IDictionary<string, string>`** type and contains data sent from the server together with the message.

4\. To handle notification activation events, it is necessary to be subscribed to the event:

```csharp
DTDMessaging.OnMessageActivated += messageAction => { /* Your code with token. */ };
```

Where **`messageAction`** is **`DevToDev.Messaging.DTDMessageAction`** class instance:

```csharp
/// <summary>
/// The class contains information about notification's action.
/// </summary>
public sealed class DTDMessageAction
{
    /// <summary>
    /// Action type.
    /// Can be: Open, Url, Share, DeepLink.
    /// </summary>
    public DTDMessageActionType ActionType { get; }

    /// <summary>
    /// Action string.
    /// </summary>
    public string ActionString { get; }

    /// <summary>
    /// Activated button ID.
    /// </summary>
    public string ButtonId { get; }

    /// <summary>
    /// Activated button's text.
    /// </summary>
    public string ButtonText { get; }

    /// <summary>
    /// Notification data.
    /// </summary>
    public IReadOnlyDictionary<string, string> MessageData { get; }
  }
```

### **6.** Disabling

Call the method to turn notifications off:

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