2011-08-25 137 views
0

我正在iphone中創建一個鬧鐘應用程序,它使用sqlite和本地通知。 我有類tableview包含4行選擇時間,重複間隔,聲音etc.when我選擇所有這些因素這些都存儲在全局變量。在我的tableview我選擇這些行我有一個保存按鈕。當我點擊保存按鈕後,選擇特定的行,時間,重複和聲音的值通過這個全局變量保存在sqlite數據庫中。現在,我將這些變量傳遞給我的通知對象,以便在點擊保存時設置警報按鈕。但通知未設置。 這是我通知代碼:如何在iPhone中設置鬧鐘並保存在本地通知中?

// 
// TAddAlarmController.m 
// StopSnooze 
// 
// Created by raji.nair on 7/18/11. 
// Copyright 2011 __MyCompanyName__. All rights reserved. 
// 

#import "TAddAlarmController.h" 
//#import "CustomCell.h" 
#import "StopSnoozeAppDelegate.h" 
#import "Alarm.h" 
#import "TAlarmNewController.h" 
#import "global.h" 
#import "TTimePickerController.h" 
#import "TAlarmSoundController.h" 
#import "AlarmMessageController.h" 
#import "TPenaltyController.h" 
#import "TSnoozeIntervalController.h" 

#define DATABASE_TITLE @"StopSnooze.sqlite" 
#define DATABASE_NAME @"StopSnooze" 

@implementation TAddAlarmController 
@synthesize dateFormatter; 
@synthesize am; 
@synthesize undoManager; 
@synthesize time; 
@synthesize tblView; 
@synthesize detailTxt; 
//@synthesize controller; 


#pragma mark - 
#pragma mark View lifecycle 


- (void)viewDidLoad { 


    [super viewDidLoad]; 
    self.title = @"Add Alarms"; 
    newtemp = anew.temp; 
    UIBarButtonItem * saveButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save)]; 
     self.navigationItem.rightBarButtonItem = saveButton; 
     [saveButton release]; 

} 


-(void)schedulealarm 
{ 
    UILocalNotification *localNotif = [[UILocalNotification alloc] init]; 
    if (localNotif == nil) 
     return; 
    localNotif.fireDate = (NSDate*)lblDate.text; 
//lblDate.text is where my date is getting saved through the picker but in the fireDate it is showing null. 
    localNotif.timeZone = [NSTimeZone defaultTimeZone]; 

    // Notification details 
    localNotif.repeatInterval = (int)newrepeat; 
//newrepeat is the global variable where is select the repeat interval for alarm. 
    localNotif.alertBody = newmessage; 
    // Set the action button 
    localNotif.alertAction = @"View"; 

    localNotif.soundName = UILocalNotificationDefaultSoundName; 
    localNotif.applicationIconBadgeNumber = 1; 

    if (localNotif == nil) { 
     NSLog(@"Yes"); 
    } 
    else { 
     NSLog(@"Notification called"); 
    } 


    // Specify custom data for the notification 
    NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"someValue" forKey:@"someKey"]; 
    localNotif.userInfo = infoDict; 

    // Schedule the notification 
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
    [localNotif release]; 


} 


- (void)viewDidUnload { 
    // Release any properties that are loaded in viewDidLoad or can be recreated lazily. 
    self.dateFormatter = nil; 
} 
-(void)saveInDatabase 
{ 
    app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication] delegate]; 
    [app copyDatabaseIfNeeded]; 
    NSString *filePath =[app getDBPath]; 
    sqlite3 *database; 

    NSTimeInterval timeStamp = [[NSDate date]timeIntervalSince1970]; 
    NSNumber *timeStampObj = [NSNumber numberWithInt:timeStamp]; 
    NSLog(@"%d",timeStampObj); 

    AlarmID =(int)timeStampObj; 
    NSLog(@"%@",AlarmID); 

    NSDate *date = [NSDate date]; 
    NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease]; 
    [formatter setDateFormat:@"HH:MM"]; 

    NSString *str = [formatter stringFromDate:date]; 
    NSLog(@"%@",str); 

    if (sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) { 
     const char *sqlStatement = "insert into AlarmsInformation(alarm_id,alarm_time,snooze_interval,sound_file,alarm_message) VALUES (?,?,?,?,?)"; 
     sqlite3_stmt *compiledStatement; 
     if (sqlite3_prepare_v2(database,sqlStatement ,-1, &compiledStatement,NULL) == SQLITE_OK) { 
      //sqlite3_bind_text(compiledStatement, 1, [AlarmID UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_int(compiledStatement, 1, AlarmID); 
      sqlite3_bind_text(compiledStatement, 2, [lblDate.text UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_int(compiledStatement, 3, databaseinterval); 
      sqlite3_bind_text(compiledStatement,4 , [newsound UTF8String], -1, SQLITE_TRANSIENT); 
      sqlite3_bind_text(compiledStatement, 5, [newmessage UTF8String], -1, SQLITE_TRANSIENT); 
     } 
     if (sqlite3_step(compiledStatement)!= SQLITE_DONE) { 
      NSLog(@"Save Error:%s",sqlite3_errmsg(database)); 
     } 
     else { 
      sqlite3_reset(compiledStatement); 
      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Alarm Set" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 
      alert = nil; 
     } 
     sqlite3_finalize(compiledStatement); 
    } 
    sqlite3_close(database); 

} 

-(IBAction)save{ 

    if (lblDate.text == NULL && interval == NULL && newsound == NULL) 
    { 
     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Set Date,interval,sound for alarm" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
     alert = nil; 
    } 
    else { 
     [self saveInDatabase]; 
     [self schedulealarm]; 
    } 

    //[self.navigationController popViewControllerAnimated:YES]; 
} 

@end 

而且在我的appDelegate已創建didReceiveNotification.But通知的方法行不通。我的火日期顯示爲空值。

可能是什麼概率?

我想傳遞值來觸發我存儲在我的變量中。請幫我解決我的問題。

+0

請問您的應用程序在後臺運行 - 我始終認爲,做一個「真正」的報警,其中的應用程序甚至沒有運行是不可能在iPhone上... – user387184

+0

@ user387184沒有我的應用程序不能在後臺運行 – Rani

回答

0

我認爲當你設置fireDate問題發生,你可以試試這個:

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = [[NSDateComponents alloc] init]; 
[components setDay:day]; 
[components setMonth:month]; 
[components setYear:year]; 
[components setMinute:minutes]; 
[components setHour:hour]; 
NSDate *date = [calendar dateFromComponents:components]; 
localNotif.fireDate = date;