Reputation: 697
I have a form where the user fills in all the information such as name,event name,date to remind etc..So what I do is I set a notification based on a key like this
- (void)scheduleNotificationForNotificationID:(NSString *)notificationID1:(NSString *)notificationID2:(NSString *)notificationKey
{
//Set notification after confirmation of saved data
Class cls = NSClassFromString(@"UILocalNotification");
reminderNotification = [[cls alloc] init];
if (cls != nil)
{
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
[dateFormat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate *notificationDate = [dateFormat dateFromString:textField2.text];
reminderNotification.fireDate = notificationDate;
reminderNotification.timeZone = [NSTimeZone defaultTimeZone];
NSString *reminderText = [NSString stringWithFormat:@"%@ 's %@ on %@",textField.text,textField1.text,strDate];
reminderNotification.alertBody = reminderText;
reminderNotification.alertAction = @"View";
reminderNotification.soundName = @"apple_ring.mp3";
reminderNotification.applicationIconBadgeNumber = 1;
//Use name and event name as keys
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:notificationID1,@"Name",notificationID2,@"Event",notificationKey,@"Date",nil];
reminderNotification.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:reminderNotification];
}
}
textField holds Name and textField1 holds Event Name
So I have a save button action for saving each reminder,where if the button title is "Save",it will save the reminder and insert a new record,if it is "Done",it will edit the reminder details and record will get updated.
Now what's the problem with notification is not of improper firing or not firing etc..The problem is with notification alert body,this is the code of did Receive local notification:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
application.applicationIconBadgeNumber = 0;
NSString *reminderText = [notification.userInfo objectForKey:kReminder];
[self.addViewController performSelector:@selector(showReminder:) withObject:reminderText afterDelay:1];
}
So here is the action for notification alert body
//Notification alert
- (void)showReminder:(NSString *)text
{
self.reminderAlert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:reminderNotification.alertBody delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(220, 10, 40, 40)];
UIImage *image= [UIImage imageNamed:@"icon@2x.png"];
[imageView setImage:image];
[reminderAlert addSubview:imageView];
[imageView release];
[reminderAlert show];
[reminderAlert release];
}
Now after I edit the reminder/record details the previous notification gets cancelled any way,but my problem is the alert body is not the same as notification alert body,i.e. notification alert body get's updated,but not alert body i.e. according to name and event name say for example:
"Steve Job's Birthday on July 3rd",if this is initial event at the time of saving and if user updates/changes it to "Steve Job's Felicitation on July 3rd"
the alert body is holding the previous event name value,where as notification alert body is holding the correct and properly updated new reminder details.I tried assigning one alert body if the title is save,and another value if title is done,I mean I took several strings to hold the values of name and event name,for save and done actions,declared the string globally,tried to make the string value nil before I assign a reminder body in done action etc...nothing worked!
Hence I assigned the reminder notification alert body itself.Even this is not working :(
Where am I wrong....???
Please help me and thanks all in advance :)
Upvotes: 0
Views: 1339
Reputation: 697
I found out a proper solution for assigning the text to reminder alert body,i.e. as we are saving the notification based on three keys:
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:notificationID1,@"Name",notificationID2,@"Event",notificationKey,@"Date",nil];
We can use those keys to assign the text/body for alert view i.e.:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
application.applicationIconBadgeNumber = 0;
NSString *name = [notification.userInfo objectForKey:@"Name"];
NSString *event = [notification.userInfo objectForKey:@"Event"];
//Get the date of event and convert it to required format...
NSString *date = [notification.userInfo objectForKey:@"Date"];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init]autorelease];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dateVal = [dateFormat dateFromString:date];
[dateFormat setDateFormat:@"MMMM dd"];
NSString *eventDate = [dateFormat stringFromDate:dateVal];
NSString *reminderText = [NSString stringWithFormat:@"%@'s %@ on %@", name, event, eventDate];
[self.addViewController performSelector:@selector(showReminder:) withObject:reminderText afterDelay:1];
}
and we need not worry about any assignment of text in showReminderAction i.e.:
- (void)showReminder:(NSString *)text
{
self.reminderAlert = [[UIAlertView alloc] initWithTitle:@"Reminder" message:text delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[reminderAlert show];
[reminderAlert release];
}
That should get the things going our way,hope this helps some one,thanks :)
Upvotes: 1