Skip to content

Commit 3c85ed9

Browse files
committed
Add back sending a push notification instructions
1 parent 3f59a33 commit 3c85ed9

1 file changed

Lines changed: 118 additions & 1 deletion

File tree

hub/apps/develop/notifications/push-notifications/push-quickstart.md

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ Open your **Package.appxmanifest**. Add the following inside the `<Application>`
151151
</Package>
152152
```
153153

154+
>[!NOTE]
155+
> An example of the completed C++ class for the sample can be found [after Step 4](#sample-code). Steps 3 and 4 provide step-by-step guidance to add each piece in the final sample.
156+
154157
### Step 3: Register for and respond to push notifications on app startup
155158

156159
Update your app's `main()` method to add the following:
@@ -377,4 +380,118 @@ int main()
377380
Use Visual Studio to build and install your app. Right-click on the solution file in the Solution Explorer and select **Deploy**. Visual Studio will build your app and install it on your machine. You can run the app by launching it via the Start Menu or the Visual Studio debugger.
378381

379382
The tutorial code's console will look like this:
380-
![working sample console](images/console_complete.png). You'll need the token to [send a push notification to your app](#send-a-push-notification-to-your-app).
383+
384+
![working sample console](images/console_complete.png)
385+
386+
You'll need the token to [send a push notification to your app](#send-a-push-notification-to-your-app).
387+
388+
## Send a push notification to your app
389+
390+
At this point, all configuration is complete and the WNS server can send push notifications to client apps. In the following steps, refer to the [Push notification server request and response headers](push-request-response-headers.md) for more detail.
391+
392+
### Step 1: Request an access token
393+
394+
To send a push notification, the WNS server first needs to request an access token. Send an HTTP POST request with your Azure TenantId, Azure AppId, and secret. For information on retrieving the Azure TenantId and Azure AppId, see [Get tenant and app ID values for signing in](/azure/active-directory/develop/howto-create-service-principal-portal#get-tenant-and-app-id-values-for-signing-in).
395+
396+
HTTP Sample Request:
397+
398+
```HTTP
399+
POST /{tenantID}/oauth2/v2.0/token Http/1.1
400+
Host: login.microsoftonline.com
401+
Content-Type: application/x-www-form-urlencoded
402+
Content-Length: 160
403+
404+
grant_type=client_credentials&client_id=<Azure_App_Registration_AppId_Here>&client_secret=<Azure_App_Registration_Secret_Here>&scope=https://wns.windows.com/.default/
405+
```
406+
407+
C# Sample Request:
408+
409+
```csharp
410+
//Sample C# Access token request
411+
var client = new RestClient("https://login.microsoftonline.com/{tenantID}/oauth2/v2.0");
412+
var request = new RestRequest("/token", Method.Post);
413+
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
414+
request.AddParameter("grant_type", "client_credentials");
415+
request.AddParameter("client_id", "[Your app's Azure AppId]");
416+
request.AddParameter("client_secret", "[Your app's secret]");
417+
request.AddParameter("scope", "https://wns.windows.com/.default");
418+
RestResponse response = await client.ExecutePostAsync(request);
419+
Console.WriteLine(response.Content);
420+
```
421+
422+
If your request is successful, you will receive a response that contains your token in the **access_token** field.
423+
424+
```json
425+
{
426+
"token_type":"Bearer",
427+
"expires_in":"86399",
428+
"ext_expires_in":"86399",
429+
"expires_on":"1653771789",
430+
"not_before":"1653685089",
431+
"access_token":"[your access token]"
432+
}
433+
```
434+
435+
### Step 2. Send a raw notification
436+
437+
Create an HTTP POST request that contains the access token you obtained in the previous step and the content of the push notification you want to send. The content of the push notification will be delivered to the app.
438+
439+
```http
440+
POST /?token=[The token query string parameter from your channel URL. E.g. AwYAAABa5cJ3...] HTTP/1.1
441+
Host: dm3p.notify.windows.com
442+
Content-Type: application/octet-stream
443+
X-WNS-Type: wns/raw
444+
Authorization: Bearer [your access token]
445+
Content-Length: 46
446+
447+
{ Sync: "Hello from the Contoso App Service" }
448+
```
449+
450+
```csharp
451+
var client = new RestClient("[Your channel URL. E.g. https://wns2-by3p.notify.windows.com/?token=AwYAAABa5cJ3...]");
452+
var request = new RestRequest();
453+
request.Method = Method.Post;
454+
request.AddHeader("Content-Type", "application/octet-stream");
455+
request.AddHeader("X-WNS-Type", "wns/raw");
456+
request.AddHeader("Authorization", "Bearer [your access token]");
457+
request.AddBody("Notification body");
458+
RestResponse response = await client.ExecutePostAsync(request);");
459+
```
460+
461+
### Step 3: Send a cloud-sourced app notification
462+
463+
If you are only interested in sending raw notifications, disregard this step. To send a cloud-sourced app notification, also known a push toast notification, first follow [Quickstart: App notifications in the Windows App SDK](../../../windows-app-sdk/notifications/app-notifications/app-notifications-quickstart.md). App notifications can either be push (sent from the cloud) or sent locally. Sending a cloud-sourced app notification is similar to sending a raw notification in **Step 2**, except the *X-WNS-Type* header is `toast`, *Content-Type* is `text/xml`, and the content contains the app notification XML payload. See [Notifications XML schema](/uwp/schemas/tiles/toastschema/schema-root) for more on how to construct your XML payload.
464+
465+
Create an HTTP POST request that contains your access token and the content of the cloud-sourced app notification you want to send. The content of the push notification will be delivered to the app.
466+
467+
```http
468+
POST /?token=AwYAAAB%2fQAhYEiAESPobjHzQcwGCTjHu%2f%2fP3CCNDcyfyvgbK5xD3kztniW%2bjba1b3aSSun58SA326GMxuzZooJYwtpgzL9AusPDES2alyQ8CHvW94cO5VuxxLDVzrSzdO1ZVgm%2bNSB9BAzOASvHqkMHQhsDy HTTP/1.1
469+
Host: dm3p.notify.windows.com
470+
Content-Type: text/xml
471+
X-WNS-Type: wns/toast
472+
Authorization: Bearer [your access token]
473+
Content-Length: 180
474+
475+
<toast><visual><binding template="ToastGeneric"><text>Example cloud toast notification</text><text>This is an example cloud notification using XML</text></binding></visual></toast>
476+
```
477+
478+
```csharp
479+
var client = new RestClient("https://dm3p.notify.windows.com/?token=AwYAAAB%2fQAhYEiAESPobjHzQcwGCTjHu%2f%2fP3CCNDcyfyvgbK5xD3kztniW%2bjba1b3aSSun58SA326GMxuzZooJYwtpgzL9AusPDES2alyQ8CHvW94cO5VuxxLDVzrSzdO1ZVgm%2bNSB9BAzOASvHqkMHQhsDy");
480+
client.Timeout = -1;
481+
482+
var request = new RestRequest(Method.POST);
483+
request.AddHeader("Content-Type", "text/xml");
484+
request.AddHeader("X-WNS-Type", "wns/toast");
485+
request.AddHeader("Authorization", "Bearer <AccessToken>");
486+
request.AddParameter("text/xml", "<toast><visual><binding template=\"ToastGeneric\"><text>Example cloud toast notification</text><text>This is an example cloud notification using XML</text></binding></visual></toast>", ParameterType.RequestBody);
487+
Console.WriteLine(response.Content);
488+
```
489+
490+
## Resources
491+
492+
- [Windows Push Notification Service (WNS)](https://aka.ms/wns)
493+
- [Push notifications sample code on GitHub](https://github.com/microsoft/WindowsAppSDK-Samples/tree/main/Samples/Notifications/Push/)
494+
- [Microsoft.Windows.PushNotifications API details](https://github.com/microsoft/WindowsAppSDK/blob/main/specs/PushNotifications/PushNotifications-spec.md#api-details)
495+
- [Push notifications spec on GitHub](https://github.com/microsoft/WindowsAppSDK/blob/main/specs/PushNotifications/PushNotifications-spec.md)
496+
- [Toast content](../app-notifications/adaptive-interactive-toasts.md)
497+
- [Notifications XML schema](/uwp/schemas/tiles/toastschema/schema-root)

0 commit comments

Comments
 (0)