Last active
August 29, 2015 14:20
-
-
Save alexshikov/3ece7abbd2779077158a to your computer and use it in GitHub Desktop.
Simple APNs registration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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