- SERVICE_NOTIFY_2A structure (winsvc.h)
- Syntax
- Members
- Remarks
- NotifyServiceStatusChangeA function (winsvc.h)
- Syntax
- Parameters
- Return value
- Remarks
- Windows Push Notification Services (WNS) overview
- How it works
- Registering your app and receiving the credentials for your cloud service
- Requesting a notification channel
- Important notes
- Authenticating your cloud service
- Important notes
- Sending a notification
- Important notes
- Expiration of tile and badge notifications
- Push notifications and battery saver
SERVICE_NOTIFY_2A structure (winsvc.h)
Represents service status notification information. It is used by the NotifyServiceStatusChangefunction.
Syntax
Members
The structure version. This member must be SERVICE_NOTIFY_STATUS_CHANGE (2).
A pointer to the callback function. For more information, see Remarks.
Any user-defined data to be passed to the callback function.
A value that indicates the notification status. If this member is ERROR_SUCCESS, the notification has succeeded and the ServiceStatus member contains valid information. If this member is ERROR_SERVICE_MARKED_FOR_DELETE, the service has been marked for deletion and the service handle used by NotifyServiceStatusChange must be closed.
A SERVICE_STATUS_PROCESS structure that contains the service status information. This member is only valid if dwNotificationStatus is ERROR_SUCCESS.
If dwNotificationStatus is ERROR_SUCCESS, this member contains a bitmask of the notifications that triggered this call to the callback function.
If dwNotificationStatus is ERROR_SUCCESS and the notification is SERVICE_NOTIFY_CREATED or SERVICE_NOTIFY_DELETED, this member is valid and it is a MULTI_SZ string that contains one or more service names. The names of the created services will have a ‘/’ prefix so you can distinguish them from the names of the deleted services.
If this member is valid, the notification callback function must free the string using the LocalFree function.
Remarks
The callback function is declared as follows:
The callback function receives a pointer to the SERVICE_NOTIFY structure provided by the caller.
The winsvc.h header defines SERVICE_NOTIFY_2 as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.
NotifyServiceStatusChangeA function (winsvc.h)
Enables an application to receive notification when the specified service is created or deleted or when its status changes.
Syntax
Parameters
A handle to the service or the service control manager. Handles to services are returned by the OpenService or CreateService function and must have the SERVICE_QUERY_STATUS access right. Handles to the service control manager are returned by the OpenSCManager function and must have the SC_MANAGER_ENUMERATE_SERVICE access right. For more information, see Service Security and Access Rights.
There can only be one outstanding notification request per service.
The type of status changes that should be reported. This parameter can be one or more of the following values.
Value | Meaning |
---|---|
SERVICE_NOTIFY_CREATED 0x00000080 | Report when the service has been created. The hService parameter must be a handle to the SCM. |
SERVICE_NOTIFY_CONTINUE_PENDING 0x00000010 | Report when the service is about to continue. The hService parameter must be a handle to the service. |
SERVICE_NOTIFY_DELETE_PENDING 0x00000200 | Report when an application has specified the service in a call to the DeleteService function. Your application should close any handles to the service so it can be deleted. The hService parameter must be a handle to the service. |
SERVICE_NOTIFY_DELETED 0x00000100 | Report when the service has been deleted. An application cannot receive this notification if it has an open handle to the service. The hService parameter must be a handle to the SCM. |
SERVICE_NOTIFY_PAUSE_PENDING 0x00000020 | Report when the service is pausing. The hService parameter must be a handle to the service. |
SERVICE_NOTIFY_PAUSED 0x00000040 | Report when the service has paused. The hService parameter must be a handle to the service. |
SERVICE_NOTIFY_RUNNING 0x00000008 | Report when the service is running. The hService parameter must be a handle to the service. |
SERVICE_NOTIFY_START_PENDING 0x00000002 | Report when the service is starting. The hService parameter must be a handle to the service. |
SERVICE_NOTIFY_STOP_PENDING 0x00000004 | Report when the service is stopping. The hService parameter must be a handle to the service. |
SERVICE_NOTIFY_STOPPED 0x00000001 | Report when the service has stopped. The hService parameter must be a handle to the service. A pointer to a SERVICE_NOTIFY structure that contains notification information, such as a pointer to the callback function. This structure must remain valid until the callback function is invoked or the calling thread cancels the notification request. Do not make multiple calls to NotifyServiceStatusChange with the same buffer parameter until the callback function from the first call has finished with the buffer or the first notification request has been canceled. Otherwise, there is no guarantee which version of the buffer the callback function will receive. WindowsВ Vista:В В The address of the callback function must be within the address range of a loaded module. Therefore, the callback function cannot be code that is generated at run time (such as managed code generated by the JIT compiler) or native code that is decompressed at run time. This restriction was removed in Windows ServerВ 2008 and WindowsВ Vista with SP1. Return valueIf the function succeeds, the return value is ERROR_SUCCESS. If the service has been marked for deletion, the return value is ERROR_SERVICE_MARKED_FOR_DELETE and the handle to the service must be closed. If service notification is lagging too far behind the system state, the function returns ERROR_SERVICE_NOTIFY_CLIENT_LAGGING. In this case, the client should close the handle to the SCM, open a new handle, and call this function again. If the function fails, the return value is one of the system error codes. RemarksThe NotifyServiceStatusChange function can be used to receive notifications about service applications. It cannot be used to receive notifications about driver services. When the service status changes, the system invokes the specified callback function as an asynchronous procedure call (APC) queued to the calling thread. The calling thread must enter an alertable wait (for example, by calling the SleepEx function) to receive notification. For more information, see Asynchronous Procedure Calls. If the service is already in any of the requested states when NotifyServiceStatusChange is called, the callback function is queued immediately. If the service state has not changed by the next time the function is called with the same service and state, the callback function is not queued immediately; the callback function is queued the next time the service enters the requested state. The NotifyServiceStatusChange function calls the OpenThread function on the calling thread with the THREAD_SET_CONTEXT access right. If the calling thread does not have this access right, NotifyServiceStatusChange fails. If the calling thread is impersonating another user, it may not have sufficient permission to set context. It is more efficient to call NotifyServiceStatusChange from a thread that performs a wait than to create an additional thread. After the callback function is invoked, the caller must call NotifyServiceStatusChange to receive additional notifications. Note that certain functions in the Windows API, including NotifyServiceStatusChange and other SCM functions, use remote procedure calls (RPC); these functions might perform an alertable wait operation, so they are not safe to call from within the callback function. Instead, the callback function should save the notification parameters and perform any additional work outside the callback. To cancel outstanding notifications, close the service handle using the CloseServiceHandle function. After CloseServiceHandle succeeds, no more notification APCs will be queued. If the calling thread exits without closing the service handle or waiting until the APC is generated, a memory leak can occur. The winsvc.h header defines NotifyServiceStatusChange as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes. Windows Push Notification Services (WNS) overviewThe Windows Push Notification Services (WNS) enable third-party developers to send toast, tile, badge, and raw updates from their own cloud service. This provides a mechanism to deliver new updates to your users in a power-efficient and dependable way. How it worksThe following diagram shows the complete data flow for sending a push notification. It involves these steps:
Registering your app and receiving the credentials for your cloud serviceBefore you can send notifications using WNS, your app must be registered with the Store Dashboard, as described here. Requesting a notification channelWhen an app that is capable of receiving push notifications runs, it must first request a notification channel through the CreatePushNotificationChannelForApplicationAsync. For a full discussion and example code, see How to request, create, and save a notification channel. This API returns a channel URI that is uniquely linked to the calling application and its tile, and through which all notification types can be sent. After the app has successfully created a channel URI, it sends it to its cloud service, together with any app-specific metadata that should be associated with this URI. Important notes
Authenticating your cloud serviceTo send a notification, the cloud service must be authenticated through WNS. The first step in this process occurs when you register your app with the Microsoft Store Dashboard. During the registration process, your app is given a Package security identifier (SID) and a secret key. This information is used by your cloud service to authenticate with WNS. The WNS authentication scheme is implemented using the client credentials profile from the OAuth 2.0 protocol. The cloud service authenticates with WNS by providing its credentials (Package SID and secret key). In return, it receives an access token. This access token allows a cloud service to send a notification. The token is required with every notification request sent to the WNS. At a high level, the information chain is as follows:
In the authentication with WNS, the cloud service submits an HTTP request over Secure Sockets Layer (SSL). The parameters are supplied in the «application/x-www-for-urlencoded» format. Supply your Package SID in the «client_id» field and your secret key in the «client_secret» field as shown in the following example. For syntax details, see the access token request reference. This is just an example, not cut-and-paste code that you can successfully use in your own code.В The WNS authenticates the cloud service and, if successful, sends a response of «200 OK». The access token is returned in the parameters included in the body of the HTTP response, using the «application/json» media type. After your service has received the access token, you are ready to send notifications. The following example shows a successful authentication response, including the access token. For syntax details, see Push notification service request and response headers. Important notes
Sending a notificationUsing the channel URI, the cloud service can send a notification whenever it has an update for the user. The access token described above can be reused for multiple notification requests; the cloud server is not required to request a new access token for every notification. If the access token has expired, the notification request will return an error. We recommended that you do not try to re-send your notification more than once if the access token is rejected. If you encounter this error, you will need to request a new access token and resend the notification. For the exact error code, see Push notification response codes. The cloud service makes an HTTP POST to the channel URI. This request must be made over SSL and contains the necessary headers and the notification payload. The authorization header must include the acquired access token for authorization. An example request is shown here. For syntax details, see Push notification response codes. For details on composing the notification payload, see Quickstart: Sending a push notification. The payload of a tile, toast, or badge push notification is supplied as XML content that adheres to their respective defined Adaptive tiles schema or Legacy tiles schema. The payload of a raw notification does not have a specified structure. It is strictly app-defined. WNS responds to indicate that the notification has been received and will be delivered at the next available opportunity. However, WNS does not provide end-to-end confirmation that your notification has been received by the device or application. This diagram illustrates the data flow: Important notes
Expiration of tile and badge notificationsBy default, tile and badge notifications expire three days after being downloaded. When a notification expires, the content is removed from the tile or queue and is no longer shown to the user. It’s a best practice to set an expiration (using a time that makes sense for your app) on all tile and badge notifications so that your tile’s content doesn’t persist longer than it is relevant. An explicit expiration time is essential for content with a defined lifespan. This also assures the removal of stale content if your cloud service stops sending notifications, or if the user disconnects from the network for an extended period. Your cloud service can set an expiration for each notification by setting the X-WNS-TTL HTTP header to specify the time (in seconds) that your notification will remain valid after it is sent. For more information, see Push notification service request and response headers. For example, during a stock market’s active trading day, you can set the expiration for a stock price update to twice that of your sending interval (such as one hour after receipt if you are sending notifications every half-hour). As another example, a news app might determine that one day is an appropriate expiration time for a daily news tile update. Push notifications and battery saverBattery saver extends battery life by limiting background activity on the device. WindowsВ 10 lets the user set battery saver to turn on automatically when the battery drops below a specified threshold. When battery saver is on, the receipt of push notifications is disabled to save energy. But there are a couple exceptions to this. The following WindowsВ 10 battery saver settings (found in the Settings app) allow your app to receive push notifications even when battery saver is on.
There is no way to check the state of these two settings, but you can check the state of battery saver. In WindowsВ 10, use the EnergySaverStatus property to check battery saver state. Your app can also use the EnergySaverStatusChanged event to listen for changes to battery saver. If your app depends heavily on push notifications, we recommend notifying users that they may not receive notifications while battery saver is on and to make it easy for them to adjust battery saver settings. Using the battery saver settings URI scheme in WindowsВ 10, ms-settings:batterysaver-settings , you can provide a convenient link to the Settings app. When notifying the user about battery saver settings, we recommend providing a way to suppress the message in the future. For example, the dontAskMeAgainBox checkbox in the following example persists the user’s preference in LocalSettings. Here’s an example of how to check whether battery saver is turned on in WindowsВ 10. This example notifies the user and launches the Settings app to battery saver settings. The dontAskAgainSetting lets the user suppress the message if they don’t want to be notified again. This is the XAML for the ContentDialog featured in this example. |