简体   繁体   中英

push notification and receiving the data from it

I have a game the presents different scenes. every hour a push notification is sent to the phone which passes the level number of the scene, this number that cause different scenes to be presented. everything was working normally. suddenly the scene is not being updated. if i want to update the scene i have to click on a button which take me to another viewController when pressing done and coming back the rootViewController, it presents the new updated scene. Sometimes its working perfectly when pressing on push notification, other times, I have to press a button and go to another view Conroller in order to the updated scene to be presented

the method [self checkSceneLevel] that compare the numbers and present the scenes is called inside ViewWillLayoutSubviews.

I have [delegate addObserver:self forKeyPath:@"currentLevel" options:0 context:nil];

to check for the current level also in viewWillLayoutSubviews.

in the app deletgate i have this defined

`

-(void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler

{
    NSLog(@"userInfo= %@ ",userInfo);
    [self handleBackgroundNotification:userInfo];
     completionHandler(UIBackgroundFetchResultNewData);
}

`

   - (void)handleBackgroundNotification:(NSDictionary *)notification
{
    NSDictionary *aps = (NSDictionary *)[notification objectForKey:@"aps"];
    NSMutableString *alert = [NSMutableString stringWithString:@""];
    NSDictionary *info=aps[@"info"];
    if (info)
    {
        if (info[@"current_level"]) {
            _currentLevel = info[@"current_level"];
            [[NSUserDefaults standardUserDefaults] setObject:_currentLevel  forKey:@"currentLevel"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }



        }

can you shed a light on what is the reason behind this weird and sudden behaviour?

It would seem that you're having a problem correctly refreshing the data being displayed on the screen. This happens for some of the following reasons.

  • You're not refreshing the data early enough that the view is loaded with it
  • Maybe one of the viewControllers you specified are pushed onto the stack, forcing the view under it to be reloaded when you return to the rootViewController (viewDidLoad is called when you pop).

I would suggest checking to see if you're refreshing the views after the date from the push notification has been stripped and stored.

The problem turns out because i was putting the value feched into _level instead of self.level in app delegate. In other words i was using ivar instead of property which cause the error in KVO ,

more info about the difference between property and ivar is here

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