检测打开的特定 localNotification

detect specific localNotification opened

我正在安排计时器并向用户发送有关某些数据的一些本地通知,例如 - 如果附近有商店。

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

当应用程序 运行 在后台时,如果用户坐标附近有商店,则会有本地通知。

例如,收到三个关于不同商店的本地通知,用户滑动第二个通知并从中激活应用程序。

问题是,要识别从什么特定通知应用程序DidBecomeActive 启动,一些launcOptions,至于从服务器推送通知?有什么解决办法吗?

你需要在didReceiveLocalNotification委托方法

中处理它
func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
        // do your jobs here
}

notification 参数将包含每个通知的信息。

另外 launchOptions 有一个包含通知的密钥 UIApplicationLaunchOptionsLocalNotificationKey

你可以像

一样得到它
let localNotification:UILocalNotification = launchOptions.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey)