本文介绍了iOS - 需要区分应用程序中的UILocalNotification:didReceiveLocalNotification:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是交易...我需要一个方法来区分应用程序状态,我收到一个 UILocalNotification



有一种情况,对我来说我不明白。这是当应用程序当前在前台运行,并且用户拉下通知中心(iOS 5)时,应用程序将被调用 applicationWillResignActive:这是完全合乎逻辑的,因为它将不会在通知中心显示在其上时处于活动状态。但我怀疑 applicationDidEnterBackground:会在它已经退出活动的 BUT 之后被调用。所以当我收到UILocalNotifications当在通知中心我的应用程序将尝试处理他们在应用程序:didReceiveLocalNotification:,但当我点击从我的应用程序的通知是在通知中心列表,那么我的应用程序将以与在通知中心收到此通知时一样处理此通知。所以我只能以一种方式处理这些通知,但我需要以两种方式处理它们。



那么我如何区分这两种情况:




  • 当应用程式在前台时,使用者已下拉

    通知中心,在此期间

    UIApplicationState = UIApplicationStateInactive


  • 在前台,并且用户已下拉

    通知中心,并从列表中主动选择应用程序的

    通知( UIApplicationState =

    UIApplicationStateInactive




p>

我存储所有的通知,时间戳为00秒。所以在应用程序:didReceiveLocalNotification:我可以检查当前时间 [NSDate date] ,看看秒是否大于00?这意味着,不是iOS已触发通知,而是来自通知中心的用户。这当然是基于这样的事实,事实上通知将事实上由iOS以这种及时的方式递送。如果iOS将在01秒发送通知,我的逻辑将会中断。但是为了它,我只是做了一些日志,以查看iOS是否提供我的通知,似乎从控制台的输出判断通知实际上按毫秒精度(+/- 1)按时交付。当然,你不能将连接到计算机的开发设备与真实世界场景进行比较。

  2012-07-08 10:09:00.789 App [7535:707]  -  [AppDelegate应用程序:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification:method running 
2012-07-08 10:10:00.789 App [7535:707] [AppDelegate应用程序:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification:method running
2012-07-08 10:11:00.788 App [7535:707] - [AppDelegate application:didReceiveLocalNotification:] [Line 399] application :didReceiveLocalNotification:方法运行
2012-07-08 10:12:00.790 App [7535:707] - [AppDelegate应用程序:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification:method running
2012-07 -08 10:13:00.790 App [7535:707] - [AppDelegate应用程序:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification:method running
2012-07-08 10:14:00.789 App [7535:707 ] - [AppDelegate应用程序:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification:method running
2012-07-08 10:15:00.789 App [7535:707] - [AppDelegate application:didReceiveLocalNotification:] [Line 399 ] application:didReceiveLocalNotification:method running


解决方案


当您在第二种情况下收到本地通知时,您的应用程式会在您收到回电后变为有效状态用户正在选择返回到您的应用程序)。那么,如何存储您的本地通知和行动(例如)50毫秒后;如果您的应用程序状态在此时处于活动状态,则用户会从通知中心选择您的应用程序。


So here's the deal... I need a way to distinguish from what application state I received a UILocalNotification.

There is one scenario that for me I don't understand. That is when the app is currently running in the foreground and the user pulls down the Notification Center (iOS 5) then the app will get called applicationWillResignActive: which is perfectly logical because it will not be active when the Notification Center will be displayed over it. But I would suspect that also applicationDidEnterBackground: would get called right after it has resigned active BUT it does not. So when I receive UILocalNotifications when in the Notification Center my app will try to deal with them in application:didReceiveLocalNotification: but when I tap on a notification from my app that is in the Notification Center list then my app will deal with this notification the same way as it did when I received it in the Notification Center. So I can only handle these notifications in just one way but I would need to handle them in two ways.

So how would I distinguish between these two situations:

  • When the app is in the foreground and the user has pulled down the
    Notification Center and I receive a notification during that time
    (UIApplicationState = UIApplicationStateInactive)

  • When the app is in the foreground and the user has pulled down the
    Notification Center and actively chooses one of my app's
    notifications from the list (UIApplicationState =
    UIApplicationStateInactive
    )

EDIT:

I store all my notifications with a timestamp with 00 seconds. So inside application:didReceiveLocalNotification: I could check the current time [NSDate date] and see if the seconds are bigger than 00? That would mean that it's not iOS who has triggered the notification but the user from the notification center. This of course relays on the fact that the notifications will in fact be delivered by iOS in this timely manner. If iOS will deliver the notification at 01 seconds my logic will break. But for the sake of it I just did some logging to see when iOS delivers my notifications and it seems that judging from the output from the console the notifications are actually delivered on time with millisecond precision (+/- 1). Of course you can't compare a development device hooked up to the computer with a real world scenario.

2012-07-08 10:09:00.789 App[7535:707] -[AppDelegate application:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification: method running
2012-07-08 10:10:00.789 App[7535:707] -[AppDelegate application:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification: method running
2012-07-08 10:11:00.788 App[7535:707] -[AppDelegate application:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification: method running
2012-07-08 10:12:00.790 App[7535:707] -[AppDelegate application:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification: method running
2012-07-08 10:13:00.790 App[7535:707] -[AppDelegate application:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification: method running
2012-07-08 10:14:00.789 App[7535:707] -[AppDelegate application:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification: method running
2012-07-08 10:15:00.789 App[7535:707] -[AppDelegate application:didReceiveLocalNotification:] [Line 399] application:didReceiveLocalNotification: method running
解决方案

Could you just wait to see if your state becomes active shortly afterwards?

When you get a local notification in your second case, your app will be becoming active right after you get the callback (since the user is choosing to return to your app). So, how about storing your local notification and acting on it (say) 50ms later; if your application state is active at that point, the user selected your app from the notification center.

这篇关于iOS - 需要区分应用程序中的UILocalNotification:didReceiveLocalNotification:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 11:11