【发布时间】:2017-06-28 07:48:47
【问题描述】:
I am currently learning local notification, but i have a few problems in my test project.
import UIKit
import UserNotifications
import Alamofire
import SwiftyJSON
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let playersStore = PlayersStore()
var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let navController = window!.rootViewController as! UINavigationController
let itemsController = navController.topViewController as! ListPlayersVC
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
if !accepted {
}
}
let category = UNNotificationCategory(identifier: "myCategory", actions: [], intentIdentifiers: [], options: [])
center.setNotificationCategories([category])
center.delegate = scheduleNotification() as? UNUserNotificationCenterDelegate
return true
}
func scheduleNotification() {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)
let content = UNMutableNotificationContent()
for i in playersStore.allItems {
let todoEndpoint: String = "url1"
let allowedCharacterSet = (CharacterSet(charactersIn: " ").inverted)
let escapedString = todoEndpoint.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)
Alamofire.request(escapedString!)
.responseJSON {response in
guard response.result.error == nil else {
// print(response.result.error!)
print("Error")
return
}
let todoEndpoint2: String = "url"
let allowedCharacterSet = (CharacterSet(charactersIn: " ").inverted)
Alamofire.request(escapedString2!)
.responseJSON {response2 in
guard response.result.error == nil else {
//print(response.result.error!)
print("Error2")
return
}
// guard let json = response.result.value as? [String: Any] else {
// print(response.result.error!)
// return
// }
let json2 = JSON(response.result.value!)
let json3 = JSON(response2.result.value!)
let test1 = json3["test"]
if test1 != i.test {
i.test = test1
self.savechanges()
content.title = "test"
content.subtitle = "testtest"
content.body = "testtest"
content.badge = 1
content.categoryIdentifier = "myCategory"
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("\(error)")
}
}
}
return
}
}
}
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
let savePlayersStore = playersStore.saveChanges()
if (savePlayersStore) {
print("saves all items")
} else {
print("error, could not save any of the item")
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func savechanges() {
let savePlayersStore = playersStore.saveChanges()
if (savePlayersStore) {
print("saves all items")
} else {
print("error, could not save any of the item")
}
}
}
抱歉编辑/解释不当..
希望你现在能理解我
这是我的源代码
我想用本地通知做什么:
联系服务器(testwise 设置为 60 秒)(工作)
如果有更新,显示通知(工作中)
在后台保存新值(self.savechanges())(不起作用) 因此,如果重复为真(不起作用),则不会生成新通知
我用 trigger repeat false 测试了我的应用程序,但我的应用程序只会显示一个通知并忽略任何未来的更改,此外它还会忽略我的播放器商店中的所有项目,因此只会发布一个通知
我希望你明白我想要完成什么
非常感谢!
【问题讨论】:
-
你的问题很不清楚。
-
提出好问题的一般提示:将所有代码粘贴到 Xcode 中,然后单击
ctrl + I修复缩进。然后通过更好地缩进代码来编辑您的问题。同时删除空白行......仍然不清楚你在问什么 -
非常感谢,我编辑了我的帖子!
标签: ios swift swift3 xcode8 ios10.3