Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Csharp classes demonstrating how to send push notifications to a specific registration using tag expressions in Azure Mobile Apps
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.OData;
using System.Web.Http.Tracing;
using Microsoft.Azure.Mobile.Server;
using App.Models;
using App.Notification;
namespace Controllers
{
public class NotificationController : TableController<Notification>
{
private ITraceWriter traceWriter;
private NotificationSender notificationSender;
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
AppContext context = new AppContext();
DomainManager = new EntityDomainManager<Notification>(context, Request);
traceWriter = Configuration.Services.GetTraceWriter();
notificationSender = new NotificationSender(Configuration);
}
public IQueryable<Notification> GetAllNotifications()
{
return Query();
}
public SingleResult<Notification> GetNotification(string id)
{
return Lookup(id);
}
public Task<Notification> PatchNotification(string id, Delta<Notification> patch)
{
return UpdateAsync(id, patch);
}
public async Task<IHttpActionResult> PostNotification(Notification item)
{
Notification current = await InsertAsync(item);
await notificationSender.SendPush("Você tem uma nova notificaçao", item.Mensagem, current.personId);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
public Task DeleteNotification(string id)
{
return DeleteAsync(id);
}
}
}
using Microsoft.Azure.Mobile.Server;
using Microsoft.Azure.Mobile.Server.Config;
using Microsoft.Azure.NotificationHubs;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Tracing;
namespace Notification
{
public class NotificationSender
{
private NotificationHubClient hub;
private ITraceWriter traceWriter;
public NotificationSender(HttpConfiguration config)
{
MobileAppSettingsDictionary settings =
config.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.
hub = NotificationHubClient
.CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
traceWriter = config.Services.GetTraceWriter();
}
public async Task SendPush(string title, string message, string tagExpression = null)
{
// Sending the message so that all template registrations that contain "messageParam"
// will receive the notifications. This includes APNS, GCM, WNS, and MPNS template registrations.
Dictionary<string, string> templateParams = new Dictionary<string, string>();
templateParams["titleParam"] = title;
templateParams["messageParam"] = message;
try
{
// Send the push notification and log the results.
NotificationOutcome result;
if (tagExpression == null)
{
result = await hub.SendTemplateNotificationAsync(templateParams);
}
else
{
result = await hub.SendTemplateNotificationAsync(templateParams, tagExpression);
}
// Write the success result to the logs.
traceWriter.Info(result.State.ToString());
}
catch (Exception ex)
{
// Write the failure result to the logs.
traceWriter.Error(ex.Message, null, "Push.SendAsync Error");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.