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")
}
}
}
抱歉编辑/解释不好..
我希望你现在能理解我
所以这是我的源代码
我想用本地通知做什么:
联系服务器(测试设置为 60 秒)(工作)
如果有更新,显示通知(工作)
将新值保存在后台 (self.savechanges())(不起作用),因此如果 repeat 为 true(不起作用),则不会生成新通知
我使用触发器重复错误测试了我的应用程序,但我的应用程序只会显示一个通知并忽略任何未来的更改,此外它还会忽略我的播放器商店中的所有项目,因此只会发布一个通知
我希望你明白我想要完成的事情
非常感谢!