我正在为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没有被调用或者它被提前调用。
发布于 2013-03-24 20:30:00
实现application:didFinishLaunchingWithOptions:并在launchOptions字典中查找UIApplicationLaunchOptionsRemoteNotificationKey密钥。
https://stackoverflow.com/questions/15598433
复制相似问题