0

AppDelegate 可以在收到推送通知后在视图控制器中运行“功能”吗?

该应用程序包含三个选项卡,第三个选项卡需要在应用程序收到推送通知时重新加载其 webview。

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {

}
4

1 回答 1

3

是的,您需要viewController为特定通知注册您的通知,例如,当您收到远程通知时,只需发布​​自定义通知并在您的视图控制器中,注册此通知/收听此通知并执行您想要执行的方法。

当你收到remoteNotification

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {

[[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];
}

在您viewController.m注册此通知的视图控制器

- (void)viewDidLoad {

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

    [super viewDidLoad];

}

然后实现选择器

-(void)pushNotificationReceived{

// do your stuff

}

最后不要忘记在您的dealloc方法中取消注册通知

-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
于 2013-03-04T20:41:19.097 回答