2017-01-27 17 views
1

アプリが終了している間に地域が入力されたときに(即時に)ローカル通知を送信しようとしています。私は現在、アプリがバックグラウンドにある場合には作業していますが、アプリが終了していれば動作させることはできません。私はこれが可能だと言う他の投稿を読んだが、解決策のどれもうまく動作せず、古くなっている。iBeacon通知がアプリケーション終了時に表示される

didFinishLaunchingWithOptionsで:私も実装didRangeBeaconsを持って

locationManager.delegate = self 
locationManager.pausesLocationUpdatesAutomatically = false 
locationManager.allowsBackgroundLocationUpdates = true    
locationManager.requestAlwaysAuthorization() 

let uuid = UUID(uuidString: "someuuid")! 
let beaconRegion = CLBeaconRegion(proximityUUID: uuid, identifier: "SomeBeacon") 
beaconRegion.notifyEntryStateOnDisplay = true 
beaconRegion.notifyOnEntry = true 
beaconRegion.notifyOnExit = true 
locationManager.startMonitoring(for: beaconRegion) 
locationManager.startRangingBeacons(in: beaconRegion) 

私はここに私のコード(AppDelegate内のすべてのは)だ

スウィフト3でいくつかの助けをいただければと思います。

+0

コードを表示できますか?それがバックグラウンドで動作する場合、なぜアプリケーションが閉じてもうまくいかないことは明らかではありません。 – davidgyoung

+0

ちょうど更新された、あなたの助けに感謝します。 – abcd123

答えて

2

コードが正しく表示され、アプリが終了したときに検出できるようになりました。

locationManager.pausesLocationUpdatesAutomatically = false 
locationManager.allowsBackgroundLocationUpdates = true 

しかし、彼らは何を傷つけるべきではありません:あなたは必要ありません。

あなたはアプリが閉じているという問題だけを述べているので、フォアグラウンド検出がうまく動作すると仮定します。それは?そうでない場合は、まずこれをトラブルシューティングしてください。

テストセットアップの問題のために失敗したアプリケーションのクローズドユースケースを適切にテストすることは、しばしば困難です。いくつかのヒントを参考にしてください:

  1. iOSは領域外であると判断した場合にのみ地域入力イベントを送信します。テストでは、それが地域にあると思うので、あなたはイベントを取得しません。あなたが地域外にいることを確認するには、前もってのアプリケーションでビーコンをオフにするか、の範囲外に出て、終了コールバックができるまで待ちます。それだけで、クローズド検出をテストするためにアプリを終了する必要があります。

  2. 電話機を再起動する場合は、起動後5分待ってからCoreLocationが完全に初期化されていることを確認してください。

  3. 他のビーコンアプリが電話機にインストールされていないことを確認し、Bluetooth検出ハードウェアアクセラレーションスロットをすべて使用してください。そうした場合、バックグラウンドでの検出は最大15分遅れる可能性があります。すべてのビーコンアプリをアンインストールし、アンインストールして再インストールします。

上記のテストのヒントに従うと、ビーコンが近くにある数秒以内に検出コールバックが表示されます。

0

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のようにスズを通過する可能性があります。

関連する問題