using System;
using System.Globalization;
using Foundation;
using MyApp.iOS.Notifications;
using MyApp.Notifications;
using WindowsAzure.Messaging;
[assembly: Xamarin.Forms.Dependency(typeof(AzureNotificationHubService))]
namespace MyApp.iOS.Notifications {
/// <summary>
/// iOS implementation of the Notification Service
/// </summary>
public class AzureNotificationHubService : INotificationService {
/// <summary>
/// Register a device to the Azure notification hub
/// </summary>
/// <param name="tag">The user tag to associate the registration with</param>
public void Register(string tag) {
//Get token from APN registration process. Store the token when registration process was completed.
var token = NSUserDefaults.StandardUserDefaults["token"];
var ConnectionString = [ConnectionString];
var SharedKey = [SharedKey];
var NotificationHubPath = [NotificationHubPath];
if (token != null) {
var cs = SBConnectionString.CreateListenAccess(new NSUrl(ConnectionString), SharedKey);
var hub = new SBNotificationHub(cs, NotificationHubPath);
hub.UnregisterAll(token as NSData, (error) => {
if (error != null) {
return;
}
var tags = new NSSet(tag);
var expire = DateTime.Now.AddYears(1).ToString(CultureInfo.CreateSpecificCulture("en-US"));
NSError returnError;
hub.RegisterTemplate(token as NSData,
"Template",
"{\"aps\":{\"alert\":\"$(message)\", \"content-available\":\"#(silent)\", \"badge\":\"#(badge)\", \"sound\":\"$(sound)\"}, \"url\":\"$(url)\"}",
expire,
tags,
out returnError);
});
}
}
}
}