Search code examples
iosswiftpush-notificationlocationuilocalnotification

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 \(shop.name) near!"//Localized().near_shop_string + shopName
    notification.alertAction = "Swipe to see offer!"//Localized().swipe_to_see_string
    notification.soundName = UILocalNotificationDefaultSoundName
    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

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?


Solution

  • 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)