单击本地通知时显示自定义UIViewcontroller

时间:2014-08-22 07:12:25

标签: ios objective-c uiviewcontroller uilocalnotification

我在 applicationDidEnterBackground 中创建本地通知

NSString *word;
word=[DictionaryHelper getWordOfTheDay];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.alertBody =("%@",word);
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication]     applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

显示本地通知并打开应用的 rootviewcontroller(初始)

但是我想在点击本地通知时打开特定的UIViewController ...

2 个答案:

答案 0 :(得分:0)

只需检查applicationDidFinishLaunchingWithOptions中的词典userInfo即可。如果它包含密钥UIApplicationLaunchOptionsLocalNotificationKey,则表示该应用程序是通过点击本地通知启动的,您可以在userInfo字典中获取对通知对象的引用。

更多信息:UIApplicationDelegate Protocol Reference

要呈现不同的UIViewController,只需为此情况初始化或实例化viewController,并将其设置为rootVC。​​

您还可以使用该方法来回复通知:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

答案 1 :(得分:0)

在didReceiveLocalNotification中:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"Local notification received: %@", notification.alertBody);
    if ([notification.userInfo[@"notificationName"] isEqualToString:@"myLocalNotification"]) {
        [self.window.rootViewController performSegueWithIdentifier:@"MainToNotification" sender:nil];
    }
}

我的自定义信息: enter image description here