简体   繁体   English

本地通知在ios中无法正常运行

[英]Local Notification is not working well in ios

I am having an app in which I am using the local notification if the user has not opened the app for sometime. 如果用户有一段时间未打开应用程序,则我正在使用一个本地通知应用程序。

If the user opens the app via local notification my DB gets updated and giving credits to the user's account and I am showing the alert "You have got extra credits". 如果用户通过本地通知打开该应用程序,则我的数据库将更新并向该用户的帐户提供积分,并且我将显示警报“您有额外的积分”。

Credits are updated in my DB When user clicks OK but my viewcontroller doesn't get refresh and it is not showing credits updated on a label at that time. 当用户单击“确定”但我的视图控制器没有刷新并且此时未在标签上显示更新的积分时,在我的数据库中更新积分。

When i go to the another view and came back it shows. 当我转到另一个视图并返回时,它显示。

How should I get updated scores at the time when user clicks OK from AlertView? 用户单击AlertView中的“确定”时,如何获得更新的分数?

Thanks in advance.... 提前致谢....

Edit 编辑

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

This is the code i did on my viewcontroller's ViewDidAppear. 这是我在viewcontroller的ViewDidAppear上执行的代码。

Now in app delegate in application didReceiveLocalNotification: I showed an alert. 现在在应用程序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];



    }

On click of an Ok button from alert I want I want my view controller to get refresh or reloaded. 在单击警报中的“确定”按钮时,我希望我的视图控制器刷新或重新加载。

Send an NSNotification (not the same as a local notification) from wherever you update the credits. 无论您在哪里更新信用额,都可以发送NSNotification (与本地通知不同)。 Have your UIViewController register for that notification, and update its display. 让您的UIViewController注册该通知,并更新其显示。

Standard notification example, to post: 标准通知示例,要发布:

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

To receive (this goes in your view controller): 接收(这在您的视图控制器中):

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

Don't forget to put this in your dealloc: 不要忘记将其放入您的dealloc中:

[[NSNotificationCenter defaultCenter] removeObserver:self];

In you AppDelegate 在您的AppDelegate

  - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {    
       [[NSNotificationCenter defaultCenter] postNotificationName:@"creditsUpdated" object:nil];
    }

In your viewcontroller 在您的viewcontroller

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

Now updateScore: method will be called whenever user taps "OK" button on local notification. 现在,只要用户在本地通知上单击“确定”按钮,就会调用updateScore:方法。 You can update label here. 您可以在此处更新标签。 Hope this helps. 希望这可以帮助。

Problem is you are updating the DB but not the viewContoller . 问题是您要更新数据库,而不是viewContoller

You just need to refresh or reload your viewController . 您只需要刷新或重新加载viewController

Try this, this might help you. 试试这个,这可能对您有帮助。

[self.view setNeedsDisplay];

or else instead of reloading the complete ViewController you can just change the label value manually when yo click "OK" button 否则,除了重新加载完整的ViewController您还可以在单​​击“确定”按钮时手动更改label

[yourLabel setText:UpdatedValue];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM