CLBeaconRegion notifyEntryStateOnDisplay和UILocalNotifications

时间:2014-01-10 00:40:14

标签: ios core-location core-bluetooth

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

  1. 更新已经发送的现有UILocalNotification,将其推送到顶部。
  2. 当锁定屏幕消失时以某种方式获得通知然后取消通知?
  3. 这是我现有的代码:

    - (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;
            }
        }
    }
    

1 个答案:

答案 0 :(得分:0)

有一些方法可以检测手机是否已锁定/解锁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报告。只有他们可以解决它。我认为您不应该更新通知(如果可以的话)。只需推一个新的,要么删除旧的或不打扰它。这是大多数应用程序所做的,用户通常不会遇到任何问题。它也是一种历史。