简体   繁体   English

推送通知并从中接收数据

[英]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. 如果我想更新场景,我必须单击一个按钮,当按下“完成”并返回rootViewController时,它将带我到另一个viewController,它将显示新的更新场景。 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. 比较数字并显示场景的方法[self checkSceneLevel]在ViewWillLayoutSubviews内部调用。

I have [delegate addObserver:self forKeyPath:@"currentLevel" options:0 context:nil]; 我有[代理addObserver:self forKeyPath:@“ currentLevel”选项:0上下文:无];

to check for the current level also in viewWillLayoutSubviews. 在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). 也许您指定的viewControllers之一被压入堆栈,迫使您返回rootViewController时重新加载其下的视图(弹出时调用viewDidLoad)。

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. 出现问题的原因是,我在应用程序委托中将值设置为_level而不是self.level。 In other words i was using ivar instead of property which cause the error in KVO , 换句话说,我使用的是ivar而不是导致KVO错误的属性,

more info about the difference between property and ivar is here 有关property和ivar之间区别的更多信息在这里

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

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