This is a general question, but I can't seem to find a straight answer, and I'm not sure what to look for. I have a backend that sends notfications when someone likes something you posted, I am able to send it and the app receives it. However, I'm not sure how to create a UI to handle the notification. My first guess was that, since receiving notifications triggers didReceiveRemoteNotification fetchCompletionHandler in the AppDelegate, I should try to get the current VC in that function and add graphical elements from there. However, I read that this is not something you should do, and that you should use NSNotifications instead. The problem is I'm not sure I understand how I can articulate remote notificattions with the NSNotificationsCenter.
What do you recommend?
NSNotification is part of Observer design pattern.
It like a "broadcast".
First you have to register every component which should be listening the event :
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myFonction) name:#"Notif name" object:nil];
//swift
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(myFonction), name: "notif name", object: nil)
Next, when you have a message to broadcast, post a notification with the same name :
[[NSNotificationCenter defaultCenter] postNotificationName:#"Notif name" object:nil];
//swift
NSNotificationCenter.defaultCenter().postNotificationName("notif name", object: nil)
When you will post the notification, the method myFonction will be called on the target
Related
I came across a piece of code that was registering a notification with:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(someStuff:)
name:#"someStuff"
object:nil];
and then triggerring it with:
[[NSNotificationCenter defaultCenter] postNotificationName:#"someStuff" object:self];
Why is one object set to nil and other to self? What does that do?
Taking the second case first, when you post a notification you indicate which object is the sender of that notification. Usually that is 'self', but you can post a notification on some other object's behalf.
On the other hand, when you register to observe notifications, you can specify that you only want to observe notifications that are posted by a particular object. So you can say "I want to observer the 'cool new data' notification, but only if this particular object posts it. If some other object says there is 'cool new data', I'm not interested."
If you pass nil as the 'object' parameter when you register an observer, then you're saying you don't care who posts this notification, you want your selector (or block) to fire for any sender. So if anyone posts 'cool new data', I want to know about it.
BTW, these are not "push notifications". That's a different feature/API.
to quote apple docs.. the first object is
notificationSender
The object whose notifications the observer wants to receive; that is, only notifications sent by this sender are delivered to the
observer.
If you pass nil, the notification center doesn’t use a notification’s sender to decide whether to deliver it to the observer.
and the second is
notificationSender
The object posting the notification.
so in second case this tell who is actually sending this notification..that is that class itself.
I don't know how to use NSNotification in our iphone application. and one more doubt of difference between delegate and NSNotification because both are communicating through objects.
and give the practical example.
=> NSNotificationCenter provides a centralized hub through which any part of an application may notify and be notified of changes from any other part of the application.
=> Observers register with a notification center to respond to particular events with a specified action.
=> Each time an event occurs, the notification goes through its dispatch table, and messages any registered observers for that event.
Use Of NS-Notification in Objective C
//Write from where you want to pass the data
[[NSNotificationCenter defaultCenter]postNotificationName:#"TeamTable" object:hdImage userInfo:nil];
Here
**TeamTable is notification observer name (Unique name)
**hdImage is what data you want to pass to another controller
Now write these code in that Controller from where you want to receive
the data
-(void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(detailsData:) name:#"TeamTable" object:nil];
}
-(void)detailsData:(NSNotification*)sender{
//In sender it contain All received data
}
It’s important for objects to remove observers before they’re deallocated, in order to prevent further messages from being sent.
-(void)viewWillDisappear:(BOOL)animated{
[[NSNotificationCenter defaultCenter]removeObserver:self name:#"TeamTable" object:nil];
}
For More Details about NS-Notification You can follow this Link http://nshipster.com/nsnotification-and-nsnotificationcenter/
what is the use of NSNotification in iphone sdk?
Thanks
I know this isn't generally a good way to answer questions, but RTFM.
NSNotification objects encapsulate information so that it can be broadcast to other objects by an NSNotificationCenter object. An NSNotification object (referred to as a notification) contains a name, an object, and an optional dictionary. The name is a tag identifying the notification. The object is any object that the poster of the notification wants to send to observers of that notification (typically, it is the object that posted the notification). The dictionary stores other related objects, if any. NSNotification objects are immutable objects.
You can create a notification object with the class methods notificationWithName:object: or notificationWithName:object:userInfo:. However, you don’t usually create your own notifications directly. The NSNotificationCenter methods postNotificationName:object: and postNotificationName:object:userInfo: allow you to conveniently post a notification without creating it first.
NSNotifications allow you to have a method called when a event occurs.
For example if you have a MPMoviePlayer and you want todo something when it is done you could use the following code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:yourMoviePlayer.moviePlayer];
or if you want to do something when the device rotates:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:self];
You should have a look at the Notifications section in the Cocoa Fundamentals Guide. There's anything you need to know about notifications there : from definition to use cases.
In terms of events NSNotifications are an alternative to delegation. Delegation can be used to notify one single delegate of an event, whereas notifications can be used to notify an arbitrary number of receivers. A notification is sent to the main notification center, which then notifies every object, that has registered for the notification.
One important difference is, that with delegation you can receive the delegates response to the event, whereas with NSNotifications you just send away the notification, but you don't know about the receivers or their response to the notification.
You register a UINotification when you want to receive a alert from iOS. So if you want to do something when a accessory is plugged in or a TV is plugged in you would register a UINotification for it and it would call a method in your app when the event occurs.
i have 2 classes.
class1 gets some information through the net. when one of these informations comes, class1 has to send a notification to class2.
i understood it so that i have to put
[[NSNotificationCenter defaultCenter] postNotificationName:at"anyUserNotification" object:class2];
into class1
[[NSNotificationCenter defaultCenter] addObserver:self selector:atselector(anyInteraction:) name:dontKnowTheSense object:dunno2];
have i understood the object:class2 in the postnotification right ?
if yes: is it possible to make an anonymious notification, so that the sending class must not know, which and how many classes are listening ?
or - at least i think so, have i understood the whole notification incorrect ?
besides the notification i dont need to pass any data, but in one case it would be helpful to pass an enum
so, could anybody please help me ?
btw: i cant find a way to post an at on this windows-pc and i dont know, why it did not indent the code, i think i made 4 spaces
Object is not a mandatory argument you can set it to nil or the object sending the notification message.
[[NSNotificationCenter defaultCenter] postNotificationName:#"NotificationName" object:notificationSenderOrNil];
When listening you can filter to only do something for notification sent by a specific sender.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(sel) name:#"NotificationName" object:notificationSenderOrNil];
And you can pass your data in a dictionnary with userInfo: argument.
Is it Ok with that ?
i am new Notification.what is the purpose of it...
can we use [[NSNotificationCenter defaultCenter] addObserver without postNotification,
If we can do it,how it is identified?what is the relationship between addObserver and postNotification?any one can explain in detail?
it's rather straight forward: you define an event by giving it a unique name, then you add an observer to it using [[NSNotificationCenter defaultCenter] addObserver and point it to a selector that receives the event.
then later on in your code you can postNotification and if the unique name matches the one you registered for - the selector you provided will be called.