RegisterAsync, ; HttpClient Notification Hub. Android, :
public async Task RegisterForPushNotifications(MobileServiceClient client)
{
if (GcmClient.IsRegistered(RootView))
{
try
{
var registrationId = GcmClient.GetRegistrationId(RootView);
var installation = new DeviceInstallation
{
InstallationId = client.InstallationId,
Platform = "gcm",
PushChannel = registrationId
};
installation.Tags.Add("topic:Sports");
PushTemplate genericTemplate = new PushTemplate
{
Body = "{\"data\":{\"message\":\"$(messageParam)\"}}"
};
var response = await client.InvokeApiAsync<DeviceInstallation, DeviceInstallation>(
$"/push/installations/{client.InstallationId}",
installation,
HttpMethod.Put,
new Dictionary<string, string>());
}
catch (Exception ex)
{
Log.Error("DroidPlatformProvider", $"Could not register with NH: {ex.Message}");
}
}
else
{
Log.Error("DroidPlatformProvider", $"Not registered with GCM");
}
}
DeviceInstallation :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace TaskList.Abstractions
{
public class DeviceInstallation
{
public DeviceInstallation()
{
Tags = new List<string>();
Templates = new Dictionary<string, PushTemplate>();
}
[JsonProperty(PropertyName = "installationId")]
public string InstallationId { get; set; }
[JsonProperty(PropertyName = "platform")]
public string Platform { get; set; }
[JsonProperty(PropertyName = "pushChannel")]
public string PushChannel { get; set; }
[JsonProperty(PropertyName = "tags")]
public List<string> Tags { get; set; }
[JsonProperty(PropertyName = "templates")]
public Dictionary<string, PushTemplate> Templates { get; set; }
}
public class PushTemplate
{
public PushTemplate()
{
Tags = new List<string>();
Headers = new Dictionary<string, string>();
}
[JsonProperty(PropertyName = "body")]
public string Body { get; set; }
[JsonProperty(PropertyName = "tags")]
public List<string> Tags { get; set; }
[JsonProperty(PropertyName = "headers")]
public Dictionary<string, string> Headers { get; set; }
}
}
RegisterForPushNotifications() . DeviceInstallation PCL.