在 UILocalNotification 之后加载视图

发布于 2024-12-11 06:48:58 字数 3675 浏览 0 评论 0原文

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

它们是三个视图 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]; 
}

上面的代码是在应用程序中编写的 完成了应用程序委托文件的启动方法

那么我做什么这里想做的是当通知 1 的警报框出现时v1 应该加载,当通知 2 的警报出现时 v2 应该加载。

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

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

提前致谢

I would like to display view on the touch of the alert of the local notification, my problem is given below

Their are three views v1, v2,v3 and i have triggered code on the button of these three different views the code for that is given below and varies from different views

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];

The code to trigger notification in the second view is

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];

Now inside the app delegate i am handling the notification with the code given below

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]; 
}

The above code is written in application did finish launch method of the app delegate file

So what i want to do here is when alert box for notification 1 comes v1 should load, when alert for notification 2 comes v2 should load.

but the thing is v1 is getting loaded perfectly but when it comes to v2 and v3 the userInfo for them is null and v1 is loaded by default. I did the same in the UIApplication delegate method for handling local notification but still the same results.

Kindly provide me some guidance or links for the same.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十六岁半 2024-12-18 06:48:58

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

You never check your key's value you just check to see if it's there. Look at your first if statement. It's a good thing you used elses or you would have loaded all of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文