Windows (UWP)

WSA Integration

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.

  2. Add the DTDAnalytics initialization block after the DTDMessaging initialization block.

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

Attention! Use the #if UNITY_WSA define to surround any notification module code on the WSA platform.

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

Optional

Module status check

Use the following method to check current status:

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:

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:

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

MethodDescription

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

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:

//...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:

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:

//...headers
extern "C" __declspec(dllimport) void __stdcall AddActivatedEventArgs(IInspectable* activatedEventArgs);
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:

DTDMessaging.WSA.SetMessagingEnabling(false);

Last updated