Sunday, September 6, 2015

Create local notification in iOS

LocalNotification.h file :


#import <Foundation/Foundation.h>

@interface LocalNotification : NSObject

+ (LocalNotification*)standardLocalNotification;

- (void)scheduleAlert:(NSString*)alertBody;

- (void)scheduleAlert:(NSString*)alertBody fireDate:(NSDate*)fireDate;


@end

LocalNotification.m file :


#import <UIKit/UIKit.h>
#import "LocalNotification.h"

static LocalNotification*   localNotification = nil;

@interface LocalNotification(){
    UIApplication* application;
}

@end

@implementation LocalNotification

- (void)dealloc{
    [super dealloc];
}

- (instancetype)init{
    self = [super init];
    if (self) {
        application = [UIApplication sharedApplication];
    }
    return self;
}

+ (LocalNotification*)standardLocalNotification {

    @synchronized(self) {
        if(nil == localNotification) {
            localNotification = [LocalNotification alloc];
        }
    }
    return localNotification;
}

- (void)scheduleAlert:(NSString*)alertBody {

    [self scheduleAlert:alertBody fireDate:[[NSDate date] dateByAddingTimeInterval:1]];
}

- (void)scheduleAlert:(NSString*)alertBody fireDate:(NSDate*)fireDate{

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];

    localNotification.fireDate = fireDate;
    localNotification.timeZone = [NSTimeZone systemTimeZone];
    localNotification.repeatInterval = 0;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.alertBody = alertBody;

    [application scheduleLocalNotification:localNotification];
    [localNotification release];
}

@end

No comments:

Post a Comment