Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegisterForRemoteNotificationTypes causes UI to hang

I can successfully register my app for push notifications:

UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(
    UIRemoteNotificationType.Alert
    | UIRemoteNotificationType.Badge
    | UIRemoteNotificationType.Sound);

But whenever I execute this, my UI hangs for usually 2-3 seconds. Even if I have it early in the app lifecycle as recommended (e.g., even WillFinishLaunching) my UI still hangs once my first ViewController loads.

My first thought was to execute the registration in a separate thread, but MonoTouch prevents this:

// From MonoTouch.UIKit
public virtual void RegisterForRemoteNotificationTypes(UIRemoteNotificationType types)
{
    UIApplication.EnsureUIThread();

My next thought was to at least pop up a UIAlertView box to let the user know something's going on, but for some reason it didn't display until after the registration took place, resulting in the UIAlertView opening and immediately closing!

modalPopup = new UIAlertView("Working", "The application is loading...", null, null);
modalPopup.Show();
// It doesn't show here!
RegisterForRemoteNotificationTypes();
// It shows here!
modalPopup.DismissWithClickedButtonIndex(0, true);

How can I either

  1. Stop the push notification registration from tying up my UI thread
  2. cover up the UI freeze?
like image 294
Calvin Fisher Avatar asked Feb 07 '13 00:02

Calvin Fisher


1 Answers

Did you override public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)

or

public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)

I've had similar problem where within Registered... function, I called a web service to upload the device token. But the web call wasn't within another thread so it caused to freezes UI.

like image 198
Marc-Alexandre Bérubé Avatar answered Nov 15 '22 07:11

Marc-Alexandre Bérubé