Search Posts in my Blog

Showing posts with label notification iphone tutorial. Show all posts
Showing posts with label notification iphone tutorial. Show all posts

Friday, 2 March 2012

UILocalNotifications Tutorial



In this post we will see how to work with the local notification but before beginning with the further reading I would request you to read my previous post which is a description on UILocalNotification class. 


Design Phase: We will trigger the local notification with the help a button and then we will close the application with the help of the home button and then after a few seconds the application will fire a notification in the form of the alert view.





Step 1: Open Xcode and create a window based application and add a UIViewController subclass file to it with any name MyViewController or any name of your choice so now there are two files into your project that’s the MyViewController.h and MyViewController.m.

Step 2: Add a function which we will call on the touch of the button inside the MyViewController.h file and then give the body to that function in the MyViewController.m file





-(void)Trigger_LocalNotification
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    UILocalNotification *_localNotification = [[UILocalNotification alloc]init];
    
      //setting the fire dat of the local notification
    _localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];


    //setting the time zone
    _localNotification.timeZone = [NSTimeZone defaultTimeZone];
    
    //setting the message to display
    _localNotification.alertBody = @"You are notified";
    
    //default notification sound
    _localNotification.soundName = UILocalNotificationDefaultSoundName;
    
    //displaying the badge number
    _localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;
    
    //schedule a notification at its specified time with the help of the app delegate
    [[UIApplication sharedApplication]scheduleLocalNotification:_localNotification];
    
}



Code Explanation: The first line of the code removes all the local notification from the system if they are declared. In the second line I am initializing the UILocalNotification variable and in the third line I am using the fireDate property to set the time when this local notification will trigger and as you can see that the notification will be triggered after 5 seconds.


The soundName is a property of the UILocalNotification class which is used to play a sound when the notification is triggered and when the app firing this local notification is not active then in that case a alert box will pop up with the default notification sound and the alert message is written in using the property alertBody. The last line of the code will attach this notification with the system.


Step 3: make sure to attach this function with the button touch up inside event





[btn addTarget:self action:@selector(Trigger_LocalNotification)forControlEvents:UIControlEventTouchUpInside];


Step 4: Now select the App Delegate.m file of your project and create the object of this class (MyViewController) 





#import "LocalNotification_DemoAppDelegate.h"
#import "MyViewController.h"

@implementation LocalNotification_DemoAppDelegate
@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    // Override point for customization after application launch.
    MyViewController *obj = [[MyViewController alloc]init];
    [self.window addSubview:obj.view];
    [self.window makeKeyAndVisible];
    return YES;
}

Step 5: Run the application and when the app is launched in the simulator then quickly press the home button to see the alert box of the local notification. Here’s the view at the final output.






After you hit the button then please press the home button and after 5 seconds you will see the local notification alert box 






I hope that this post has helped you out in learning how to implement UILocalNotification

Thursday, 1 March 2012

NSNotificationCenter tutorial



Design Phase: In today’s post we will be seeing a simple demonstration for the notification that I have explained in my earlier post.


I will be using two UIViewController subclasses one with the label which will be the listener and the other will be the broadcaster with the slider control, and when the slider will slide then we will register one notification and then refer that notification with its name to display the data of the slider on the first view controller, here’s a view at the final output to see what we are going to do.






Step 1: Open Xcode create a windows based application add two files of UIViewController subclass one with the name ListenerViewController and other with the name BroadCasterViewController so now the new files that are added into your project are 
ListenerViewController.h ,ListenerViewController.m and
BroadCasterViewController.h , BroadCasterViewController.m





Step 2: Create the ListernerViewController and the BroadCasterViewController like the one given in the image above in the design phase. After that select the appdelegate.m file in your project and create the object of these two view controller classes and add them to the tabbarController here’s the code to do that


#import "Notification_DemoAppDelegate.h"
#import "BroadCasterViewController.h"
#import "ListenerViewController.h"

@implementation Notification_DemoAppDelegate
@synthesize window=_window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    // Override point for customization after application launch.
    BroadCasterViewController *broadCastObject = [[BroadCasterViewController alloc]init];
    ListenerViewController *listenerObject = [[ListenerViewController alloc]init];
    NSArray *objectArray = [[NSArray alloc]initWithObjects:listenerObject,broadCastObject, nil];
    UITabBarController *tabC = [[UITabBarController alloc]init];
    tabC.viewControllers = objectArray;
    [self.window addSubview:tabC.view];
    [self.window makeKeyAndVisible];
    return YES;
}

Step 3: Now it’s time to register our notification for the slider control (remember we are registering the notification here in the notification dispatch table with an appropriate name and with the help of this name we will refer the slider value and display the slider value onto the UILabel object in the ListenerViewController) Create a method in the BroadCasterViewController and assign this method to the sliders value changed event


BoradCasterViewController.h file code


#import <UIKit/UIKit.h>

@interface BroadCasterViewController : UIViewController {
   
     UISlider *_sliderView;
}
-(void)Register_Notification:(UISlider*)control;

@end



BoradCasterViewController.m file code


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _sliderView = [[UISlider alloc]initWithFrame:CGRectMake(18, 203, 284, 23)];
        _sliderView.minimumValue = 1.0f;
        _sliderView.maximumValue = 100.0f;
        [_sliderView addTarget:self action:@selector(Register_Notification:)forControlEvents:UIControlEventValueChanged];
        self.title = @"Broad cast";
    }
    return self;
}


now inside the function write this code


-(void)Register_Notification:(UISlider*)control
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MySlider_Notification" object:control];
}


Code Explanation: 

postNotificationName: object: function is used to give your notification a name and register it with the object posting the notification in this case it will be the slider control as we have passed the slider reference as a parameter for the object.

This is all you have to do inorder to register a notification.

Step 4: Now once we are done with registering the notification, we will code for the observer who is observing this notification i.e the listener of this notification. So select the ListenerViewController.h file and create a function (we will come to the explanation of this function later)


-(void)ReadSliderValue:(NSNotification*)notification;

Now after the declaration of this function its time to give body to this function inside the ListenerViewController.m file, here's the code to do that


-(void)ReadSliderValue:(NSNotification*)notification
{
    UISlider *slider = [notification object];
    _sliderValueLabel.text = [NSString stringWithFormat:@"%.f",slider.value];
                       
}


Code Explanation: The above method has a parameter which is the object of the NSNotification class and this class has an object method which itself is called as object which returns the object associated with a particular notification in this case UISlider.

The main moto of the demo is that when you will select the tab of the BroadCasterView and slide the slider then in that case a notification must be registered and when you again come to the ListenerView then you must see the sliders value in the label, so we have to read the data from the dispatch table for the slider and assign it to the label in such a way so that when the user selects the ListenerView then he must see the changed value of the slider through the dispatch table so for doing this you have to add in one small piece of code in the ListenerViewControllers init method which is given below.


[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(ReadSliderValue:)name:@"MySlider_Notification" object:nil];


Code Explanation: The addObserver method of the NSNotifficationCenter class is used to add an observer for the notification in the dispatch table with a selector, which performs an appropriate action when the said notification is triggered, here we will be reading the value of the slider control present in the BroadCastViewController in the ListenerViewController.


Step 5: Run the app and get the output like the one given in the below image









You can even add more than one notification at a time here's a view at the same demo that i have explained above and this time i am using the switch control as well to determine whether the switch is on or off via notification, here's a view at the output 







I hope that this post has helped you