user3224534 user3224534 - 11 months ago 75
Swift Question

detect specific localNotification opened

I am scheduling timer and sending some local notification for user about some data, example is - if there is some store near.

func configureNotification(shop: Shop) {
let notification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 0)
notification.alertBody = "There is a store \( near!"//Localized().near_shop_string + shopName
notification.alertAction = "Swipe to see offer!"//Localized().swipe_to_see_string
notification.soundName = UILocalNotificationDefaultSoundName

When app running in background if there is some store near users coordinates, there is a local notification.

For example, there is three local notification received about different stores and user swipes the second one and make app active from it.

The question is, to recognize from what specific notification applicationDidBecomeActive was launched, some launcOptions, as for push notifications from server? Any solutions?

Answer Source

You need handle it in didReceiveLocalNotification delegate method

func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
        // do your jobs here

notification param will contain info for every notification.

Also launchOptions has a key UIApplicationLaunchOptionsLocalNotificationKey that contains notification.

You can get it like

let localNotification:UILocalNotification = launchOptions.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey)