我正在尝试在打开手机并且我位于特定区域内时触发本地通知。这按预期工作,但每次我打开设备时,我都会收到新的通知。如果我只是保留现有通知,它可以被推到通知列表的底部。如果我取消现有通知并创建一个新通知,我会得到一个奇怪的动画。有没有办法:
- 更新已传递的现有 UILocalNotification 以将其推送到顶部。
- 当锁定屏幕消失时以某种方式收到通知并取消通知?
这是我现有的代码:
- (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设备是否已锁定/解锁?
有关进一步的通知,我建议您查看此列表: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");
}
考虑到奇怪的动画,如果你不喜欢它,那就向苹果报告。只有他们才能修复它。我认为您不应该更新通知(如果可能的话)。只需推一个新的,要么删除旧的,要么不打扰它。这是大多数应用程序所做的,用户通常对此没有问题。它也是一种历史。