Skip to content

Instantly share code, notes, and snippets.

@ra7bi
Forked from gfosco/AppDelegate.cs
Last active September 19, 2017 21:04
Show Gist options
  • Save ra7bi/5d5c7da474590eafbcc8ac558e6be452 to your computer and use it in GitHub Desktop.
Save ra7bi/5d5c7da474590eafbcc8ac558e6be452 to your computer and use it in GitHub Desktop.
Parse Push on Xamarin iOS
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using Parse;
using UIKit;
namespace x2coder.iOS
{
[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else
{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
ParseObject obj = ParseObject.Create("_Installation");
string dt = deviceToken.ToString().Replace("<", "").Replace(">", "").Replace(" ", "");
obj["deviceToken"] = dt;
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!!!: " + error.Message);
}
}
}
else
{
//Console.WriteLine("Saved/Retrieved Installation");
var data = NSUserDefaults.StandardUserDefaults;
data.SetString("currentInstallation", obj.ObjectId);
//Console.WriteLine("Installation ID = " + obj.ObjectId);
}
});
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
new UIAlertView("Error registering push notifications", error.LocalizedDescription, null, "OK", null).Show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment