Monday, July 1, 2013

Local Notification in IOS

Local Notification in IOS



Step 1
Design this
  
Step 2
add this code in "viewController.h" 


#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    IBOutlet UITextField *message;
}

@property (nonatomic, retain) UITextField *message;

-(IBAction) btnSet:(id) sender;
-(IBAction) btnCancelAll:(id) sender;

@end


Step 3
type this code in "viewController.m" 


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize message;

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

-(IBAction) btnSet:(id) sender
{
    
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    
    //---set the notification to go off in 10 seconds time---
    localNotification.fireDate =
    [[NSDate alloc] initWithTimeIntervalSinceNow:10];
    
    //---the message to display for the alert---
    localNotification.alertBody = message.text;
    
    localNotification.applicationIconBadgeNumber = 1;
   
    //---uses the default sound---
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    
    //---title for the button to display---
    localNotification.alertAction = @"View Details";
    
   //---schedule the notification---
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    [localNotification release];
}

-(IBAction) btnCancelAll:(id) sender
{
    //---cancel all notifications---
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
}

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

@end


Step 4

add this method in "AppDelegate.m" 


- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    
    
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Inside application Receive Local Notification "
                          message:notification.alertBody
                          delegate:self
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil];
    application.applicationIconBadgeNumber = 0;
    
    [alert show];
    [alert release];
     
}

Step 5




Step 6

Run Your app

Type Something 

After 10 Second

          


























Click the Notification


























No comments:

Post a Comment