|
1 | 1 | ---
|
2 | 2 | ms.topic: "include"
|
3 | 3 | ---
|
4 |
| -* **.NET backend (C#)**: |
| 4 | +**.NET backend (C#)**: |
5 | 5 |
|
6 |
| - 1. In Visual Studio, right-click the server project and click **Manage NuGet Packages**, search for `Microsoft.Azure.NotificationHubs`, then click **Install**. This installs the Notification Hubs library for sending notifications from your backend. |
7 |
| - 2. In the backend's Visual Studio project, open **Controllers** > **TodoItemController.cs**. At the top of the file, add the following `using` statement: |
| 6 | +1. In Visual Studio, right-click the server project and click **Manage NuGet Packages**, search for `Microsoft.Azure.NotificationHubs`, then click **Install**. This installs the Notification Hubs library for sending notifications from your backend. |
| 7 | +2. In the backend's Visual Studio project, open **Controllers** > **TodoItemController.cs**. At the top of the file, add the following `using` statement: |
8 | 8 |
|
9 | 9 | ```csharp
|
10 |
| - using Microsoft.Azure.Mobile.Server.Config; |
11 |
| - using Microsoft.Azure.NotificationHubs; |
| 10 | + using Microsoft.Azure.Mobile.Server.Config; |
| 11 | + using Microsoft.Azure.NotificationHubs; |
12 | 12 | ```
|
13 | 13 |
|
14 |
| - 3. Replace the `PostTodoItem` method with the following code: |
| 14 | +3. Replace the `PostTodoItem` method with the following code: |
15 | 15 |
|
16 | 16 | ```csharp
|
17 |
| - public async Task<IHttpActionResult> PostTodoItem(TodoItem item) |
| 17 | + public async Task<IHttpActionResult> PostTodoItem(TodoItem item) |
| 18 | + { |
| 19 | + TodoItem current = await InsertAsync(item); |
| 20 | + // Get the settings for the server project. |
| 21 | + HttpConfiguration config = this.Configuration; |
| 22 | + |
| 23 | + MobileAppSettingsDictionary settings = |
| 24 | + this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); |
| 25 | + |
| 26 | + // Get the Notification Hubs credentials for the Mobile App. |
| 27 | + string notificationHubName = settings.NotificationHubName; |
| 28 | + string notificationHubConnection = settings |
| 29 | + .Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString; |
| 30 | + |
| 31 | + // Create a new Notification Hub client. |
| 32 | + NotificationHubClient hub = NotificationHubClient |
| 33 | + .CreateClientFromConnectionString(notificationHubConnection, notificationHubName); |
| 34 | + |
| 35 | + // iOS payload |
| 36 | + var appleNotificationPayload = "{\"aps\":{\"alert\":\"" + item.Text + "\"}}"; |
| 37 | + |
| 38 | + try |
18 | 39 | {
|
19 |
| - TodoItem current = await InsertAsync(item); |
20 |
| - // Get the settings for the server project. |
21 |
| - HttpConfiguration config = this.Configuration; |
22 |
| - |
23 |
| - MobileAppSettingsDictionary settings = |
24 |
| - this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings(); |
25 |
| - |
26 |
| - // Get the Notification Hubs credentials for the Mobile App. |
27 |
| - string notificationHubName = settings.NotificationHubName; |
28 |
| - string notificationHubConnection = settings |
29 |
| - .Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString; |
30 |
| - |
31 |
| - // Create a new Notification Hub client. |
32 |
| - NotificationHubClient hub = NotificationHubClient |
33 |
| - .CreateClientFromConnectionString(notificationHubConnection, notificationHubName); |
34 |
| - |
35 |
| - // iOS payload |
36 |
| - var appleNotificationPayload = "{\"aps\":{\"alert\":\"" + item.Text + "\"}}"; |
37 |
| - |
38 |
| - try |
39 |
| - { |
40 |
| - // Send the push notification and log the results. |
41 |
| - var result = await hub.SendAppleNativeNotificationAsync(appleNotificationPayload); |
42 |
| - |
43 |
| - // Write the success result to the logs. |
44 |
| - config.Services.GetTraceWriter().Info(result.State.ToString()); |
45 |
| - } |
46 |
| - catch (System.Exception ex) |
47 |
| - { |
48 |
| - // Write the failure result to the logs. |
49 |
| - config.Services.GetTraceWriter() |
50 |
| - .Error(ex.Message, null, "Push.SendAsync Error"); |
51 |
| - } |
52 |
| - return CreatedAtRoute("Tables", new { id = current.Id }, current); |
| 40 | + // Send the push notification and log the results. |
| 41 | + var result = await hub.SendAppleNativeNotificationAsync(appleNotificationPayload); |
| 42 | + |
| 43 | + // Write the success result to the logs. |
| 44 | + config.Services.GetTraceWriter().Info(result.State.ToString()); |
| 45 | + } |
| 46 | + catch (System.Exception ex) |
| 47 | + { |
| 48 | + // Write the failure result to the logs. |
| 49 | + config.Services.GetTraceWriter() |
| 50 | + .Error(ex.Message, null, "Push.SendAsync Error"); |
53 | 51 | }
|
| 52 | + return CreatedAtRoute("Tables", new { id = current.Id }, current); |
| 53 | + } |
54 | 54 | ```
|
55 | 55 |
|
56 |
| - 4. Republish the server project. |
| 56 | +4. Republish the server project. |
| 57 | + |
| 58 | +**Node.js backend**: |
| 59 | + |
| 60 | +1. If you haven't already done so, [download the quickstart project](../articles/app-service-mobile/app-service-mobile-node-backend-how-to-use-server-sdk.md#download-quickstart) or else use the [online editor in the Azure portal](../articles/app-service-mobile/app-service-mobile-node-backend-how-to-use-server-sdk.md#online-editor). |
| 61 | + |
| 62 | +2. Replace the todoitem.js table script with the following code: |
| 63 | + |
| 64 | + ```javascript |
| 65 | + var azureMobileApps = require('azure-mobile-apps'), |
| 66 | + promises = require('azure-mobile-apps/src/utilities/promises'), |
| 67 | + logger = require('azure-mobile-apps/src/logger'); |
| 68 | + |
| 69 | + var table = azureMobileApps.table(); |
| 70 | + |
| 71 | + // When adding record, send a push notification via APNS |
| 72 | + table.insert(function (context) { |
| 73 | + // For details of the Notification Hubs JavaScript SDK, |
| 74 | + // see http://aka.ms/nodejshubs |
| 75 | + logger.info('Running TodoItem.insert'); |
| 76 | + |
| 77 | + // Create a payload that contains the new item Text. |
| 78 | + var payload = "{\"aps\":{\"alert\":\"" + context.item.text + "\"}}"; |
| 79 | + |
| 80 | + // Execute the insert; Push as a post-execute action when results are returned as a Promise. |
| 81 | + return context.execute() |
| 82 | + .then(function (results) { |
| 83 | + // Only do the push if configured |
| 84 | + if (context.push) { |
| 85 | + context.push.apns.send(null, payload, function (error) { |
| 86 | + if (error) { |
| 87 | + logger.error('Error while sending push notification: ', error); |
| 88 | + } else { |
| 89 | + logger.info('Push notification sent successfully!'); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + return results; |
| 94 | + }) |
| 95 | + .catch(function (error) { |
| 96 | + logger.error('Error while running context.execute: ', error); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + module.exports = table; |
| 101 | + ``` |
57 | 102 |
|
58 |
| -* **Node.js backend**: |
59 |
| - |
60 |
| - 1. If you haven't already done so, [download the quickstart project](../articles/app-service-mobile/app-service-mobile-node-backend-how-to-use-server-sdk.md#download-quickstart) or else use the [online editor in the Azure portal](../articles/app-service-mobile/app-service-mobile-node-backend-how-to-use-server-sdk.md#online-editor). |
61 |
| - |
62 |
| - 2. Replace the todoitem.js table script with the following code: |
63 |
| - |
64 |
| - ```javascript |
65 |
| - var azureMobileApps = require('azure-mobile-apps'), |
66 |
| - promises = require('azure-mobile-apps/src/utilities/promises'), |
67 |
| - logger = require('azure-mobile-apps/src/logger'); |
68 |
| - |
69 |
| - var table = azureMobileApps.table(); |
70 |
| - |
71 |
| - // When adding record, send a push notification via APNS |
72 |
| - table.insert(function (context) { |
73 |
| - // For details of the Notification Hubs JavaScript SDK, |
74 |
| - // see http://aka.ms/nodejshubs |
75 |
| - logger.info('Running TodoItem.insert'); |
76 |
| - |
77 |
| - // Create a payload that contains the new item Text. |
78 |
| - var payload = "{\"aps\":{\"alert\":\"" + context.item.text + "\"}}"; |
79 |
| - |
80 |
| - // Execute the insert; Push as a post-execute action when results are returned as a Promise. |
81 |
| - return context.execute() |
82 |
| - .then(function (results) { |
83 |
| - // Only do the push if configured |
84 |
| - if (context.push) { |
85 |
| - context.push.apns.send(null, payload, function (error) { |
86 |
| - if (error) { |
87 |
| - logger.error('Error while sending push notification: ', error); |
88 |
| - } else { |
89 |
| - logger.info('Push notification sent successfully!'); |
90 |
| - } |
91 |
| - }); |
92 |
| - } |
93 |
| - return results; |
94 |
| - }) |
95 |
| - .catch(function (error) { |
96 |
| - logger.error('Error while running context.execute: ', error); |
97 |
| - }); |
98 |
| - }); |
99 |
| - |
100 |
| - module.exports = table; |
101 |
| - ``` |
102 |
| - |
103 |
| - 2. When editing the file on your local computer, republish the server project. |
| 103 | +3. When editing the file on your local computer, republish the server project. |
0 commit comments