App Center Push

App Center Push enables you to send push notifications to users of your app from the App Center portal.

Prerequisite - Enable Apple Push Notifications service (APNs) for your app

Configure Apple Push Notifications service (APNs) for your app from your Apple developer account and the App Center portal before adding App Center Push to your app.

Enable push notifications on your application

Set up APNs

Log in to the App Center portal, select your application, click on the Push button from the left menu then click Next to reveal the push notification settings UI:

app-center-push-settings

  • On the bottom of the page, select Sandbox for initial development or Production for production version of your application.

  • Collect the following information:

    1. Prefix and ID

      • Go to your Apple developer account and select your application from the App ID list in Identifiers.

      • Copy the Prefix value from this window and paste it to the App Center push settings.

      • Do the same with the ID value.

      apple-dev-center-app-id

    Note

    As per guidance from the Apple Developer documentation, all new app developers who joined after 2011 will have the Team ID same as the App Prefix ID. If you have joined before, you will need to get the Team ID instead.

    1. Key ID

      • In your Apple developer account create a new key in Certificates, Identifiers & Profiles/Keys.

      • Make sure to check the APNs checkbox.

      • Fill in the key name

      • Press Continue then Confirm.

      apple-dev-center-new-auth-key

      • On the next screen, copy the Key ID value and paste it to the App Center push settings.

      • Download the key file.

      apple-dev-center-confirm-auth-key

    2. Push Token

      • Open your key file with a text editor and copy the authentication token it contains.

      auth-key-file

      • On the App Center push settings, paste this token to the Push Token field then click Done to complete this configuration.

For more information, see to the Apple documentation.

Add App Center Push to your app

If you haven't set up the SDK in your application already, see instructions in the Unity 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.

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.