Bug 12740 - UIApplication.SharedApplication.RegisterForRemoteNotificationTypes() causes Crash
Summary: UIApplication.SharedApplication.RegisterForRemoteNotificationTypes() causes C...
Status: RESOLVED ANSWERED
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 6.3.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: 7.2.0 (iOS 7.1)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-06-18 14:41 UTC by asp_net
Modified: 2016-01-11 21:41 UTC (History)
5 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description asp_net 2013-06-18 14:41:24 UTC
When calling 

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

somewhere within the application the app crashes after starting the debugger for the device. Even if the code isn't called.
Comment 1 Rolf Bjarne Kvinge [MSFT] 2013-06-20 09:22:08 UTC
I added that code to a test project, and it doesn't crash for me.

Can you attach a complete test project I can try out?
Comment 3 PJ 2013-11-19 16:44:49 UTC
This bug was targeted for a past milestone, moving to the next non-hotfix active milestone.
Comment 4 ilya.golovach 2014-05-13 05:46:44 UTC
I have the same issue. Testing with Xamarin.iOS 7.0.6.168

It seems that the problem is in  public override void RegisteredForRemoteNotifications (        UIApplication application, NSData deviceToken)

deviceToken	System.Exception: Could not initialize an instance of the type 'MonoTouch.Foundation.NSString': the native 'initWithDa…
Comment 5 Rolf Bjarne Kvinge [MSFT] 2014-05-13 06:11:51 UTC
Ilya, it seems you're using deviceToken.ToString () [1], which is not always guaranteed to return the correct result [2]. You need to fetch deviceToken as binary data (the easiest is probably to use deviceToken.AsStream () and read binary data from it).

[1] You probably got your code from here: http://docs.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/remote_notifications_in_ios/, which is wrong (we know about this and a fix is in progress).

[2] An NSData does not necessarily contain data that can be converted to a string (and for this case Apple's documentation [3] explicitly states that deviceToken is 32 bytes of binary data).

[3] https://developer.apple.com/library/ios/documentation/uikit/reference/uiapplicationdelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:didRegisterForRemoteNotificationsWithDeviceToken:
Comment 6 ilya.golovach 2014-05-13 08:44:41 UTC
Rolf,
Thank you for the info. It helped. 
Some info of getting readable device token can be found here
http://weblogs.thinktecture.com/cweyer/2010/12/implementing-push-notifications-for-ios-with-c-monotouch-using-the-cloud-urban-airship.html
Comment 7 GouriKumari 2016-01-11 21:41:40 UTC
BAsed on comment #5 and comment #6, I am closing this bug.