Skip to content

Instantly share code, notes, and snippets.

@alexshikov
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save alexshikov/3ece7abbd2779077158a to your computer and use it in GitHub Desktop.

Select an option

Save alexshikov/3ece7abbd2779077158a to your computer and use it in GitHub Desktop.
Simple APNs registration
[Register ("AppDelegate")]
public partial class AppDelegate : ApplicationDelegate
{
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
// common logic here
SetupPushNotifications (options);
return true;
}
private void SetupPushNotifications (NSDictionary options)
{
if (Application.IsIOS8orHigher)
{
var apnsSettings = UIUserNotificationSettings.GetSettingsForTypes (
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, new NSSet ());
UIApplication.SharedApplication.RegisterUserNotificationSettings (apnsSettings);
}
else
{
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes (
UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound);
}
if (options != null)
{
// TODO handle notification options
if (options.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
{
}
if (options.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
{
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}
}
}
public override void DidRegisterUserNotificationSettings (UIApplication application, UIUserNotificationSettings notificationSettings)
{
UIApplication.SharedApplication.RegisterForRemoteNotifications ();
}
public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
string token = string.Empty;
using (var stream = deviceToken.AsStream()) {
for (int i = 0; i < stream.Length; i++) {
token += stream.ReadByte ().ToString ("X2");
}
}
// Send token to the server
}
public override void FailedToRegisterForRemoteNotifications (UIApplication application, NSError error)
{
// Usually nothing here
}
public override void ReceivedRemoteNotification (UIApplication application, NSDictionary notification)
{
// Handle remote notifications, if required
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment