2016-02-11 71 views

回答

-1

您可以使用您用来发送通知的方法进行更新。

3

我认为你无法知道你的本地通知何时发出,主要是因为你可以安排你的本地通知或马上发送,以防你的应用程序在后台。

根据Apple

您安排致电scheduleLocalNotification:本地通知。该应用程序使用UILocalNotification对象中指定的发送日期。或者,您可以通过调用presentLocalNotificationNow:方法立即显示通知。

应用程序在后台运行并且某些消息,数据或其他物品到达时可能会发现有用的通知,这些通知可能会对用户有用。在这种情况下,应用可以使用UIApplication方法presentLocalNotificationNow:(iOS为应用提供有限的时间在后台运行)立即显示通知。

没有办法知道什么时候发送了UILocalNotification,但是当它收到时就是某种情况。你的情况,我想你要安排UILocalNotification让我们通过部分分为:

  • 当应用没有打开,用户触摸它

    一个委托通知交付iOS应用程序实现了application:didFinishLaunchingWithOptions:方法来处理本地通知。它使用UIApplicationLaunchOptionsLocalNotificationKey键从启动选项字典中获取关联的UILocalNotification对象。所以你可以通过触摸UILocalNotification知道应用程序的开启位置。请看下面的代码:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    
        if let launchNotification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as? UILocalNotification { 
         // do anything you want update UIViewController, etc 
        } 
    
        return true 
    } 
    
  • 应用程序运行时,此通知被传递,它

    一旦用户触摸用户触摸UILocalNotification您使用didReceiveLocalNotification方法可以处理它,做任何你想在里面。

  • 应用程序运行时,通知交付,用户不要去碰它

万一用户不碰UILocalNotification你可以做知道的唯一的事您发送的UILocalNotification与您之前计划的UILocalNotifications保持一个列表,并在稍后检查(这不是一个很好的解决方案,但它的工作)。

然而始终存在UIApplicationWillEnterForegroundNotification可用于通知要更新,该应用程序将在前台以这种方式进入像UIViewController

private var foregroundNotification: NSObjectProtocol! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    foregroundNotification = NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationWillEnterForegroundNotification, object: nil, queue: NSOperationQueue.mainQueue()) { 
     [unowned self] notification in 

     print("Notified ViewControllerB") 
    } 
} 

deinit { 
    // make sure to remove the observer when this view controller is dismissed/deallocated 

    NSNotificationCenter.defaultCenter().removeObserver(foregroundNotification) 
} 

我认为你必须正确你需要什么思考在用户不点击它的情况下收到您的通知时在您的应用程序中执行操作。

我希望这对你有所帮助。

+0

谢谢你很多,我认为当用户没有点击通知但在通知发送后进入应用程序时,我更加接近更新视图。但似乎我每次打开应用程序时都会收到“通知ViewControllerB”,而不仅仅是在发出通知之后。我认为这是因为它没有正确地放置或在我的应用程序的正确位置。 –

+0

@LudvigSørensen不,如果'UIViewController'仍然是内存,你会收到通知,仅此而已。 –

+0

哦,好的。我正在构建这款日常挑战应用程序,用户每天都会遇到挑战,然后才能完成。我想在发送通知后立即运行的功能就是取新的挑战功能,这样视图就会更新并给用户一个新的挑战。但唯一的问题是,当我在你给我的代码中运行该函数时,每次用户重新输入应用程序时,视图都会更新。我只想在新的挑战到来时更新视图。 (每天) –

1

有这个目标C的方法,你可以搜索类似的迅速..在你的appdelegate添加这种方法

- (void)userNotificationCenter:(UNUserNotificationCenter *)center 
     willPresentNotification:(UNNotification *)notification 
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions UNNotificationPresentationOptionBadge))completionHandler{ 
     [self.tableview reloadData]; 
} 

我认为这将有助于..

相关问题