iphone - 本地通知在 ios 中运行不正常

标签 iphone ios objective-c uilocalnotification

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

如果用户通过本地通知打开应用程序,我的数据库会更新并为用户帐户提供积分,并且我会显示“您已获得额外积分”的警报。

我的数据库中的信用更新当用户单击“确定”时,但我的 View Controller 没有刷新,并且当时没有在标签上显示更新的信用。

当我转到另一个 View 并返回时,它显示了。

当用户从 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 上做的代码。

现在在 application didReceiveLocalNotification: 的 app delegate 中,我显示了一个警报。

        [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];



    }

单击警报中的“确定”按钮后,我希望我的 View Controller 能够刷新或重新加载。

最佳答案

从更新学分的任何地方发送一个 NSNotification(与本地通知不同)。让您的 UIViewController 注册该通知,并更新其显示。

标准通知示例,发布:

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

接收(这进入你的 View Controller ):

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

不要忘记将其放入您的 dealloc 中:

[[NSNotificationCenter defaultCenter] removeObserver:self];

关于iphone - 本地通知在 ios 中运行不正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16833198/

相关文章:

ios - 如何使用 AFNetworking 或 STHTTPRequest 来请求 SOAP Web 服务?

iphone:安全的 restfull 服务器“此服务器的证书无效

ios - 弹出 Objective -C 时如何保留 ViewController

ios - CAKeyframeAnimation动画开始没有第一帧(路径)

iphone - 在获取 UITableView Section 和 Row 之后...这段代码做了什么?

iphone - 日历中的EKEvent如何设置不闹钟

ios - 覆盖 3.5 英寸 iOS 设备的字体大小

ios - 2.10 iPhone 应用程序也必须在 iPad 上运行,无需修改,iPhone 分辨率和 2X iPhone 3GS 分辨率

ios - 将图像保存在 NSUserDefaults 中?

iPhone 由于 stringWithUTF8String 导致内存泄漏