如何在iPhone中设置闹钟并保存在本地通知中? [英] How to set the alarm in iPhone and save in local notification?

查看:108
本文介绍了如何在iPhone中设置闹钟并保存在本地通知中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iPhone中创建一个警报应用程序,该应用程序使用SQLite和本地通知.我有一个带有tableview的类,其中包含4行用于选择时间,重复间隔,声音等.当我选择所有这些因素时,这些都存储在全局变量中.

I am creating an alarm application in iPhone which makes use of SQLite and local notification. I have class with tableview which contains 4 rows for selecting time,repeat interval, sound etc. When I select all these factors these are stored in global variables.

在我选择这些行的表格视图中,我有一个保存按钮.在选择特定的行后单击保存按钮时,通过此全局变量将时间,重复和声音的值保存在SQLite数据库中.现在,我将此变量传递给通知对象,以便在单击保存按钮时设置警报.但是通知没有设置.

In my tableview where I am selecting these rows I have a save button. When I click on the save button after choosing the particular rows the values of time, repeat and sound are saved in SQLite database through this global variables. Now I am passing this variables to my notification objects so that alarm should get set when I click on the save button. But notification is not getting set.

这是我的通知代码:

//
//  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创建了一个方法.但是通知不起作用.我的开火日期显示为空值.

And in the appDelegate I have created a method for didReceiveNotification. But notification does not work. My fire date shows null value.

可能是什么问题?我想将值传递给存储在变量中的日期.

What might be the problem? I want to pass values to firedate that I stored in my variable.

推荐答案

我认为在设置fireDate时发生问题,您可以尝试以下方法:

I think problem happen when you set fireDate, you can try this:

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;

这篇关于如何在iPhone中设置闹钟并保存在本地通知中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆