Skip to content

Geofence delay notification solution (iOS)

Demo App can be downloaded here.

1. Add StreetHawk component to your project and configure.

You can simply pod whole StreetHawk component:

pod "streethawk"

Or minimum requirement is to pod Geofence, Locations, Push and Feed components:

pod "streethawk/Geofence"
pod "streethawk/Locations"
pod "streethawk/Push"
pod "streethawk/Feed"

The detail guide for configuring Geofence, Locations, Push, Feed.

2. Create geofence in web console.

Each geofence has the element: title, latitude, longitude.

3. Create campaign with geofence, and action choose InApp Feed.

4. Client side register delay fire local notification when enter geofence

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [StreetHawk registerInstallForApp:@"SHGeofence" withDebugMode:YES];

    //Get notified when enter/exit a server defined geofence.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(geofenceEnterExitHandler:) name:SHLMEnterExitGeofenceNotification object:nil];

    return YES;
}

- (void)geofenceEnterExitHandler:(NSNotification *)notification
{
    NSDictionary *geofence = notification.userInfo;
    //It contains many property for compare: latitude, longitude, radius, title, suid etc. I use "title" in this demo as it's easy to read.
    NSString *title = geofence[@"title"];
    BOOL isInside = [geofence[@"isInside"] boolValue];
    if (isInside) //means enter a geofence, register delay local notification with matching information.
    {
        [StreetHawk feed:0 withHandler:^(NSArray *arrayFeeds, NSError *error) //fetch feed json
        {
            for (SHFeedObject *feedObj in arrayFeeds)
            {
                NSDictionary *json = feedObj.content;
                if ([title compare:json[@"title"]] == NSOrderedSame)  //find the match feed json, you can define your own compare condition, using latitude, longitude, radius, title or suid etc.
                {
                    NSString *title = feedObj.title;
                    NSString *message = feedObj.message;
                    double delayMins = [json[@"delay"] doubleValue]; //feed can add customized value such as delay time
                    //create delay fire local notification
                    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
                    localNotification.alertTitle = title;
                    localNotification.alertBody = message;
                    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:delayMins * 60]; //delay fire
                    localNotification.soundName = UILocalNotificationDefaultSoundName;
                    localNotification.applicationIconBadgeNumber = 1;
                    localNotification.userInfo = @{@"title": title};
                    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
                    break;
                }
            }
        }];
    }
    else //means exit a geofence, cancel the not fired local notification.
    {
        for (UILocalNotification *localNotification in [UIApplication sharedApplication].scheduledLocalNotifications)
        {
            if ([title compare:localNotification.userInfo[@"title"]] == NSOrderedSame)
            {
                [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
            }
        }
    }
}