App Center Push

App Center Push enables you to send push notifications to users of your app from the App Center portal. App Center Push relies on Windows Notification Services (WNS) to send notifications to the devices.

Note

The device displays a toast only if the application is in background at the moment the Push is received.

Add App Center Push to your app

If you haven't already set up and started the SDK in your application, see the Get started section of the docs.

Enable App Center Push

Note

To use the APIs presented below, you must add the following using statement to your .cs file:

using Microsoft.AppCenter.Unity.Push;

You also have to make sure that Use Push is checked in the settings of your App Center game object.

Register your app for Windows Notification Services (WNS)

Before you can send notification using WNS, your app must be registered with the Windows Store. This step will provide you with credentials for your app that App Center will use to authenticate with WNS. These credentials consist of a Package Security Identifier (SID) and an application secret key. To register, you need to associate your app with the Windows Store in Visual Studio.

To register the app, first export it as a project. To do so, build the app on the Unity Build Settings tab, then open the built solution in Visual Studio.

1. Windows Store Association in Visual Studio

  1. In Visual Studio Solution Explorer, right-click the UWP project, then select Store -> Associate App with the Store...

    uwp store association

  2. In the wizard, click Next, sign in with your Windows developer account, type a name for your app in Reserve a new app name, then click Reserve.

  3. After completing the app registration, select the new app name, click Next, and then click Associate. This adds the required Windows Store registration information to the application manifest.

Note

You must have a Windows Developer account to send push notifications to your UWP app. You can register a Microsoft developer account if you don't already have one.

2. Get Package SID and Application secret from the Application Registration Portal

  1. Navigate to the Microsoft Application Registration Portal, sign-in with your Microsoft account. Click the Windows Store app you associated in the previous step.

    uwp app registration

  2. In the registration page, make a note of the value under Application Secrets and the Package SID, which you will use to configure your App Center Push backend.

3. Configure Package SID and Security Key in App Center portal

  1. Login to App Center.
  2. Select the application in the left navigator, then select the Push section.
  3. Click Next button to get to the configuration page.
  4. Enter Package SID and Security key that you obtained from the Microsoft Application Registration Portal in the previous step.
  5. Click Apply changes.

Intercept push notifications

You can set up a listener to be notified whenever a push notification is received in foreground, or a background push notification has been clicked by the user.

Note

A notification is not generated when your application receives a push in the foreground.

Note

If the push is received in background, the event is NOT triggered at receive time. The event is triggered when you click on the notification.

Note

The background notification click callback does NOT expose title and message. Title and message are only available in foreground pushes.

To register the listener, see the following example:

Push.PushNotificationReceived += (sender, e) =>
{
    string customData = "";
    if (e.CustomData != null)
        foreach (var data in e.CustomData)
            customData += data.Key + " = " + data.Value + Environment.NewLine;

    // Message and title cannot be read from a background notification object.
    // Message being a mandatory field, you can use that to check foreground vs background.
    if (string.IsNullOrEmpty(e.Message))
    {
        // Use the custom data dictionary when a background push is clicked.
        Debug.Log("Background push notification received:" + Environment.NewLine +
                  "Custom data: " + customData);
    }
    else
    {
        // Display an alert for foreground push.
        Debug.Log("Foreground push notification received:" + Environment.NewLine +
                  "Notification title: " + e.Title + Environment.NewLine +
                  "Message: " + e.Message + Environment.NewLine +
                  "Custom data: " + customData);
    }
};

Enable or disable App Center Push at runtime

You can enable and disable App Center Push at runtime. If you disable it, the SDK stops updating the Google registration identifier used to push, but the existing one will continue working. Disabling App Center Push in the SDK will NOT stop your application from receiving push notifications.

Push.SetEnabledAsync(false);

To enable App Center Push again, use the same API but pass true as a parameter.

Push.SetEnabledAsync(true);

The state is persisted in the device's storage across application launches.

This API is asynchronous, you can read more about in our App Center Asynchronous APIs guide.

Check if App Center Push is enabled

You can also check if App Center Push is enabled or not.

bool isEnabled = await Push.IsEnabledAsync();

This API is asynchronous, you can read more about in our App Center Asynchronous APIs guide.