在 UILocalNotification 之后加载视图

Posted

技术标签:

【中文标题】在 UILocalNotification 之后加载视图【英文标题】:Loading views after the UILocalNotification 【发布时间】:2011-10-21 04:46:08 【问题描述】:

我想在触摸本地通知的警报时显示视图,我的问题在下面给出

它们是 v1、v2、v3 三个视图,我在这三个不同视图的按钮上触发了代码,下面给出的代码因不同视图而异

notificationObject_ViewOne = [[UILocalNotification alloc]init];
notificationObject_ViewOne.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];
notificationObject_ViewOne.timeZone = [NSTimeZone defaultTimeZone];
notificationObject_ViewOne.alertBody = @"You are notified";
notificationObject_ViewOne.alertAction = @"View 1";
notificationObject_ViewOne.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;

NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 1", @"Key 1", nil];
notificationObject_ViewOne.userInfo = infoDict;


[[UIApplication sharedApplication]scheduleLocalNotification:notificationObject_ViewOne];
[notificationObject_ViewOne release];

SecondViewController *sec = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

[self.navigationController pushViewController:sec animated:YES];

[sec release];

第二个视图中触发通知的代码是

notificationObject_ViewTwo = [[UILocalNotification alloc]init];
notificationObject_ViewTwo.fireDate = [NSDate dateWithTimeIntervalSinceNow:35];
notificationObject_ViewTwo.timeZone = [NSTimeZone defaultTimeZone];
notificationObject_ViewTwo.alertBody = @"You are notified";
notificationObject_ViewTwo.alertAction = @"View 2";
notificationObject_ViewTwo.applicationIconBadgeNumber = [[UIApplication sharedApplication]applicationIconBadgeNumber]+1;

NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Object 2", @"Key 2", nil];
notificationObject_ViewTwo.userInfo = infoDict;

[[UIApplication sharedApplication]scheduleLocalNotification:notificationObject_ViewTwo];

[notificationObject_ViewTwo release];

ThirdViewController *ThirdObj = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];

[self.navigationController pushViewController:ThirdObj animated:YES];

[ThirdObj release];

现在在应用程序委托中,我正在使用下面给出的代码处理通知

UILocalNotification *localNotificationObject = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

if (localNotificationObject) 


    firstObject = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];

    NSLog(@"noti %@",[localNotificationObject.userInfo valueForKey:@"Key 1"]);

    // firstObject.title = @"FirstView";
    [self.window addSubview:firstObject.view];



else if(localNotificationObject)

    SecondViewController *secondObject = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

    NSLog(@"noti %@",[localNotificationObject.userInfo valueForKey:@"Key 2"]);

    [self.window addSubview:secondObject.view];

    //secondObject.title = @"Second View";


else if(localNotificationObject)

    ThirdViewController *thirdObject = [[ThirdViewController alloc]initWithNibName:@"ThirdViewController" bundle:nil];

    NSLog(@"noti %@",[localNotificationObject.userInfo valueForKey:@"Key 3"]);

    [self.window addSubview:thirdObject.view];

    // thirdObject.title = @"Third View";

else

    firstObject = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];
    UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:firstObject];


    [self.window addSubview:navC.view]; 

上面的代码是写在application did finish launch method of the app delegate file

所以我在这里要做的是,当通知 1 的警报框出现 v1 时应该加载,当通知 2 的警报出现时应该加载 v2。

但问题是 v1 正在完美加载,但是当涉及到 v2 和 v3 时,它们的 userInfo 为 null 并且默认情况下会加载 v1。我在处理本地通知的 UIApplication 委托方法中做了同样的事情,但结果仍然相同。

请为我提供一些指导或链接。

提前致谢

【问题讨论】:

【参考方案1】:

您永远不会检查密钥的值,而只是检查它是否存在。查看您的第一个 if 语句。你使用 else 是件好事,否则你会加载所有这些。

【讨论】:

以上是关于在 UILocalNotification 之后加载视图的主要内容,如果未能解决你的问题,请参考以下文章

将 UILocalNotification 同步到 Watch

如何在 Swift 中使用 UILocalNotification

检测从哪个 UILocalNotification 打开应用程序

在 UILocalNotification 中播放录制的音频

UILocalNotification 特殊字符警报正文

带有振动的 UILocalNotification