我正在开发适用于iPad的iOS应用。我正在使用名为HelpShift的服务推送通知。我想在用户点击通知时运行一段代码。它实际上在应用程序处于活动状态时有效,但是当它处于后台或处于非活动状态时,它不起作用。这是我的代码:
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if ([[userInfo objectForKey:@"origin"] isEqualToString:@"helpshift"]) {
UIApplicationState state = [application applicationState];
if (state == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"You were answered in HelpShift"
message:@"Hello"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Show", nil];
[alertView show];
} if (state== UIApplicationStateBackground) {
UIViewController *vc = self.window.rootViewController;
[[Helpshift sharedInstance] handleNotification:userInfo withController:vc];
[self showHelpShift];
} if (state == UIApplicationStateInactive) {
UIViewController *viewController =
[[UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:NULL] instantiateViewControllerWithIdentifier:@"home"];
[[Helpshift sharedInstance] handleNotification:userInfo withController:viewController];
}
}
}
- (void) showHelpShift {
UIViewController *vc = self.window.rootViewController;
[[Helpshift sharedInstance] showSupport:vc];
}
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1){
UIViewController *vc = self.window.rootViewController;
[[Helpshift sharedInstance] showSupport:vc];}
}
正如您所看到的,问题是[self showHelpShift]没有被调用或者它被提前调用。
答案 0 :(得分:2)
实施application:didFinishLaunchingWithOptions:
并在UIApplicationLaunchOptionsRemoteNotificationKey
词典中查找launchOptions
键。