Mobile Center Push

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

Note

A toast is displayed only if the application is in background at the moment the Push is received.

Prerequisite - 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 will provide you with credentials for your app that Mobile Center will use to authenticate with WNS. These credentials consist of a Package Security Identifier (SID) and an application secret key. To perform this registeration, you need to associate your app with the Windows Store in Visual Studio.

1. Windows Store Association in Visual Studio

  1. In Visual Studio Solution Explorer, right-click the UWP project, click 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 the app registration is successfully created, 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 will need a Windows Developer account to send push notifications to your UWP app. You can register a developer account if you don't already have one.

2. Get Package SID and Application secret from 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 next use to configure your Mobile Center Push backend.

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

Navigate to your Mobile Center app -> Push, enter Package SID and Security key that you obtained from the Microsoft Application Registration Portal in the previous step, click Apply changes.

Add Mobile Center Push to your app

Please follow the Get started section if you haven't set up and started the SDK in your application.

1. Add the Mobile Center Push package

The Mobile Center SDK is designed with a modular approach – a developer only needs to integrate the modules of the services that they're interested in. It can be integrated using Visual Studio or Package Manager Console.

Visual Studio for Mac or Xamarin Studio

  • Under your project, select Packages, open context menu and click Add packages.
  • Search for Mobile Center, and select Mobile Center Push.
  • Click Add Packages.

Visual Studio for Windows

  • Navigate to the Project > Manage NuGet Packages...
  • Search for Mobile Center, then install Microsoft.Azure.Mobile.Push.

Package Manager Console

  • Type the following command in Package Manager Console:

    PM> Install-Package Microsoft.Azure.Mobile.Push

Note

If you use the Mobile Center SDK in a portable project (such as Xamarin.Forms), you need to install the packages in each of the projects: the portable, Android, iOS and UWP ones.

Now that you've integrated Mobile Center Push in your application, it's time to start the SDK and make use of Mobile Center.

2. Start Mobile Center Push service

In order to use Mobile Center, you need to opt in to the module(s) that you want to use, meaning by default no modules are started and you will have to explicitly call each of them when starting the SDK.

Add the following using statement to the top of the file you are referencing Push from:

using Microsoft.Azure.Mobile.Push

Next, add typeof(Push) to your MobileCenter.Start() method to start Mobile Center Push service.

MobileCenter.Start("{Your App Secret}", typeof(Push));

Make sure you have replaced {Your App Secret} in the code sample above with your app secret.

Note

If your UWP project is part of a Xamarin.Forms application, it is not necessary to add the call to MobileCenter.Start() in the UWP portion of the project. The method call can instead be made from the PCL or shared project portion of your Xamarin.Forms application.

Intercept push notifications

Mobile Center Push makes it possible to intercept push notifications but there is some setup required to enable this feature in UWP.

Additional setup

Call Push.CheckLaunchedFromNotification(e); after MobileCenter.Start in OnLaunched method.

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
    MobileCenter.Start("{Your App Secret}", typeof(Analytics), typeof(Push));
    Push.CheckLaunchedFromNotification(e);
}

Subscribe to the push event

You can subscribe to the Push.PushNotificationReceived event to be notified whenever a push notification is received in the foreground or a background push notification has been clicked by the user.

Note

No toast is shown when the push is received in 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

UWP toasts do NOT expose title and message to the background notification click callback. Title and message are only available in foreground pushes.

The following example demonstrates how to use the push event.

// This should come before MobileCenter.Start() is called
Push.PushNotificationReceived += (sender, e) => {

    // Add the notification message and title to the message
    var summary =  $"Push notification received:" +
                        $"\n\tNotification title: {e.Title}" +
                        $"\n\tMessage: {e.Message}";

    // If there is custom data associated with the notification,
    // print the entries
    if (e.CustomData != null)
    {
        summary += "\n\tCustom data:\n";
        foreach (var key in e.CustomData.Keys)
        {
            summary += $"\t\t{key} : {e.CustomData[key]}\n";
        }
    }

    // Send the notification summary to debug output
    System.Diagnostics.Debug.WriteLine(summary);
};

Enable or disable Mobile Center Push at runtime

You can enable and disable Mobile Center Push at runtime. If you disable it, the SDK will stop updating the WNS registration identifier that is used to push notifications, but the existing one will continue to work. In other words, disabling the Mobile Center Push in the SDK will NOT stop your application from receiving push notifications.

Push.SetEnabledAsync(false);

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

Push.SetEnabledAsync(true);

You don't need to await this call to make other API calls (such as IsEnabledAsync) consistent.

Check if Mobile Center Push is enabled

You can also check if Mobile Center Push is enabled or not:

bool isEnabled = await Push.IsEnabledAsync();