2017-01-27 5 views
2

私はアプリはプッシュ通知を経由して開いているかどうかを検出するために、別のスタックオーバーフローのポストからこのソリューションを取った:iOSでプッシュ通知でアプリが開かれていないかどうかを検出しますか?

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) { 
    print("Push notification received: \(data)") 
    if #available(iOS 9, *) { 
    if let type = data["type"] as? String, type == "status" { 
     // IF the wakeTime is less than 1/10 of a second, then we got here by tapping a notification 
     if application.applicationState != UIApplicationState.background && NSDate().timeIntervalSince(wakeTime as Date) < 0.1 { 
      // User Tap on notification Started the App 
      sendPushStatistic() 
     } 
     else { 
      // DO stuff here if you ONLY want it to happen when the push arrives 
     } 
    } 
    else { 
    } 
    } 
} 

今私はアプリはクリックせずに(コールドスタートとバックグラウンドから)開かれたかどうかを確認する方法を疑問に思いますプッシュ通知ではなく、アプリアイコンまたは実行中のアプリビューから

+0

"applicationdidfinishlauching"メソッドを確認できます。ユーザーがアプリアイコンをクリックしたときにアプリが起動されるときに呼び出されます。 – Pawan

+0

プッシュで開くと呼び出されませんか?ありがとうございました! –

+0

アプリケーションがすでにバックグラウンドで実行されている場合はnoです。 – Pawan

答えて

2

これを行う1つの方法は、プッシュ通知で開いたかどうかを示す上記の関数にブール値を追加することです。

次に、あなたのviewdidLoadで、このブール値をチェックすることができます。

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) { 
print("Push notification received: \(data)") 
if #available(iOS 9, *) { 
if let type = data["type"] as? String, type == "status" { 
    // IF the wakeTime is less than 1/10 of a second, then we got here by tapping a notification 
    if application.applicationState != UIApplicationState.background && NSDate().timeIntervalSince(wakeTime as Date) < 0.1 { 
     // User Tap on notification Started the App 
     sendPushStatistic() 
    } 
    else { 
     pushNotificationLaunch = true 

    } 
} 
else { 
} 
} 

}

その後のviewDidLoad機能であなたが作成した変数が真であるかどうかを確認するためにif文を作成することができます。

override func viewDidLoad() { 
    super.viewDidLoad() 

    if pushNotifcationLaunch = false { 
    //Code for did launched via app click. 
} 
関連する問題