NSNotificationCenter not acting on the message - iphone

In one of my classes I post a notification:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ALERTNOTI" object:self userInfo:nil];
In my app delegaate I listen:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMethod) name:#"ALERTNOTI" object:nil];
I use NSLog to track when I send and when the method myMethod gets called.
The method is not getting called despite me sending out the notification.
Is there something that I need to know about NSNotification? Is it tempermental?

do this changes
[[NSNotificationCenter defaultCenter] postNotificationName:#"ALERTNOTI" object:nil];
if you want to pass some object through the notification. then do this
ex: you want to pass an NSDictionary *dict
[[NSNotificationCenter defaultCenter] postNotificationName:#"ALERTNOTI" object:nil userInfo:dict];
the method you want to call via notification should be like this.
-(void)method:(NSNotification *) notif
{
// your code here.
//if you want to access your dict
NSDictionary *myDict=[notif userInfo];
}

Try this:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ALERTNOTI" object:nil];

Related

How to remove observer

I have an ARC enabled project
There are few observers added in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(getSipNotification:) name:#"getSipNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(syncExtensionData:) name:#"syncExtensionData" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(showLocalNotification:) name:#"showLocalNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(outgoingCall:) name:#"outgoingCall" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playRingtone) name:#"playRingtone" object:nil];
I want to remove all observers so I added following line in viewDidUnload
[[NSNotificationCenter defaultCenter] removeObserver:self];
Now my question is, is this remove all observers?
If not how can do it?
UPDATE
If I want to remove a single observer how can do it?
Can you help me please.
Yes, It will remove all observers.
[[NSNotificationCenter defaultCenter] removeObserver:self];
And you can remove a particular observer like this...
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"syncExtensionData" object:nil];
In my application i used this notification :
for particular observer remove this way :
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotatedFeedBackView:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)deviceRotatedFeedBackView:(NSNotification*)notification
{
//right whetever you want
}
- (void)viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
may be it will helpful to you.
Yes it'll remove all the observers in your class.
You can use following to remove single observer:
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"getSipNotification" object:nil];
To remove individual observer.
viewDidUnload is deprecated in iOS6 and later, so Your observer never be removed from notification center in iOS6 and later. To remove single observer try
- (void)removeObserver:(id)notificationObserver name:(NSString *)notificationName object:(id)notificationSender

NSNotification not receiving in View Controller from appDelegate

hello i assign nsnotifiaction in app delegate.m's method and this method call eprox every 30sec, and i wants its notifcation in viewcontroller adn execute method,
here is my code of appdelegate .m
- (void)layoutAnimated:(BOOL)animated{
BOOL yy= self.bannerView.bannerLoaded;
if (yy==1){
self.iAdString=[NSMutableString stringWithString:#"1"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"BannerViewActionWillBegin" object:self];
}
else{
self.iAdString=[NSMutableString stringWithString:#"0"];
[[NSNotificationCenter defaultCenter] postNotificationName:#"BannerViewActionDidFinish" object:self];
}
}
and in viewcontroller.m
//i defined in viewdidload method
- (void)viewDidLoad{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willBeginBannerViewActionNotification:) name:#"BannerViewActionWillBegin "object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didFinishBannerViewActionNotification:) name:#"BannerViewActionDidFinish" object:nil];
}
its method are..
- (void)willBeginBannerViewActionNotification:(NSNotification *)notification{
[self.view addSubview:self.app.bannerView];
NSLog(#"come");
}
- (void)didFinishBannerViewActionNotification:(NSNotification *)notification {
NSLog(#"come");
[self.app.bannerView removeFromSuperview];
}
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
I have not getting response of excessing method while method read in appdelegate file.
Please help me.
You have a typo error.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willBeginBannerViewActionNotification:) name:#"BannerViewActionWillBegin "object:nil];
//Your error here-------------------------------------------------------------------------------------------------------------------------------------^
You have put a space there.
SideNote: For all notification names, you should/can create a separate file and put all your notification names as constants strings.
const NSString *kBannerViewActionWillBegin=#"BannerViewActionWillBegin";
this will be easier to change the value and no such typo will happen.
From your code what I get is except for the notification name's all other stuff is fine, did you check whether the notification gets fired. Try keeping break points at the notification firing line.

postNotificationName not calling observer method

I am trying to call a method within an uiview from AppDelegate using the NSNotificationCenter to no avail..
AppDelegate.m
[[NSNotificationCenter defaultCenter] postNotificationName:#"ProcessDidComplete" object:items];
Then via MainStoryboard, the main view is loaded which controller class is MainViewController
in MainViewController.h viewDidLoad i have
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ProcessDidComplete:) name:#"ProcessDidComplete" object:nil];
and then the method
- (void) ProcessDidComplete:(NSNotification *)pNotification
but it never gets called.
Thanks for any help!
just change a way..
First add observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ProcessDidComplete:) name:#"ProcessDidComplete" object:nil];
Then Post Notification
[[NSNotificationCenter defaultCenter] postNotificationName:#"ProcessDidComplete" object:items];
Finally remove in viewWillDisappear
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"ProcessDidComplete" object:nil];
Your code looks okay, which makes me wonder where in your app delegate you post the notification?
If you post the notification before you add the observer in the view controlller, then the notification will never be received. Can you not send the message to the main view controller directly, i.e., as a property, rather than using notifications?
Just wanted to note here that iOS notification names are case-sensitive:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handUpdate:) name:#"scheduleUpdated" object:nil];
will not respond to:
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScheduleUpdated" object:items];
(as I spent the last 20 minutes figuring out...)

Does NSNotification work everywhere?

I'm sending a notification using:
[[NSNotificationCenter defaultCenter] postNotificationName:#"historyLoaded" object:jsonReturn];
And receiving the notification using:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(manageHistory:) name:#"historyLoaded" object:nil];
Then the method in the selector is:
- (void) manageHistory: (NSNotification *) historyData{
NSLog(#"this bit of code was run");
}
For some reaason the notification doesn't get through. Can notifications be send and received from anywhere in the app?
The object parameter in postNotification should be filled with an object which is "sending" the notification, or nil if the sender is not necessarily specified.
If you want to pass some information along, you should use postNotificationName:object:userInfo and put the information in userInfo dictionary instead.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(manageHistory) name:#"historyLoaded" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"historyLoaded"
object:nil userInfo:jsonReturn];
- (void) manageHistory: (NSNotification *) historyData{
NSDictionary* _dict = historyData.userInfo;
NSLog(#"Your information embedded to dictiuonary obj %#",_dict);
}
NOTE : Make sure your historyData should be a dictionary object in postNotificationName

NSNotification not being sent when postNotificationName: called

I'm trying to get one instance of using NSNotificationCenter with addObserver and postNotificationName but I can't work out why it won't work.
I have 2 lines to code to add the observer and send the message in 2 different classes
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(newEventLoaded:) name:#"Event" object:nil];
and
[[NSNotificationCenter defaultCenter]postNotificationName:#"Event" object:self];
If I set the name to nil it works fine becuase it's just a broadcast, when i try and define a notification name the messages never get through.
All my code makes use of NSNotifications like so:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateView) name:#"ScanCompleted" object:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:#"ScanCompleted" object:nil];
The first one is registering the notification and the second posting of the notification.
Basically it's all to do with the order of execution. If you've executed postNotificationName before addObserver, then this is an easy problem to have. Use breakpoints and step through the code :)
Your first breakpoint should stop here:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(updateView:) name:#"ScanCompleted" object:nil];
Then here:
[[NSNotificationCenter defaultCenter]postNotificationName:#"ScanCompleted" object:self];
Also, make sure the selector has a colon on. Because it's method signature will be:
- (void)updateView:(NSNotification *)notification;
I had the same problem.
The reason is that I called removeObserver method at
- (void)viewDidDisappear:(BOOL)animated{
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self];
}
So check whether if you had called removeObserver before postNotification.
Tips: You can search the keyword "removeObserver" to find if you had called this function.
Change this:
[[NSNotificationCenter defaultCenter]postNotificationName:#"Event" object:self];
to this:
[[NSNotificationCenter defaultCenter]postNotificationName:#"Event" object:nil];
If your first notification is registered properly, newEventLoaded should be called.
I had a similar issue and my problem was due to the notification being called on another thread. This solved my problem.
dispatch_async(dispatch_get_main_queue(),^{
[[NSNotificationCenter defaultCenter]postNotificationName:#"Event" object:self];
});
Have you tried any other names but #"Event" and nil? Just to be sure, you could define your event names in one file and include that into both notification registration and sending. For example:
Header file:
extern NSString * const NOTE_myEventName;
Source file:
NSString * const NOTE_myEventName = #"MyEventName";
Registration:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(handleMyEvent:)
name:NOTE_myEventName
object:nil];
Notification sending:
[[NSNotificationCenter defaultCenter]
postNotificationName:NOTE_myEventName object:nil];
I successfully fixed my "NSNotification not being sent when postNotificationName: called" crash.
I found the real bug is in notification message handler.
The postNotificationName and addObserver are all right as the first post of this thread.