Reputation: 335
I've got some troubles with triggering push notifications. I need to open specific VC when user taps on notification which has some payload for it. Considering case that app is background I'm using this code
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
if ((application.applicationState == .inactive || application.applicationState == .background)) {
//open specific VC depends on notification payload
}
}
Here the problems starts: 1) I cant push VC from AppDelegate, only by setting new rootController 2) I have to use this code from AppDelegate's didReceiveRemoteNotification method
Upvotes: 0
Views: 190
Reputation: 852
Please refer to this answer: How to make your push notification Open a certain view controller?
Use this code in AppDelegate to detect if the app is opened from the notification. Before the app has become active, Set the initial VC when the app state is UIApplicationStateInactive. Write your code to set your VC as well as the content within that VC.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
if(application.applicationState == UIApplicationStateActive) {
//app is currently active, can update badges count here
} else if(application.applicationState == UIApplicationStateBackground){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
} else if(application.applicationState == UIApplicationStateInactive){
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
}
Upvotes: 3