You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: hub/apps/develop/notifications/push-notifications/push-quickstart.md
+118-1Lines changed: 118 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -151,6 +151,9 @@ Open your **Package.appxmanifest**. Add the following inside the `<Application>`
151
151
</Package>
152
152
```
153
153
154
+
>[!NOTE]
155
+
> An example of the completed C++ classfor 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
+
154
157
### Step 3: Register for and respond to push notifications on app startup
155
158
156
159
Update your app's `main()` method to add the following:
@@ -377,4 +380,118 @@ int main()
377
380
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.
378
381
379
382
The tutorial code's console will look like this:
380
-
. You'll need the token to [send a push notification to your app](#send-a-push-notification-to-your-app).
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).
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
+
varclient=newRestClient("[Your channel URL. E.g. https://wns2-by3p.notify.windows.com/?token=AwYAAABa5cJ3...]");
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>
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)
0 commit comments