1

I am trying to get a local notification to fire any time I turn on my phone and I am inside a specific region. This works as expected but each time I turn my device on I get a new notification. If I just leave the existing notification it can get pushed down to the bottom of the notification list. If I cancel the existing notification and create a new one I get a weird animation. Is there a way to either:

  1. Update an existing UILocalNotification that has already been delivered to push it to the top.
  2. Somehow get notified when the lock screen goes away and cancel the notification then?

Here is my existing code:

- (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;
        }
    }
}
Patrick Tescher
  • 3,387
  • 1
  • 18
  • 31

1 Answers1

0

There are definitely ways to detect if the phone is locked/unlocked Is there a way to check if the iOS device is locked/unlocked?

For further notifications, I suggest you look at this list:http://iphonedevwiki.net/index.php/SpringBoard.app/Notifications It contains the com.apple.springboard.deviceWillShutDown notification that is called when the phone is shutting down. I just tested it with this code and seems to work, however, the app gets killed immediately and the XCode session terminates so I'm not sure how useful this is for a real application.

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");
}

Considering the weird animation, if you don't like it, then report to Apple. Only they can fix it. I don't think you should update the notification (if this is possible at all). Simply push a new one, and either remove the old or don't bother with it. This is what most applications do and users generally have no issues with it. It serves as a kind of history, too.

Community
  • 1
  • 1
allprog
  • 16,540
  • 9
  • 56
  • 97
  • Yeah I can detect if the phone is locked or unlocked but I get notified when they turn their phone on and I don't get notified again when they turn their phone off. – Patrick Tescher Jan 10 '14 at 23:12
  • I'm not sure I fully understand your notification problems but I added some more detail to the answer. Let me know if it helps. – allprog Jan 11 '14 at 22:32