Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if app got focus due to UILocalNotification

I'm making an app that keeps track of some reminders that repeats with an user defined interval.

I've made it so when the alert displays, the action title says "Renew". When you click this, the app opens, and here I want to create the next reminder, but the problem is that I don't know if the app opens because the notification button was tapped or if the notification fired while the app was running.

Anyone got any ideas?

like image 888
oskob Avatar asked Oct 03 '11 15:10

oskob


1 Answers

The 'correct' way to do this is to examine your NSApplication's applicationState property in the application:didReceiveRemoteNotification: method of your delegate.

From the documentation for handling local notifications:

iOS Note: In iOS, you can determine whether an application is launched as a result of the user tapping the action button or whether the notification was delivered to the already-running application by examining the application state. In the delegate’s implementation of the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification: method, get the value of the applicationState property and evaluate it. If the value is UIApplicationStateInactive, the user tapped the action button; if the value is UIApplicationStateActive, the application was frontmost when it received the notification.

This is similar to your solution using flags set in applicationWillEnterForeground and applicationDidBecomeActive but with system support.

like image 129
Robin Summerhill Avatar answered Sep 30 '22 16:09

Robin Summerhill