【问题标题】:Geofence alerts/local notifications not waking up locked phone when triggered地理围栏警报/本地通知在触发时不会唤醒锁定的手机
【发布时间】:2017-04-22 00:38:45
【问题描述】:

我正在制作一个使用圆形区域作为地理围栏的应用程序。当手机处于活动状态或应用程序打开时,地理围栏通知在模拟器和设备(运行 10.3.1 的 iPhone 6)中都可以正常工作。

在模拟器中可以正常工作;当用户进入一个区域时,它会醒来,发出声音并在锁定屏幕上显示警报。

在手机上,进入区域时会发出“didEnterRegion”委托调用(我记录了一些消息),但手机没有发出警报并唤醒。当我按下主页按钮一次时,我可以在锁定屏幕上看到警报,但我希望它立即唤醒并显示警报 - 就像我收到消息时一样。它在模拟器中工作,所以我想知道可能出了什么问题?它已经为我工作了几次,在手机和手表上都显示了警报,但 95% 的时间它不起作用 - 通知会生成,但只有在我手动唤醒手机时才能看到。

如何解决这个问题?

这是我用于创建本地通知的代码:

        // https://blog.codecentric.de/en/2016/11/setup-ios-10-local-notification/


        let location = CLLocation(latitude: item.coordinate.latitude, longitude: item.coordinate.longitude)
        GeoTools.decodePosition(location: location) {
            (address, city) in
            let content = UNMutableNotificationContent()
            content.title = "Camera nearby!"
            content.subtitle = item.id
            content.body = "\(address), \(city)"
            content.categoryIdentifier = Constants.notificationCategoryId
            content.sound = UNNotificationSound.default()
            content.threadIdentifier = item.id

            // FIXME make action for clicking notification

            let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.001, repeats: false) // FIXME HACK

            let request = UNNotificationRequest(identifier: "camNotification", content: content, trigger: trigger)

            let unc = UNUserNotificationCenter.current()
            unc.removeAllPendingNotificationRequests()
            unc.add(request, withCompletionHandler: { (error) in
                if let error = error {
                    print(error)
                }
                else {
                    print("completed")
                }

            })

        }

【问题讨论】:

    标签: ios swift core-location uilocalnotification geofencing


    【解决方案1】:

    这是我刚刚验证的一些代码,当通知出现时唤醒设备:

    let message = "CLRegion event"
    // Show an alert if application is active:
    if UIApplication.shared.applicationState == .active {
        if let viewController = UIApplication.shared.keyWindow?.rootViewController {
            showSimpleAlertWithTitle(nil, message: message, viewController: viewController)
        }
    }
    else {
        // Otherwise app is in background, present a local notification:
        let content = UNMutableNotificationContent()
        content.body = message
        content.sound = UNNotificationSound.default()
        content.categoryIdentifier = "message"
    
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1.0, repeats: false)
        let request = UNNotificationRequest(identifier: "com.foobar", content: content, trigger: trigger)
        UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
    }
    

    真的唯一的区别是我不打电话给removeAllPendingNotifications(),所以如果你必须删除通知,我想知道removePendingNotificationRequests(withIdentifiers identifiers: [String])是否更精确?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2018-08-04
      相关资源
      最近更新 更多