【问题标题】:CLBeaconRegion notifyEntryStateOnDisplay and UILocalNotificationsCLBeaconRegion notifyEntryStateOnDisplay 和 UILocalNotifications
【发布时间】:2014-01-10 00:40:14
【问题描述】:

我正在尝试在我打开手机并且我在特定区域内时触发本地通知。这可以按预期工作,但是每次打开设备时,我都会收到一条新通知。如果我只是离开现有的通知,它可以被推到通知列表的底部。如果我取消现有通知并创建一个新通知,我会得到一个奇怪的动画。有没有办法:

  1. 更新一个已经交付的现有 UILocalNotification 以将其推送到顶部。
  2. 以某种方式在锁定屏幕消失时收到通知然后取消通知?

这是我现有的代码:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
    if ([region isKindOfClass:[CLBeaconRegion class]]) {
        CLBeaconRegion *beaconRegion = (CLBeaconRegion*)region;
        UILocalNotification *existingNotification = self.beaconNotification;
        switch (state) {
            case CLRegionStateInside:
                self.beaconNotification = [[UILocalNotification alloc] init];
                self.beaconNotification.alertBody = @"You're inside the region";
                [[UIApplication sharedApplication] presentLocalNotificationNow:self.beaconNotification];
                if (existingNotification) {
                    [[UIApplication sharedApplication] cancelLocalNotification:existingNotification];
                }
                break;
            case CLRegionStateOutside:
                self.beaconNotification = [[UILocalNotification alloc] init];
                self.beaconNotification.alertBody = @"You're outside the region";
                [[UIApplication sharedApplication] cancelAllLocalNotifications];
                break;
            default:
                break;
        }
    }
}

【问题讨论】:

    标签: ios core-location core-bluetooth


    【解决方案1】:

    肯定有办法检测手机是否被锁定/解锁Is there a way to check if the iOS device is locked/unlocked?

    如需进一步通知,我建议您查看此列表:http://iphonedevwiki.net/index.php/SpringBoard.app/Notifications 它包含手机关机时调用的com.apple.springboard.deviceWillShutDown 通知。我刚刚用这段代码对其进行了测试,似乎可以工作,但是,应用程序立即被终止,XCode 会话终止,所以我不确定这对真正的应用程序有多大用处。

    CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(),
                                        NULL,
                                        shutdownCallback,
                                        CFSTR("com.apple.springboard.deviceWillShutDown"),
                                        NULL,
                                        CFNotificationSuspensionBehaviorDeliverImmediately);
    void shutdownCallback (CFNotificationCenterRef center, void *observer,
                     CFStringRef name, const void *object, CFDictionaryRef userInfo)
    {
        NSLog(@"Shutting down");
    }
    

    考虑到奇怪的动画,如果您不喜欢它,请向 Apple 报告。只有他们可以修复它。我认为您不应该更新通知(如果可能的话)。只需推一个新的,然后删除旧的或不理会它。这是大多数应用程序所做的,用户通常对此没有任何问题。它也是一种历史。

    【讨论】:

    • 是的,我可以检测到手机是锁定还是解锁,但是当他们打开手机时我会收到通知,而当他们关闭手机时我不会再收到通知。
    • 我不确定我是否完全理解您的通知问题,但我在答案中添加了更多细节。如果有帮助,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多