-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationsController.cs
More file actions
45 lines (37 loc) · 1.59 KB
/
NotificationsController.cs
File metadata and controls
45 lines (37 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using Microsoft.Azure.Mobile.Server;
using Microsoft.Azure.Mobile.Server.Config;
using Microsoft.Azure.NotificationHubs;
using System.Web.Http;
using System.Web.Http.Tracing;
using System;
using System.Threading.Tasks;
namespace wpc_appService.Controllers
{
[MobileAppController]
public class NotificationsController : ApiController
{
public async Task<string> PostNotification(string type, string message, string deviceTag, string fromUser)
{
HttpConfiguration config = this.Configuration;
MobileAppSettingsDictionary settings =
this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
// Create a new Notification Hub client.
NotificationHubClient hub =Notifications.Instance.Hub;
var notif = "{ \"data\" : {\"type\":\"" + type + "\", \"message\":\"" + message + "\", \"fromUser\":\"" + fromUser + "\", \"sound\" : \"default\"}}";
try
{
// Send the push notification and log the results.
var result = await hub.SendGcmNativeNotificationAsync(notif, deviceTag);
// Write the success result to the logs.
config.Services.GetTraceWriter().Info(result.State.ToString());
}
catch (Exception ex)
{
// Write the failure result to the logs.
config.Services.GetTraceWriter()
.Error(ex.Message, null, "Push.SendAsync Error");
}
return deviceTag;
}
}
}