Reputation: 757
I wish to present a particular view Controller when the user taps on a view controller. I know there are many questions for the same thing that have been posted before and there have been many answers as well but none of it seems to be working for me. Here is the code in the 'didReceiveRemoteNotification' method in my 'AppDelegate'
if(application.applicationState == UIApplicationStateInactive) {
NSLog(@"Inactive");
//Show the view with the content of the push
if(userInfo)
{
NSLog(@"In inactive payload");
// Create a pointer to the C object
NSString *cId = [userInfo objectForKey:@"c"];
NSLog(cId);
PFObject *targetC = [PFObject objectWithoutDataWithClassName:@"C" objectId:cId];
// Fetch C object
[targetC fetchInBackgroundWithBlock:^(PFObject *object, NSError *error) {
C *c = [[C alloc] initWithPFObject:object];
// Show c view controller
if (!error) {
NSLog(c.title);
CDetailsViewController *viewController = [[CDetailsViewController alloc] initWithC:c];
//[self.navigationController pushViewController:viewController animated:YES];
[self.navigationController presentViewController:viewController animated:YES completion:nil];
}
}];
}
handler(UIBackgroundFetchResultNewData);
I am getting the exact data I have sent in my push as I can see from the log prints I get. Only problem is the view controller that I am trying to present/push is never seen when the notification is tapped. Any workaround for this? Any thing that I'm doing wrong? Any help is highly appreciated.
Upvotes: 0
Views: 752
Reputation: 2052
I assume its because didReceiveRemoteNotification: is called on a background thread and presentViewController might need to be called on the main thread.
The below code, forces the view controller to be presented in the main thread asynchronously Note: a block should never be synchronously run on the main thread
. To learn more about Grand Central Dispatch, see here.
dispatch_async(dispatch_get_main_queue(), ^
{
CDetailsViewController *viewController = [[CDetailsViewController alloc] initWithC:c];
[self.navigationController presentViewController:viewController animated:YES completion:nil];
}
Upvotes: 1
Reputation: 5047
If your app is in background, the method "application:didReceiveRemoteNotification:" is never called.
You need add this, in the method: "application:didFinishLaunchingWithOptions:"
// At the end of the method you force call didNotification:
// Handle any notifications.
if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]!=nil)
{
NSDictionary * aPush =[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
[self application:application didReceiveRemoteNotification:aPush];
}
return YES;
}
If your app is started due to the user tap a notification, in the launchOptions dict, the notification payload will be attached.
Upvotes: 3