Bug 29885 - CFNotificationCenter Darwin Notifications not working
Summary: CFNotificationCenter Darwin Notifications not working
Status: VERIFIED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.10
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 8.10.2 (C5SR2)
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2015-05-08 13:50 UTC by Josh
Modified: 2015-05-20 09:31 UTC (History)
5 users (show)

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


Attachments
here it is. (13.20 KB, application/zip)
2015-05-08 15:10 UTC, Josh
Details


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:
VERIFIED FIXED

Description Josh 2015-05-08 13:50:03 UTC
I am trying to add Darwin Notifications to communicate between my main iOS app and my WatchKitExtension. After having no luck getting notified in the WatchKitExtension when posting from the main app, I tried an even simpler standalone experiment in a separate app that shows that adding observers to the Darwin notification center fail.

In a Single View iOS app I added the following lines into ViewDidLoad which should get fired when the iPhone is locked:

const string notificationName = "com.apple.springboard.lockcomplete";

			var token = CFNotificationCenter.Darwin.AddObserver(
				name: notificationName, 
				objectToObserve: null,
				notificationHandler: (name, userInfo) => {
				// this check should really only be necessary if you reuse this one callback method
				//  for multiple Darwin notification events

				Console.WriteLine("got here");
			},
				suspensionBehavior: CFNotificationSuspensionBehavior.DeliverImmediately);

But the notificationHandler never gets fired. I tried it with a variety of Apple-defined Darwin notifications and never got anything. When hovering over an instance of CFNotificationCenter.Darwin in the debugger, I see that there are always "0 listeners" which probably explains why the handler never gets invoked.

I've also attached zipped up and attached my sample solution.

Thanks,

Josh
Comment 1 Josh 2015-05-08 13:51:34 UTC
The comparable code in XCode with Objective C works immediately. Since I don't appear to have the ability to attach additional files, just paste this code into a Single View App's viewController and watch your output window when you lock the iOS Simulator:

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                    NULL, // observer
                                    displayStatusChanged, // callback
                                    CFSTR("com.apple.springboard.lockcomplete"), // event name
                                    NULL, // object
                                    CFNotificationSuspensionBehaviorDeliverImmediately);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

static void displayStatusChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
    NSLog(@"event received!");
    // you might try inspecting the `userInfo` dictionary, to see
    //  if it contains any useful info
    if (userInfo != nil) {
        CFShow(userInfo);
    }
}

@end
Comment 2 Sebastien Pouliot 2015-05-08 15:04:25 UTC
> I've also attached zipped up and attached my sample solution.

There's nothing attached to the bug report. maybe it failed to upload ?
Comment 3 Josh 2015-05-08 15:10:23 UTC
Created attachment 11135 [details]
here it is.

Let me try again.
Comment 4 Sebastien Pouliot 2015-05-08 16:18:19 UTC
Thanks for the test case! Your diagnosis is right (but there's something to fix too).
Comment 5 Sebastien Pouliot 2015-05-19 10:16:05 UTC
Fixed in maccore/master 8e235c06a4e02eabdbda3a20f613918a25bc0450 (last commit of 3)

We're planning on backporting this for 8.10.2 once QA has verified the fix.
Comment 6 Sunil Kumar 2015-05-20 09:31:02 UTC
I have checked this issue with patch build monotouch-8.11.0.810 .Now this issue is working fine notificationHandler will get fired once we locked the screen. Here is the screencast for the same: http://www.screencast.com/t/S9685t5Z

Hence closing this issue.