iOS - Need to distinguish UILocalNotification in application:didReceiveLocalNotification:

我怕爱的太早我们不能终老 提交于 2019-12-10 16:44:21

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/11377197/ios-need-to-distinguish-uilocalnotification-in-applicationdidreceivelocalnoti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!