处理通知回调 - IOS

时间:2014-08-13 08:18:13

标签: ios callback notifications

我在按下UIButton时触发了通知。以下是代码。

UILocalNotification* notif = [[UILocalNotification alloc] init];
if (notif)
{
     notif.repeatInterval    = 0;
     notif.soundName         = UILocalNotificationDefaultSoundName;
     notif.alertAction       = NSLocalizedString(@"View", @"View");
     notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:0];
     notif.alertBody = s;
     notif.timeZone = [NSTimeZone defaultTimeZone];
     [[UIApplication sharedApplication]  presentLocalNotificationNow:notif];
}

现在,通知正在触发。点击通知我需要去其他一些UIViewController。但是目前,它重定向到调用它的UIViewController。请指导如何完成相同的工作。

1 个答案:

答案 0 :(得分:2)

您必须在AppDelegate.m文件中管理didReceiveLocalNotification和didFinishLaunchingWithOptions方法,如下所示

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    ...

    UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

    if(localNotif) {
         ...

    }
}

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
    if (app.applicationState == UIApplicationStateInactive )
    {
        NSLog(@"app not running");
    }
    else if(app.applicationState == UIApplicationStateActive )
    {
        NSLog(@"app running");
    }

    UINavigationController *navController = (UINavigationController*)self.window.rootViewController;

    YourViewController *yourVC = ... ;

    [navController presentViewController:yourVC animated:NO completion:nil];

}