UPDATE @davidgyoungは、別の質問に私に指摘したように、それは監視APIを使用して何をしている範囲にされていないため、UNLocationNotificationTrigger
を使用して、この方法は、あなたにメジャーとマイナーの識別情報を報告しません。メジャー番号とマイナー番号を取得する必要があります。これは、このラッパーAPIで何をするかを制限します。それは確かに便利ですが、希望通りに役立つものではありません。
私が言うことができるものから新しい(iOSの10、スウィフト3)の方法は、UserNotifications
ライブラリを使用してUNNotificationRequest
でそれを実装することです。ビーコン領域にはUNLocationNotificationTrigger
で渡すことができます。今はあなたが望むもの(アプリが閉鎖されているときの通知)に直観的に反しているようですが、NSLocationAlwaysUsageDescription
の代わりにキーをInfo.plist
に追加したいと考えています。なぜそれがそのようなのか分かりませんが、それは必要なステップです。
// Ensure you use a class variable in order to ensure the location
// manager is retained, otherwise the alert message asking you to
// authorize it will disappear before you can tap "allow" which
// will keep it from working
let locationManager = CLLocationManager()
// ...
// Somewhere in your class (e.g. AppDelegate or a ViewController)
self.locationManager.requestWhenInUseAuthorization()
let region = CLBeaconRegion(proximityUUID: UUID(uuidString: "YOUR-UNIQUE-UUID-STRING")!, identifier: "com.yourcompany.youridentifier")
region.notifyOnEntry = true
region.notifyOnExit = false
let content = UNMutableNotificationContent()
content.title = "Notification Title"
content.body = "Body text goes here"
// Not sure if repeats needs to be set to true here, but that's
// how I've implemented it
let trigger = UNLocationNotificationTrigger(region: region, repeats: true)
// Use the same identifier each time to ensure it gets overwritten
// in the notification system
let identifier = "BeaconLocationIdentifier"
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)
// This is probably unnecessary since we're using the same identifier
// each time this code is called
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
// Make sure you set the notification center's delegate. My
// containing class is the AppDelegate. I implement the delegate
// methods there.
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request, withCompletionHandler: { (error) in
})
// Not sure if this is required either, but after I added this, everything
// started working
self.locationManager.startRangingBeacons(in: region)
あなたは通知センターのデリゲートメソッドを実装していることを確認してください:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
// Called when the notification has been swiped or tapped by the user
// Do something with the response here
// Make sure you call the completion handler
completionHandler()
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Called when the app is in the foreground
// Do something with the notification here
// Make sure you call the completion handler
completionHandler([.alert, .sound])
}
最後に、あなたがする必要があるとしているここで私は(iBeacon)ロケーションベースの通知を作成するために使用しているコードですまずすべての機能を有効にするために通知を承認します。あなたはそれを行うには、このコードを使用することができます:
let options: UNAuthorizationOptions = [.alert,.sound]
UNUserNotificationCenter.current().requestAuthorization(options: options) {
(granted, error) in
if !granted {
debugPrint("Something went wrong")
} else {
debugPrint("Notifications granted")
}
}
最後の一つのヒント:私は(一種の)私が使用しているビーコンのためのファラデーケージとして働き、古い金属錫を持っています。私はちょうど1分ほどそこに私のビーコンを閉じて、アプリは私が範囲外になったことを検出します。これは、ビーコンから遠く離れて歩いたり、バッテリーを取り外そうとするよりも便利です(これはまったく動作しません)。いくつかのビーコンは他のビーコンよりも強いシグナルを持ち、YMMVのようにスズを通過する可能性があります。
コードを表示できますか?それがバックグラウンドで動作する場合、なぜアプリケーションが閉じてもうまくいかないことは明らかではありません。 – davidgyoung
ちょうど更新された、あなたの助けに感謝します。 – abcd123