简体   繁体   中英

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?

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.

Now in app delegate in application didReceiveLocalNotification: I showed an alert.

        [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. Have your UIViewController register for that notification, and update its display.

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:

[[NSNotificationCenter defaultCenter] removeObserver:self];

In you AppDelegate

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

In your 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. You can update label here. Hope this helps.

Problem is you are updating the DB but not the viewContoller .

You just need to refresh or reload your 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

[yourLabel setText:UpdatedValue];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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