2017-07-26 18 views
3

を扱う画面の遅れにつながる、私はいくつかの地元のプッシュ通知を登録したとき、私は奇妙な問題に気づいた:LGSideMenuController:複数のローカルプッシュ通知の登録は

for model in myModelArray 
{ 
    let calendar = Calendar.current 
    let scheduleDate = calendar.date(byAdding: .second, value: i * 5, to: Date())! 

    let notification = UILocalNotification() 
    notification.fireDate = scheduleDate 
    notification.alertBody = "My push notification title" 

    UIApplication.shared.scheduleLocalNotification(notification) 
    i += 1 
} 

私の地元のプッシュ通知は、バックグラウンドモードで表示されたら、私は上のタップ

func application(_ application: UIApplication, didReceive notification: UILocalNotification) 
{ 
    let mainMenuVC = self.window?.rootViewController as! MenuViewController 
    let navController = mainMenuVC.sideMenuController?.rootViewController as! UINavigationController 

    let myDestinationVC = navController.storyboard?.instantiateViewController(withIdentifier: "MyIdentifier") as! MyDestinationViewController 
    navController.pushViewController(mydestinationVC, animated: false) 
} 

しかし、私のmenuVCと私のdestinationVCとの間の移行が起こるまでには最大1分かかります。私のアプリが開かれたら、View Controllerを押すことは完全に機能します。私は何かを忘れましたか?私のコードで何が間違っていますか?この遅延は、アプリが終了してからプッシュ通知をタップした後に再び開いたときにのみ発生します。

答えて

4

iOS10以降、UILocalNotificationは廃止されました。プッシュ通知を受け取るかもしれませんが、funcアプリケーション(_アプリケーション:UIApplication、didReceive通知:UILocalNotification)は決して呼び出されません。あなたが経験している "遅延"は、アプリケーションがアクティブなときに処理される2番目のローカルプッシュ通知です。

代わりにUNUserNotificationCenterを使用しますが、iOS9デバイスの「古い処理」を維持してください。このような

セットアップ

あなたの通知:

 let center = UNUserNotificationCenter.current() 
     let content = UNMutableNotificationContent() 
     content.title = „MyTitle“ 
     content.body = „MyBody“ 
     content.categoryIdentifier = UNNotificationDefaultActionIdentifier 

     let units: Set<Calendar.Component> = [.second, .hour, .minute, .day, .month, .year] 
     let dateComponents = Calendar.current.dateComponents(units, from: date) 

     let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) 

     let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) 
     center.add(request) 

と扱うようなあなたの受信通知:

import UserNotifications 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate 
{ 
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
{ 
    // Local Push notifications 
    if #available(iOS 10.0, *) 
    { 
     let center = UNUserNotificationCenter.current() 
     center.delegate = self 
     center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in 

     } 
    } 
    else 
    { 
     application.registerUserNotificationSettings(UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)) 
    } 

    return true 
} 

@available(iOS 10.0, *) 
func userNotificationCenter(_ center: UNUserNotificationCenter, 
          didReceive response: UNNotificationResponse, 
          withCompletionHandler completionHandler: @escaping() -> Void) 
{ 
    // handle your response here 
    completionHandler() 
} 
} 
+0

ありがとうございます!なぜこのコンパイラ警告はこの非推奨を示していないのですか?どのような厄介なバグ。デリゲートメソッドが起動しない唯一のケースは、アプリケーションが終了したときです。 iOS10の通知処理を調整します。 –

関連する問題