【问题标题】:Is it possible to automatically fetch parse objects?是否可以自动获取解析对象?
【发布时间】:2015-10-30 19:40:27
【问题描述】:

一旦另一端的用户发送消息,许多即时消息服务会自动显示消息。

现在,我能想到的唯一方法是使用一个 nstimer,它会运行适当的代码块来获取消息并更新表格视图。这是资源密集型的,并且每秒会浪费一个请求。有什么方法可以自动执行此过程并使其仅在发送/接收新消息时发生?

【问题讨论】:

  • 我该怎么做?
  • 在您的应用委托中使用func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject])。您将能够检查通知负载以确定推送通知是什么,并相应地刷新视图

标签: parse-platform swift2 ios9 pfquery pfobject


【解决方案1】:

这是一个在应用程序委托中使用didReceiveRemoteNotification 来响应推送通知的示例。您尤其关心在应用处于活动状态时收到通知的情况。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {

    if (PFUser.currentUser() == nil) {
        return
    }

    if (application.applicationState == UIApplicationState.Inactive || application.applicationState == UIApplicationState.Background) { 
        // Received the push notification when the app was in the background
        PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)

        // Inspect userInfo for the push notification payload
        if let notificationPayloadTypeKey: String = userInfo["someKey"] as? String {
            // Do something
        }
    } else {
        // Received the push notification while the app is active
        if let notificationPayloadTypeKey: String = userInfo["someKey"] as? String {
            // Use NSNotificationCenter to inform your view to reload
            NSNotificationCenter.defaultCenter().postNotificationName("loadMessages", object: nil)
        }
    }
}

然后你只需要在你的视图控制器中添加一个监听器。在viewDidLoad 内添加以下内容,每当收到通知时,它将调用函数loadMessages

NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadMessages", name: "loadMessages", object: nil)

如果你下载the code for Parse's Anypic example project,你可以看到他们如何处理远程通知。

【讨论】:

    猜你喜欢
    • 2023-02-07
    • 2020-02-16
    • 2011-01-08
    • 2014-04-07
    • 2012-10-09
    • 2012-01-13
    • 2011-02-16
    • 1970-01-01
    相关资源
    最近更新 更多