我正在为我的 iphone“谚语”应用程序使用本地通知。每天(上午 11 点 53 分)应该将我的应用程序中的一句话通知给用户。我的问题是,我没有得到任何适当的参考来使用 userinfo 属性,所以我试图以另一种方式做到这一点。我使用了一个整数变量,每次都会增加,当应用程序收到通知时,但这也不能正常工作,本地通知每天都显示相同的说法..请帮我解决这个问题.. . 这是我的代码.....
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar] ;
NSDateComponents *componentsForReferenceDate =
[calendar components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit ) fromDate:[NSDate date]];
[componentsForReferenceDate setDay:11] ;
[componentsForReferenceDate setMonth:6] ;
[componentsForReferenceDate setYear:2013] ;
NSDate *referenceDate = [calendar dateFromComponents:componentsForReferenceDate] ;
// set components for time 7:00 a.m.
NSDateComponents *componentsForFireDate =
[calendar components:(NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit| NSSecondCalendarUnit ) fromDate: referenceDate];
[componentsForFireDate setHour:11] ;
[componentsForFireDate setMinute:53] ;
[componentsForFireDate setSecond:0] ;
NSDate *fireDateOfNotification = [calendar dateFromComponents: componentsForFireDate];
// Create the notification
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
DataHandler *obj=[[DataHandler alloc] init];
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];//fireDateOfNotification ;
notification.timeZone = [NSTimeZone localTimeZone] ;
notification.alertBody = [obj.proverb objectAtIndex:sectionindex];//[objz[NSString stringWithFormat: @"അകലത്തെ ബന്ധുവിനേക്കാൾ അരികത്തെ ശത്രു നല്ലത് ."] ;
notification.alertAction = @"go back";
notification.userInfo= [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Some information"] forKey:@"information"];
notification.repeatInterval= NSDayCalendarUnit ;
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification] ;
}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
DataHandler *obj=[[DataHandler alloc] init];
static int i=0;
NSLog(@"hai");
UILabel *label=[UILabel alloc];
UILabel *label1=[UILabel alloc];
label.text=[obj.explanation objectAtIndex:i];
label1.text=[obj.proverb objectAtIndex:i];
UIAlertView *message = [[UIAlertView alloc] initWithTitle:label1.text
message:label.text
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
i++;
sectionindex++;
}