【问题标题】:detect specific localNotification opened检测特定的本地通知打开
【发布时间】:2016-08-22 10:42:08
【问题描述】:

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

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

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

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

问题是,要识别从哪个特定通知 applicationDidBecomeActive 启动,一些 launcOptions,至于来自服务器的推送通知?有什么解决办法吗?

【问题讨论】:

    标签: ios swift push-notification location uilocalnotification


    【解决方案1】:

    你需要在didReceiveLocalNotification委托方法中处理它

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

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

    launchOptions 也有一个包含通知的键 UIApplicationLaunchOptionsLocalNotificationKey

    你可以得到它像

    let localNotification:UILocalNotification = launchOptions.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey)
    

    【讨论】:

    • 好的,很酷,我会收到 3 个通知,并在那里处理它们,但是我如何知道哪个特定的通知应用程序处于活动状态?
    • 启动选项可以从方法 didFinishLaunchingWithOptions 如果应用程序没有关闭,只是发送到后台,这个方法不执行
    • didReceiveLocalNotification 在应用从本地通知变为活动状态时执行?
    • 所以 didReceiveLocalNotification 将在您从通知中打开应用程序后进行处理,并且它将变为活动状态。如果您需要在应用程序后处理,请再次打开。你不会处理它。
    • 是的,它会执行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    相关资源
    最近更新 更多