How to send the same alarmid when the stop button is clicked in iPhone?
When I select time from time picker and click on the save button notification is set and at the same time values are inserted in the sqlite database. In my sqlite database, fields are alarmID,alarmtime,soundfile,alarmmessage. I am setting timestamp to my alarmid.
So when I create a new alarm a new alarmid is generated with the alarmtime at which开发者_JS百科 the time the alarm was set. I am setting my alarmid as a primary key in a table named AlarmsInformation table and the reference to this alarmid is generated in other table named PenaltyTransaction table. So when the save button is clicked all the data i.e alarmID,alarmtime,soundfile,alarmmessage are inserted in the database.
When the notification is received in the app did finish launching I am calling an alertview method with 2 button on it Stop and Snooze respectively. So when a received and when my app is running then an alertview is displayed with 2 buttons Stop and Snooze. When stop button is clicked the current notification that is being presented should be stopped through it's respective alarmid. When snooze button is clicked the I am snoozing the current notification for 2 mins. This works properly.
But the values are not inserted properly in my Penaltytransaction table. When I click on the stop button it does not insert the alarmid that is being stopped and same is for snooze button.
This is my code.
//this is my save button code
-(IBAction)save{
if (app.timerdate == 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 scheduleNotification];
[self saveInDatabase];
}
}
//this is my schedulenotification code
-(void)scheduleNotification
{
timepicker = [[TTimePickerController alloc]init];
double double_date = (double)[[NSDate date] timeIntervalSince1970];
NSDate *now= [NSDate dateWithTimeIntervalSince1970:double_date];
NSMutableArray *array = [[NSUserDefaults standardUserDefaults]objectForKey:@"time"];
for (NSDate *date in array)
{
if ([date isEqualToDate:now])
{
itemDate = date;
NSLog(@"%@",itemDate);
}
else if([date earlierDate:now]){
itemDate = date;
}
else if([date laterDate:now])
{
itemDate =date;
}
}
Class cls = NSClassFromString(@"UILocalNotification");
if (cls!= nil) {
UILocalNotification *notif = [[cls alloc]init];
notif.fireDate = itemDate;
[app.dateFormatter setDateFormat:@"hh:mm a"];
newstring = [app.dateFormatter stringFromDate:notif.fireDate];
NSLog(@"new fire date:%@",newstring);
notif.timeZone = [NSTimeZone defaultTimeZone];
notif.alertBody = @"Alarm";
notif.alertAction = @"View";
notif.soundName = UILocalNotificationDefaultSoundName;
notif.applicationIconBadgeNumber=1;
notif.repeatInterval = 0;
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
NSNumber *timeStampObj = [NSNumber numberWithInt:timeStamp];
NSLog(@"%d",timeStampObj);
//am =[[Alarm alloc]init];
am.AlarmID =(int)timeStampObj;
NSLog(@"%@",am.AlarmID);
app = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
[app.newtest setObject:[NSNumber numberWithInteger:am.AlarmID]forKey:@"idtemp"];
//notif.userInfo = [NSDictionary dictionaryWithObject:newtemp forKey:@"idtemp"];
notif.userInfo = app.newtest;
NSLog(@"notif userinfo:%@",notif.userInfo);
[[UIApplication sharedApplication]scheduleLocalNotification:notif];
[[UIApplication sharedApplication]scheduleLocalNotification:notif];
[notif release];
}
}
// i am trying to store a dictionary object in my userinfo of notification.
//this is my insert database code where i am inserting into AlarmsInformation table
-(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);
am =[[Alarm alloc]init];
am.AlarmID =(int)timeStampObj;
NSLog(@"%@",am.AlarmID);
[app.dateFormatter setDateFormat:@"dd/MM hh:mm a"];
NSString *str = [app.dateFormatter stringFromDate:app.timerdate];
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_int(compiledStatement, 1, am.AlarmID);
sqlite3_bind_text(compiledStatement, 2, [str 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);
}
//this code is where i am setting the alertview to be displayed when the notification is received
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Class cls = NSClassFromString(@"UILocalNotification");
if (cls) {
UILocalNotification *notification = [launchOptions objectForKey:
UIApplicationLaunchOptionsLocalNotificationKey];
if (notification) {
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
[alarm showReminder:reminderText :notification];
array_dates = [[NSMutableArray alloc]init];
}
}
application.applicationIconBadgeNumber = 0;
[self.window makeKeyAndVisible];
return YES;
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
application.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo
objectForKey:kRemindMeNotificationDataKey];
//[viewController showReminder:reminderText];
notification.soundName = UILocalNotificationDefaultSoundName;
NSLog(@"%@",notification.soundName);
alarm = [[TAlarmController alloc]init];
[alarm showReminder:reminderText:notification];
NSLog(@"Recieved Notification %@",notification);
}
All this is working fine the notification is also displayed at proper time and the values are also inserted properly in the AlarmsInformation table. But when I click the stop button, its reference is not passed properly to the other table i.e PenaltyTransaction table.What is the problem.
Now I want this userinfo to be accesed in other class which contains the dictionary objects. How is this possible?
精彩评论