There are a lot of stackoverflow threads regarding this topic, but I still didn't find a good solution.
If the app is not in the background, I can check launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]
in application:didFinishLaunchingWithOptions:
call to see if it's opened from a notification.
If the app is in the background, all the posts suggest to use application:didReceiveRemoteNotification:
and check the application state. But as I experimented (and also as the name of this API suggests), this method gets called when the notification is received, instead of tapped.
問題は、アプリが起動されてからバックグラウンドになり、通知が受信されたことがわかっている場合application:didReceiveNotification
(application:didFinishLaunchWithOptions:
この時点ではトリガーされません)、ユーザーが通知をタップしてアプリを再開したのか、それともアプリ アイコンをタップしただけなのかをどうやって知るかということです。ユーザーが通知をタップした場合、その通知に記載されているページが開かれることが期待されるためです。それ以外の場合は、開かれるべきではありません。
カスタムアクション通知に使用することもできますhandleActionWithIdentifier
が、これは、ユーザーが通知の本文をタップしたときではなく、カスタムアクションボタンがタップされたときにのみトリガーされます。
ありがとう。
編集:
以下の回答を読んだ後、私は次のようにすれば自分の質問を明確にできると思いました。
これら 2 つのシナリオをどのように区別できるでしょうか。
(A) 1. アプリがバックグラウンドになる。2. 通知を受信する。3. ユーザーが通知をタップする。4. アプリがフォアグラウンドになる
(B) 1. アプリがバックグラウンドになる。2. 通知を受信する。3. ユーザーが通知を無視し、後でアプリアイコンをタップする。4. アプリがフォアグラウンドに入る
application:didReceiveRemoteNotification:
どちらの場合もステップ 2 で がトリガーされます。
または、application:didReceiveRemoteNotification:
(A) のみのステップ 3 でトリガーされるはずですが、何らかの理由でアプリを間違って構成したため、ステップ 2 で表示されていますか?
ベストアンサー1
わかりました。やっと分かりました。
ターゲット設定 ➝ 機能タブ ➝ バックグラウンドモードで、「リモート通知」にチェックを入れると、application:didReceiveRemoteNotification:
通知が届くとすぐにトリガーされます (アプリがバックグラウンドにある限り)。その場合、ユーザーが通知をタップするかどうかはわかりません。
そのボックスのチェックを外すと、application:didReceiveRemoteNotification:
通知をタップしたときにのみトリガーされます。
このボックスをチェックすると、アプリのデリゲート メソッドの動作が変わるというのは少し奇妙です。このボックスをチェックすると、Apple は通知の受信と通知のタップに 2 つの異なるデリゲート メソッドを使用するので、より便利になります。開発者のほとんどは、通知がタップされたかどうかを常に知りたいと思います。
この問題に遭遇した他の人にとってこれが役に立つことを願っています。Appleもこれを明確に文書化していませんでしたここそれで、理解するのにしばらく時間がかかりました。