Skip to content

Latest commit

 

History

History
94 lines (73 loc) · 4.64 KB

app-service-mobile-dotnet-backend-configure-push-apns.md

File metadata and controls

94 lines (73 loc) · 4.64 KB
  • .NET backend (C#):

    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.

    2. In the backend's Visual Studio project, open Controllers > TodoItemController.cs. At the top of the file, add the following using statement:

       using Microsoft.Azure.Mobile.Server.Config;
       using Microsoft.Azure.NotificationHubs;
      
    3. Replace the PostTodoItem method with the following code:

       public async Task<IHttpActionResult> PostTodoItem(TodoItem item)
       {
           TodoItem current = await InsertAsync(item);
           // Get the settings for the server project.
           HttpConfiguration config = this.Configuration;
      
           MobileAppSettingsDictionary settings = 
               this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
      
           // Get the Notification Hubs credentials for the Mobile App.
           string notificationHubName = settings.NotificationHubName;
           string notificationHubConnection = settings
               .Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString;
      
           // Create a new Notification Hub client.
           NotificationHubClient hub = NotificationHubClient
           .CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
      
           // iOS payload
           var appleNotificationPayload = "{\"aps\":{\"alert\":\"" + item.Text + "\"}}";
      
           try
           {
               // Send the push notification and log the results.
               var result = await hub.SendAppleNativeNotificationAsync(appleNotificationPayload);
      
               // Write the success result to the logs.
               config.Services.GetTraceWriter().Info(result.State.ToString());
           }
           catch (System.Exception ex)
           {
               // Write the failure result to the logs.
               config.Services.GetTraceWriter()
                   .Error(ex.Message, null, "Push.SendAsync Error");
           }
           return CreatedAtRoute("Tables", new { id = current.Id }, current);
       }
      
    4. Republish the server project.

  • Node.js backend :

    1. If you haven't already done so, download the quickstart project or else use the online editor in the Azure portal.

    2. Replace the todoitem.js table script with the following code:

         var azureMobileApps = require('azure-mobile-apps'),
             promises = require('azure-mobile-apps/src/utilities/promises'),
             logger = require('azure-mobile-apps/src/logger');
      
         var table = azureMobileApps.table();
      
         // When adding record, send a push notification via APNS
         table.insert(function (context) {
             // For details of the Notification Hubs JavaScript SDK, 
             // see http://aka.ms/nodejshubs
             logger.info('Running TodoItem.insert');
      
             // Create a payload that contains the new item Text.
             var payload = "{\"aps\":{\"alert\":\"" + context.item.text + "\"}}";
      
             // Execute the insert; Push as a post-execute action when results are returned as a Promise.
             return context.execute()
                 .then(function (results) {
                     // Only do the push if configured
                     if (context.push) {
                         context.push.apns.send(null, payload, function (error) {
                             if (error) {
                                 logger.error('Error while sending push notification: ', error);
                             } else {
                                 logger.info('Push notification sent successfully!');
                             }
                         });
                     }
                     return results;
                 })
                 .catch(function (error) {
                     logger.error('Error while running context.execute: ', error);
                 });
         });
      
         module.exports = table;
      
    3. When editing the file on your local computer, republish the server project.