4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

getDeliveredNotificationsでは位置情報のNotificationを取得できない

Posted at

小さなことですが、軽く躓いたのでメモしておきます。

UNUserNotificationCenterにはgetDeliveredNotificationsというメソッドがあります。これは通知センターにある通知のうち、自分のアプリが発行した通知を取得することができます。

自分は位置情報をトリガにしてLocal通知を発行し、同じ通知を2度発行しないために発行済の通知を取得してアプリ内部でID保持しようと考え、このgetDeliveredNotificationsを実行しました。しかし何度やっても空の配列が返ってくるだけで、発行済通知が取得できませんでした。
調べてみてもドキュメントにはそれらしいことは書いていません。

色々探しているうちに、Jump to Defineでコードを定義されているコードを見てみたらこのようなコメントを見つけました。

Notifiations triggered by location cannot be retrieved, but can be removed.

場所によってトリガされた通知は取得できませんが、削除できます。

回避法

というわけで、位置情報をトリガにしたNotificationは通知センターから削除することはできても、通知されたのか、されていないのかを取得することはできません。
一応 getPendingNotificationRequestsでは位置情報をトリガにしたNotificationも取得できるので、発行時に発行済IDを保存し、getPendingNotificationRequestsからIDが消えたら発行されたとみなしてもよさそうです。

ややこしいですけど・・・。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?