Skip to content

Instantly share code, notes, and snippets.

@ajpinedam
Forked from gfosco/AppDelegate.cs
Last active August 29, 2015 14:07
Show Gist options
  • Save ajpinedam/110fe3967e4733d4e4d5 to your computer and use it in GitHub Desktop.
Save ajpinedam/110fe3967e4733d4e4d5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using Xamarin.Forms;
using Parse;
namespace XamarinTest1.iOS
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
UIWindow window;
ParseObject currentInstallation;
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
Forms.Init ();
window = new UIWindow (UIScreen.MainScreen.Bounds);
window.RootViewController = App.GetMainPage ().CreateViewController ();
window.MakeKeyAndVisible ();
ParseClient.Initialize("app id here",
"NET client key here");
var data = NSUserDefaults.StandardUserDefaults;
var storedInstallation = data.StringForKey ("currentInstallation");
if (storedInstallation != null) {
currentInstallation = ParseObject.CreateWithoutData ("_Installation", storedInstallation);
}
if (currentInstallation == null) {
if (UIApplication.SharedApplication.RespondsToSelector (new MonoTouch.ObjCRuntime.Selector ("registerUserNotificationSettings:"))) {
// iOS8
UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes(UIUserNotificationType.Badge | UIUserNotificationType.Sound | UIUserNotificationType.Alert, new NSSet());
app.RegisterUserNotificationSettings (settings);
app.RegisterForRemoteNotifications ();
} else {
// iOS7
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge;
app.RegisterForRemoteNotificationTypes (notificationTypes);
}
}
return true;
}
public override void RegisteredForRemoteNotifications (
UIApplication application, NSData deviceToken)
{
Console.WriteLine ("RegisteredForRemoteNotifications");
ParseObject obj = new ParseObject ("_Installation");
string dt = deviceToken.ToString ().Replace ("<", "").Replace (">", "").Replace (" ", "");
obj["deviceToken"] = dt;
obj["deviceType"] = "ios";
obj["appIdentifier"] = "com.your.bundle";
obj["timeZone"] = "UTC";
obj["appName"] = "YourAppName";
obj["appVersion"] = "1.0.0";
obj["parseVersion"] = "1.3.0";
obj.SaveAsync ().ContinueWith (t => {
if (t.IsFaulted) {
using (IEnumerator<System.Exception> enumerator = t.Exception.InnerExceptions.GetEnumerator()) {
if (enumerator.MoveNext()) {
ParseException error = (ParseException) enumerator.Current;
Console.WriteLine (error.Message);
}
}
} else {
Console.WriteLine("Saved/Retrieved Installation");
var data = NSUserDefaults.StandardUserDefaults;
data.SetString ("currentInstallation", obj.ObjectId);
}
});
}
public override void FailedToRegisterForRemoteNotifications (UIApplication application, NSError error)
{
Console.WriteLine (error.Description);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment