本地通知在ios中不能很好地工作



我有一个应用程序,我使用本地通知,如果用户没有打开应用程序的一段时间。

如果用户通过本地通知打开应用程序,我的DB得到更新,并给用户的帐户信用,我显示警报"你有额外的信用"。

信用更新在我的DB当用户单击OK,但我的视图控制器没有得到刷新,它没有显示信用更新的标签在那个时候。

当我去到另一个视图并返回时,它显示。

当用户从AlertView点击OK时,我应该如何获得更新的分数?

提前感谢....

编辑

[[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上所做的代码。

现在在app delegate in application 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];

    }

在点击Ok按钮时,我希望我的视图控制器得到刷新或重新加载。

发送NSNotification(与本地通知不同),无论您更新信用。让您的UIViewController注册该通知,并更新其显示。

标准通知示例,到post:

[[NSNotificationCenter defaultCenter] postNotificationName:@"MyAppCreditsUpdatedNotification" object:nil];

接收(在你的视图控制器中):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateCredits:) name:@"MyAppCreditsUpdatedNotification" object:nil];

别忘了把这个放到你的dealloc中:

[[NSNotificationCenter defaultCenter] removeObserver:self];

In you 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:方法将被调用,每当用户点击"OK"按钮在本地通知。你可以在这里更新标签。

问题是您正在更新DB而不是viewContoller

你只需要刷新或重新加载你的viewController

试试这个,这可能对你有帮助。

[self.view setNeedsDisplay];

或其他而不是重新加载完整的ViewController,你可以在点击"确定"按钮时手动更改label的值

[yourLabel setText:UpdatedValue];

最新更新