[英]Local Notification is not working well in ios
如果用户有一段时间未打开应用程序,则我正在使用一个本地通知应用程序。
如果用户通过本地通知打开该应用程序,则我的数据库将更新并向该用户的帐户提供积分,并且我将显示警报“您有额外的积分”。
当用户单击“确定”但我的视图控制器没有刷新并且此时未在标签上显示更新的积分时,在我的数据库中更新积分。
当我转到另一个视图并返回时,它显示。
用户单击AlertView中的“确定”时,如何获得更新的分数?
提前致谢....
编辑
[[UIApplication sharedApplication]cancelAllLocalNotifications];
NSDate *today=[NSDate date];
NSLog(@"%@",today);
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@"dd-MMM-yyyy hh:mm:ss a"];
NSTimeInterval secondsInEightHours = 20;
NSString *tt=[dateFormat1 stringFromDate:today];
//tt=@"20-Apr-2013 06:39:32 PM";
NSDate *Ass_date=[dateFormat1 dateFromString:tt];
Ass_date=[Ass_date dateByAddingTimeInterval:secondsInEightHours];
NSLog(@"%@",tt);
NSLog(@"%@",today);
NSLog(@"%@",Ass_date);
//[[UIApplication sharedApplication]cancelAllLocalNotifications];
UILocalNotification *localNotif1 = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication]cancelLocalNotification:localNotif1];
if (localNotif1 == nil)
return;
localNotif1.fireDate = Ass_date;
localNotif1.timeZone = [NSTimeZone defaultTimeZone];
// Notification details
localNotif1.alertBody = @"You have not played for a long time. Play and get 250 Stars credits for free!";
// Set the action button
localNotif1.alertAction = @"Get It Now";
localNotif1.soundName = UILocalNotificationDefaultSoundName;
//localNotif.applicationIconBadgeNumber = 1;
//localNotif1.applicationIconBadgeNumber ++;
// Schedule the notification
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif1];
[localNotif1 release];
这是我在viewcontroller的ViewDidAppear上执行的代码。
现在在应用程序didReceiveLocalNotification中的应用程序委托中:我显示了一个警报。
[self localNotif];
////NSLog(@"Recieved Notification %@",localNotif);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:@"250 Stars have been credited to your account" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
在单击警报中的“确定”按钮时,我希望我的视图控制器刷新或重新加载。
无论您在哪里更新信用额,都可以发送NSNotification
(与本地通知不同)。 让您的UIViewController
注册该通知,并更新其显示。
标准通知示例,要发布:
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyAppCreditsUpdatedNotification" object:nil];
接收(这在您的视图控制器中):
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCredits:) name:@"MyAppCreditsUpdatedNotification" object:nil];
不要忘记将其放入您的dealloc中:
[[NSNotificationCenter defaultCenter] removeObserver:self];
在您的AppDelegate
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
[[NSNotificationCenter defaultCenter] postNotificationName:@"creditsUpdated" object:nil];
}
在您的viewcontroller
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateScore:) name:@"creditsUpdated" object:nil];
现在,只要用户在本地通知上单击“确定”按钮,就会调用updateScore:
方法。 您可以在此处更新标签。 希望这可以帮助。
问题是您要更新数据库,而不是viewContoller
。
您只需要刷新或重新加载viewController
。
试试这个,这可能对您有帮助。
[self.view setNeedsDisplay];
否则,除了重新加载完整的ViewController
您还可以在单击“确定”按钮时手动更改label
值
[yourLabel setText:UpdatedValue];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.