1
在我的應用程序中,我使用Facebook圖形API下載數據,並希望在有新數據時提供通知。我想下載數據,將其與已經存儲在NSUserDefaults中的數據進行比較,如果不同,則顯示通知。如何在應用程序關閉時顯示通知(iOS)?
我讓背景中的info.plist文件,並在我的appDelegate取,我已經添加以下代碼:
在didFinishLaunchingWithOptions():
UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(30)
在performFetchWithCompletionHandler()
(這不包括檢查提取的數據是否是新的代碼,這只是一個測試)
let url = "https://graph.facebook.com/109315262061/posts?limit=20&fields=id,full_picture,picture,from,shares,attachments,message,object_id,link,created_time,comments.limit(0).summary(true),likes.limit(0).summary(true)&access_token=\(API_KEY)"
let task = NSURLSession.sharedSession().dataTaskWithURL(NSURL(string: url)!) { (data, response, error) in
if error == nil
{
dispatch_async(dispatch_get_main_queue(), {
do
{
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers)
ids.removeAll()
if let items = jsonData["data"] as? [[String:AnyObject]]
{
for item in items
{
if let id = item["id"] as? String
{
ids.append(id)
}
}
if ids.count == 20
{
print(jsonData)
let notification = UILocalNotification()
notification.alertBody = "You have new notifications!"
notification.alertTitle = "NSITConnect"
notification.fireDate = NSDate(timeIntervalSinceNow: 1)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
completionHandler(UIBackgroundFetchResult.NewData)
}
}
}
catch
{
}
})
}
}
task.resume()
下載操作成功發生,但沒有看到通知。我怎樣才能解決這個問題?當應用程序強制關閉並且顯示通知時,是否有下載數據的方式?如果這是一個愚蠢的問題,我很抱歉,我對這個概念相當陌生!
如果應用程序已經打開,嘗試創建警報以顯示消息或創建自定義控制器/視圖,則不會顯示通知。 – ohr