Last active
September 19, 2017 20:50
-
-
Save gfosco/a526cbc3061398d50e8b to your computer and use it in GitHub Desktop.
Parse Push on Xamarin iOS
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
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); | |
} | |
} | |
} | |
This just helped me, thank you for sharing
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes! This is great!!!!!