[英]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.