推送通知后运行功能
问题描述:
我正在为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];}
}
因此,大家可以看到,问题是[自showHelpShift]不会被调用或者被调用提前。
答
执行application:didFinishLaunchingWithOptions:
并在launchOptions
字典中查找UIApplicationLaunchOptionsRemoteNotificationKey
键。
+0
我不知道你的意思。我试过UIApplicationLaunchOptionsRemoteNotificationKey,但应用程序崩溃... – 2013-03-24 13:12:04
通知发送到传递给vapplicationDidLaunch消息的字典中。上述消息不在发布时发送。 – 2013-03-24 12:28:45
那么?我有什么编码? – 2013-03-24 12:37:09
所以你实现了推送通知,但从来没有读过Apple的优秀指南呢?打开“本地通知和推送通知”指南并阅读“处理本地和远程通知”部分。 – 2013-03-25 12:11:13