Dela via


How to manage metered network cost constraints (HTML)

This topic details best practices for maintaining awareness of network connection cost or data plan status changes and enable your app to use this information to avoid incurring additional costs for roaming or exceeding a specified data transfer limit using classes in the Windows.Networking.Connectivity namespace.

Prerequisites

The following examples use JavaScript and are based on the Network information sample. For general help creating a Windows Runtime app using JavaScript, see Create your first Windows Runtime app using JavaScript.

Knowing what a ConnectionProfile is and how to access the information that it represents is important. For more information, see Hot to retrieve network connection information. For additional code examples, download the Network information sample.

What are metered networks?

A metered network is any network that imposes a cost to the user for a specified amount of data usage or while roaming. Most mobile network providers have placed caps on the amount of data transferred per month, per user, and exceeding the data cap (or transferring data while roaming) can result in significant charges to the user. With the growing popularity of mobile broadband technologies, increasing demand for data consumption will increase the impact of this issue, leading to a more variable set of data plans. As a result, Windows Runtime apps need to be aware of such network limitations and adapt its behavior as conditions change.

Windows 8, Windows Server 2012, and Windows Phone 8.1 and later categorize metered networks as allowing unrestricted, fixed, or variable cost limits. The NetworkCostType enumeration defines these values. If a user goes over the cap specified by the data plan or are roaming, most networks implement a steep charge either financially, or by blocking/throttling access.

Most mobile network users will move between various networks, metered and otherwise, throughout the course of a day. A 3G/4G network might be used for network connectivity while away from home or the office. When at home or the office, one or more Wi-Fi networks are often available; similarly, those same Wi-Fi networks will eventually be unavailable when the user leaves home. There may also be times when no network is available. Given the proliferation of Wi-Fi and mobile broadband networks, such network change scenarios are increasingly common.

Who is impacted?

Improper use of metered network bandwidth can influence the reputation of your app and organization. In general, all Windows Runtime apps (especially those that transfer large amounts of data) need to follow the guidance below.

General guidance

Your app can be made aware of connection changes, in this case cost or data plan changes, by registering for connection change events. Once notified of a change, your app can access the indicated ConnectionProfile for current information before a decision is made to adapt app behavior. For instructions on this process, see How to manage network connection events and changes in availability.

Specific to metered network scenarios, the following ConnectionCost properties can be used to gather information and identify current data plan settings for a specific connection:

Property Description

ApproachingDataLimit

Gets a value that indicates if a connection is approaching the data usage allowance specified by the data plan, which is defined by the carrier. This limitation is most commonly referred to as a data cap.

NetworkCostType

Gets a value that indicates the current network cost for a connection.

  • Unrestricted: Use of this network connection is unlimited. It has no usage charges and capacity constraints.
  • Fixed: Use of this network connection is unrestricted up to a data limit set by the carrier.
  • Variable: Use of this network connection is metered on a per-byte basis.
  • Unknown: Cost information is not available for this network connection.

OverDataLimit

Gets a value that indicates if the connection has exceeded the data usage allowance specified by the data plan, which is defined by the carrier.

Roaming

Gets a value that indicates whether the connection is to a network outside of the home provider.

 

Apps that use metered networks to connect to destinations on the Internet can obtain cost information directly by calling GetInternetConnectionProfile and accessing the associated cost properties. By using this information, an app can follow these guidelines to decide how best to use network resources:

Behavior App Guidelines Example

Normal

If the NetworkCostType is Unrestricted or Unknown and the ConnectionCost is not Roaming, then your app should implement Normal behavior.

In Normal scenarios, your app does not implement restrictions. Apps treat the connection as Unlimited in cost, and Unrestricted by usage charges and capacity constraints.

Examples:

  • Play an entire HD movie.
  • Download a large file without restrictions or UI prompts.
  • Media player app plays an entire HD movie.
  • App downloads a large file without any restrictions or prompts.

Conservative

If the NetworkCostType is Fixed or Variable, and the ConnectionCost is not Roaming or OverDataLimit, then the app should implement Conservative behavior.

In conservative scenarios, the app implements restrictions for optimizing network usage to handle transfers over metered networks.

Examples:

  • Play movies in lower resolutions.
  • Delay non-critical downloads.
  • Avoid pre-fetching of information over a network.
  • Switch to a header-only mode when retrieving email messages.
  • Media player app plays movies in lower resolutions.
  • App delays non-critical downloads.

Opt-In

If the ConnectionCost is Roaming or OverDataLimit, your app should implement Opt-In behavior.

For opt-in scenarios, your app handles cases where the network access cost is significantly higher than the plan cost. For example, when a user is roaming, a mobile carrier may charge a higher rate data usage.

Examples:

  • Prompt the user before accessing the network.
  • Suspend all background data network activities.
  • App prompts user to access the network.
  • App suspends all background data network activities.

 

This code example checks the connection cost and returns suggestions for appropriate app behavior.

var CostGuidance = { Normal: 0, Conservative: 1, OptIn: 2 };
// GetCostGuidance returns an object with a Cost (with value of CostGuidance), 
// CostName (a string) and Reason, which says why the cost is what it is.

function GetCostGuidance() 
{
    var connectionCost = Windows.Networking.Connectivity.NetworkInformation.getInternetConnectionProfile().getConnectionCost();
    var networkCostConstants = Windows.Networking.Connectivity.NetworkCostType;
    var Retval = new Object();
    if (connectionCost.roaming || connectionCost.overDataLimit)
    {
        Retval.Cost = CostGuidance.OptIn;
        Retval.CostName = "OptIn";
        Retval.Reason = connectionCost.roaming
            ? "Connection is roaming; using the connection may result in additional charge."
            : "Connection has exceeded the usage cap limit.";
    }
    else if (connectionCost.networkCostType == networkCostConstants.fixed
        || connectionCost.networkCostType == networkCostConstants.variable)
    {
        Retval.Cost = CostGuidance.conservative;
        Retval.CostName = "Conservative";
        Retval.Reason = connectionCost.networkCostType == NetworkCostType.fixed
            ? "Connection has limited allowed usage."
            : "Connection is charged based on usage. ";
    }
    else
    {
        Retval.Cost = CostGuidance.Normal;
        Retval.CostName = "Normal";
        Retval.Reason = connectionCost.networkCostType == networkCostConstants.unknown
            ? "Connection is unknown"
            : "Connection cost is unrestricted";
    }

    return Retval;
}

Behavior differences between Windows Store apps and Windows Phone Store apps

Some mobile broadband providers do not set the cost information for a user's plan in the provisioned profile for mobile broadband. The user is responsible for entering this cost information on Windows and Windows Phone.

Cost information is reported differently for mobile broadband networks. On Windows 8.1, if the cost information is not set by the user for mobile broadband, NetworkCostType will be reported as Fixed cost. On Windows Phone 8.1, if the cost information for the mobile broadband (cellular data) is not set, it will reported as Unknown. This behavior difference affects the general guidance above for how to check the connection cost and returns suggestions for appropriate app behavior. Using the above guidance Windows 8.1 would suggest Conservative behavior and Windows Phone 8.1 would suggest Normal behavior. Both platforms should suggest Conservative behavior. As a result, the general guidance for appropriate behavior should be revised on Windows Phone for mobile broadband profiles.

Handling maximum transfer size

Transferring large files over a metered network can result in significant, unexpected costs for the user. Apps that synchronize, upload or download files when on a metered network can do so without user permission only if the file size is less than the Max Transfer size specified by the carrier.

To transfer large amounts of data, apps should obtain user permission or wait for access to an unrestricted network. Optionally, the application can allow the user to bypass future user permission messages.

The following example code demonstrates how to retrieve the maximum transfer size for a connection:

var dataPlanStatus = InternetProfile.getDataPlanStatus();

if (dataPlanStatus.maxTransferSizeInMegabytes !== null) {
  
    mySample.displayStatus("Maximum Transfer Size in Megabytes: " + dataPlanStatus.maxTransferSizeInMegabytes);
}
else{

    mySample.displayError("Maximum Transfer Size in Megabytes: " + "Not Defined");
}

Summary

In this topic we reviewed metered network scenarios and how best to prevent your connected app from incurring additional charges on behalf of the end-user.

While this covers metered network scenarios, a NetworkStatusChanged event can also represent changes to the availability of a connection. For more information and guidance on how best to engage these scenarios, see How to manage network connection events and changes in availability.

Other

Create your first Windows Runtime app using JavaScript

How to handle exceptions in network apps

How to manage network connection events and changes in availability

How to retrieve network adapter and locality information

How to retrieve network connection information

How to retrieve network connection usage data

Reference

ConnectionCost

ConnectionProfile

DataPlanStatus

NetworkCostType

NetworkInformation

Windows.Networking.Connectivity

Samples

Network information sample

Network status background sample