Ios 点击通知时启动特定视图控制器
我希望在用户点击视图控制器时呈现一个特定的视图控制器。我知道以前有很多关于同一件事的问题,也有很多答案,但似乎没有一个对我有用。以下是我的“AppDelegate”中的“DidReceiveEmotentification”方法中的代码Ios 点击通知时启动特定视图控制器,ios,objective-c,apple-push-notifications,Ios,Objective C,Apple Push Notifications,我希望在用户点击视图控制器时呈现一个特定的视图控制器。我知道以前有很多关于同一件事的问题,也有很多答案,但似乎没有一个对我有用。以下是我的“AppDelegate”中的“DidReceiveEmotentification”方法中的代码 if(application.applicationState == UIApplicationStateInactive) { NSLog(@"Inactive"); //Show the view with the content of
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);
我得到了推送过程中发送的确切数据,从我得到的日志打印中可以看出。唯一的问题是当点击通知时,我试图呈现/推送的视图控制器从未出现。有解决办法吗?我做错什么了吗?非常感谢您的帮助。我想这是因为DidReceiveMemotentification:在后台线程上调用,而presentViewController可能需要在主线程上调用。
下面的代码强制视图控制器在主线程中异步显示。
注意:块不应在主线程上同步运行。
。要了解有关Grand Central Dispatch的更多信息,请参阅
我假设这是因为在后台线程上调用了DidReceiveEmotentification:并且可能需要在主线程上调用presentViewController。
下面的代码强制视图控制器在主线程中异步显示。
注意:块不应在主线程上同步运行。
。要了解有关Grand Central Dispatch的更多信息,请参阅
如果您的应用程序位于后台,则永远不会调用方法“application:didReceiveRemoteNotification:” 您需要在方法“application:didFinishLaunchingWithOptions:”中添加此选项:
如果您的应用程序由于用户点击通知而启动,则在launchOptions dict中,将附加通知负载。如果您的应用程序位于后台,则不会调用“application:DidReceiveMemoteNotification:”方法 您需要在方法“application:didFinishLaunchingWithOptions:”中添加此选项:
如果您的应用程序由于用户点击通知而启动,则在launchOptions dict中,将附加通知负载。如果需要,我会添加它。。但我可以看到“DidReceiveMemoteNotification”在通知点击时被调用,我的数据在“userinfo”中可用。如果需要的话,我会添加它。。但是我可以看到“DidReceiveMemoteNotification”在一个通知点击中被调用,我的数据在“userinfo”中可用。你能详细说明一下这里发生了什么吗?你能详细说明一下这里发生了什么吗?
dispatch_async(dispatch_get_main_queue(), ^
{
CDetailsViewController *viewController = [[CDetailsViewController alloc] initWithC:c];
[self.navigationController presentViewController:viewController animated:YES completion:nil];
}
// 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;
}