我創建了推送通知測試。我創建了一個代碼,當應用程序轉到後臺時,每10秒該應用程序顯示一個本地推送通知。該代碼在幾分鐘內運行良好。幾分鐘後,後臺停止工作。我認爲iOS會在一段時間後殺死在後臺運行的作品。是嗎? 有一種方式,iOS不會阻止我在後臺工作?iOS9上的後臺工作
這是我的代碼: 的ViewController:
import UIKit
class ViewController: UIViewController {
var timer = NSTimer()
var backgroundTaskIdentifier: UIBackgroundTaskIdentifier?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
notificatioAppGoToBackground()
setupNotificationSettings()
//call handle notifications
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleModifyListNotification), name: "modifyListNotification", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleDeleteListNotification), name: "deleteListNotification", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.handleStopTimerNotification), name: "stopTimer", object: nil)
backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({
UIApplication.sharedApplication().endBackgroundTask(self.backgroundTaskIdentifier!)
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func alert(title:String, message:String){
let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
self.presentViewController(alert, animated: true){}
}
func handleStopTimerNotification(){
timer.invalidate()
print("acao foreground")
UIApplication.sharedApplication().cancelAllLocalNotifications() //cancela a notificacao
}
func handleModifyListNotification() {
timer.invalidate()
print("acao editar")
//alert("Atenção", message: "Modificação ativada, notificação cancelada")
UIApplication.sharedApplication().cancelAllLocalNotifications() //cancela a notificacao
}
func handleDeleteListNotification() {
print("executou a exclusao")
}
func setupNotificationSettings() {
let notificationSettings: UIUserNotificationSettings! = UIApplication.sharedApplication().currentUserNotificationSettings()
if (notificationSettings.types == UIUserNotificationType.None){
let notificationTypes: UIUserNotificationType = [.Alert, .Sound, .Badge]
let modifyListAction = UIMutableUserNotificationAction()
modifyListAction.identifier = "editList"
modifyListAction.title = "Edit list"
modifyListAction.activationMode = UIUserNotificationActivationMode.Foreground
modifyListAction.destructive = false
modifyListAction.authenticationRequired = true
let trashAction = UIMutableUserNotificationAction()
trashAction.identifier = "trashAction"
trashAction.title = "Delete list"
trashAction.activationMode = UIUserNotificationActivationMode.Background
trashAction.destructive = true
trashAction.authenticationRequired = true
let actionsArray = NSArray(objects: modifyListAction, trashAction)
let actionsArrayMinimal = NSArray(objects: trashAction, modifyListAction)
// Specify the category related to the above actions.
let shoppingListReminderCategory = UIMutableUserNotificationCategory()
shoppingListReminderCategory.identifier = "shoppingListReminderCategory"
shoppingListReminderCategory.setActions(actionsArray as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Default)
shoppingListReminderCategory.setActions(actionsArrayMinimal as? [UIUserNotificationAction], forContext: UIUserNotificationActionContext.Minimal)
let categoriesForSettings = NSSet(objects: shoppingListReminderCategory)
// Register the notification settings.
let newNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: categoriesForSettings as? Set<UIUserNotificationCategory>)
UIApplication.sharedApplication().registerUserNotificationSettings(newNotificationSettings)
}
}
func setActions(actions: [AnyObject]!, forContext context: UIUserNotificationActionContext){}
func notificatioAppGoToBackground(){
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplicationWillResignActiveNotification, object: nil)
}
func appMovedToBackground() {
print("App moved to background!!!")
listenerSchedule()
}
func listenerSchedule() {
timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: #selector(ViewController.startNotication), userInfo: nil, repeats: true)
}
func startNotication(){
let date = NSDate()
let dateComponets: NSDateComponents = NSCalendar.currentCalendar().components([.Day, .Month, .Year, .Hour, .Minute, .Second], fromDate: date)
if(dateComponets.second < 20){
print("nao notifica \(dateComponets.second)")
}else{
print("aguardando notificação \(dateComponets.second)")
let localNotification = UILocalNotification()
localNotification.alertBody = "Teste de notificação"
localNotification.alertAction = "View List"
localNotification.category = "shoppingListReminderCategory"
localNotification.applicationIconBadgeNumber = dateComponets.second
localNotification.soundName = UILocalNotificationDefaultSoundName
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
}
}
}
的AppDelegate:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
print(notificationSettings.types.rawValue)
}
//executa a noficacao agendada com a app aberta
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
// Do something serious in a real app.
print("Received Local Notification:")
print(notification.alertBody!)
application.applicationIconBadgeNumber = 0 //limpa o badge ao carregar a app
application.cancelAllLocalNotifications() //limpa o badge ao carregar a app
NSNotificationCenter.defaultCenter().postNotificationName("stopTimer", object: nil)
}
//Essa funcao exibe a notificacao quando a app esta parada ou em segundo plano
func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler:() -> Void) {
if identifier == "editList" {
NSNotificationCenter.defaultCenter().postNotificationName("modifyListNotification", object: nil)
}
else if identifier == "trashAction" {
NSNotificationCenter.defaultCenter().postNotificationName("deleteListNotification", object: nil)
}
completionHandler()
}
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 throttle down OpenGL ES frame rates. 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.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("stopTimer", object: nil)
application.applicationIconBadgeNumber = 0 //limpa o badge ao carregar a app
application.cancelAllLocalNotifications() //limpa o badge ao carregar a app
// 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:.
}
}
是否啓用了背景模式 –
Humm,我只創建了一個代碼。我在哪裏做? –
單擊項目導航器中的項目 - >項目目標 - >功能 - >向下滾動到背景模式並打開開關。您必須啓用最適合您應用的任何服務才能在後臺運行。如需更多幫助,請查看此鏈接http://stackoverflow.com/questions/8943214/iphone-nstimers-in-background?rq=1#comment11195182_8943911 –