Preform function inside ViewController from AppDel

2019-08-16 08:49发布

Can an AppDelegate run a "function" in a viewcontroller on receipt of a push notification?

The application contains three tabs, the third needs to reload its webview when the application gets a push notification.

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

}

1条回答
\"骚年 ilove
2楼-- · 2019-08-16 09:15

yes, you need to register your viewController for the particular notification, e.g in your case when you receive a remote notification, just post a custom notification and in your viewController, register this notification/listen to this notification and perform the method you want to perform.

when you receive remoteNotification

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

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

in your viewController.m register the viewcontroller for this notification

- (void)viewDidLoad {

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

    [super viewDidLoad];

}

and next implement the selector

-(void)pushNotificationReceived{

// do your stuff

}

and finally don't forget to unregister from notification in your dealloc method

-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
查看更多
登录 后发表回答