Wednesday, February 3, 2016

Objective C Notifications

There are 3 types of notifications available in iOS.

  1.  Local Notifications
  2.  Push Notifications
  3.  In App Notifications

Local Notifications:

Local notifications are used when we want to notify something to user even when the app is in closed state. These are created and triggered by our application only based on a condition. 


                         



 For example our app has to notify user like

1. They have kept some download/upload and it is completed/failed.
2. There are some new updates available from the background service in the app.
3. Location changes.
4. Weather changes  etc

In the above scenarios we have to notify the user even when the app is not in active state. But having said that, these notifications are triggered by app only

We use UILocalNotification to generate these type of local notifications.

Push Notifications:


These notifications need a server setup and some key files generation using the apple certificates. These are sent by servers to notify users regarding information like

1. User got a friend request like in Facebook.
2. User got a new connection request like in LinkedIn

These notifications are from servers and the also notify the user even if the app is in inactive state.

In App Notifications:


Generally we use the below features to pass information from one screen to another screen in Objective C.

1. Variables
2. Delegates
3. Blocks

What if we want broadcast a message in the application.

Confused? :)



                         


I want to pass a message/data to all the screens which need an information.

For example, user has edited his/her full name in the edit profile screen. Obviously I need to update that in all the screens where I am displaying the username. In this case we need something which just fires an event and forgets.

Using NSNotificationCenter in foundation framework, we can achieve this.


In the above scenario, edit profile screen fires an event when there is a full name change as shown below.



 
[[NSNotificationCenter defaultCenterpostNotificationName:@"fullnameChange" 
                                                                                                    object:newFullName];



In the object parameter, we can pass the data also. In this case it is the new full name of the user.

Here, all the screens which need this full name change should observe for that change like shown below.



 
[[NSNotificationCenter defaultCenteraddObserver:self selector:@selector(fullnameChange:)
                                                                                 name:@"fullnameChange" object:nil];



So, whenever there is a full name change in the edit screen, these screens who are observing for that change will get this notification and the fullnameChange method will get called like shown below.



   
-(void) fullnameChange : (NSNotification *)notification
   {
            myLabel.text = (NSString*)[notification object];
   }



Like shown in the above code, we can get the data from the notification object. In tis case it is a string data. So I type casted it to a string.

We can send any data type at the time of posting a notification.


Hope this post is useful. Feel free to comment in case of any queries.