Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Region based local notification

Im currently working with the "Regions" Sample code: https://developer.apple.com/library/ios/#samplecode/Regions/Introduction/Intro.h tml#//apple_ref/doc/uid/DTS40010726-Intro-DontLinkElementID_2

I'd like to take it a step further and generate, or fire, a notifcation when the user exits the region (can be for both enter & exit, i don't mind, whatever is easiest for an initial implementation).

I have been looking at the CLLocation Class reference, Location Awareness Programming Guide and Local and Push Notification Programming guide. And im suffering from information overload.

Many thanks :)

EDIT: I think i may have an idea which solves the problem: in the RegionsViewController implementation file there is this:

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region        {
    NSString *event = [NSString stringWithFormat:@"didExitRegion %@ at %@", region.identifier, [NSDate date]];

    [self updateWithEvent:event];
}

Since i want to implement a local notification when the user exits the designated region boundary i've input this:

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
    NSString *event = [NSString stringWithFormat:@"didExitRegion %@ at %@", region.identifier, [NSDate date]];
    [self updateWithEvent:event];
    //implement local notification: 
    UIApplication *app                = [UIApplication sharedApplication];
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    if (notification == nil)
        return;

    notification.alertBody = [NSString stringWithFormat:@"Did You Lock Your House?"];
    notification.alertAction = @"Lock House";
    notification.soundName = UILocalNotificationDefaultSoundName; 
    notification.applicationIconBadgeNumber = 1;
    [app presentLocalNotificationNow:notification];

    [notification release];
}

Could somebody advice me as to whether this is correct, or if there are any recommendations? (apologies for the poor formatting)

like image 314
Dan_R Avatar asked Apr 05 '12 12:04

Dan_R


People also ask

What is local notification?

Local notifications are scheduled by an app and delivered on the same device. They are suited for apps with time-based behaviors, such as calendar events. When you run your app on a device with Android OS 8.0 or above, Kony uses default channels that are mentioned in the localnotificationconfig.

What is local notification in iOS?

Local notifications reach users whether your app is running in the foreground or the background and require no external infrastructure to send, which is why they are aptly termed “local.” These notifications simply require that a user's device is on in order to be received.

How many types of notification are there in Swift?

Apple provides three different types of notifications in iOS: NSNotificationCenter, UILocalNotification, and Remote Notifications. In addition, there has been a Notification Center to manage notifications since iOS 5. The three notifications have three very different uses.


1 Answers

you are correct, there is no better way than firing a local notification from locationManager:didExitRegion: Moreover, this will work even if your app is running in background or closed.

like image 177
Laurent Avatar answered Nov 06 '22 22:11

Laurent