Now a days, marketing for apps / games has more focus on engaging users directly rather than doing experiments under the hood. Push notification has its own significance in achieving this goal. It can be incredibly helpful if used with proper plan in terms of frequency, time and  relevance.

Push notifications with added filtration based on location can be more helpful in making it relevant and decreasing the frequency to avoid annoyance.

On continuous request coming from our users, App42 has come up with location based push notifications that enables you as app admin to send notifications to the app users based on specific geo-location.
Here we will be discussing about how to

  • configure your xcode project to receive and handle the geo-based push notification
  • send geo-based push from our AppHQ Console

Getting Started with App42:

If you have not integrated App42 SDK yet then download our SDK and follow the Getting started guide.

Configure Xcode Project:

To make your project ready for geo-based push, the project must be configured for

  • receiving Push Notifications, and
  • location based manipulations

This blog assumes you have prior knowledge of configuring the project for Push Notification which involves:

  • Certificate & provisional profile creation from iOS Dev Center
  • Registering your device to receive push notification
  • Storing device token to App42 Cloud

If you are new to iOS Push notification then follow our App42 iOS Push Notification Tutorial.

To make the integration hassle free, I have created a plugin that manages all the dirty work under the hood and you just need to call one of its method to start using. So first download the plugin from here then drag and drop the plugin folder to your xcode project to add.

The plugin does some operations in the background when push comes, so it’s time to do some background related settings as follows:

  • Select your project from project navigator in left pane of your xcode
  • Select project target under TARGETS in the middle pane of your xcode
  • Select Capabilities option from the options available on the top of the middle pane
  • Search for Background Modes from the list and enable it by clicking on the toggle button shown in above image
  • Now select Location updates, Background fetch and Remote notifications options to enable

Figure-1: Xcode Settings

Also open the info.plist file and add NSLocationAlwaysUsageDescription key with relevant message like “Enabling location tracking allows friends to see where you are.”

Start Coding:
Once you are done with the settings mentioned above then it’s time to write some code to use the plugin you added above.

  • First add the following import statement in AppDelegate.m class:
#import "App42PushManager.h"
  • Add the following code to your “application: didFinishLaunchingWithOptions:” method of AppDelegate.m class:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
	[App42API initializeWithAPIKey:@"APP42_APP_KEY" andSecretKey:@"APP42_SECRET_KEY"];
	//[App42API enableApp42Trace:YES]; //Enable to see SDK internal logs

	// Initialize the plugin
	[App42PushManager sharedManager];
	return YES;

The above code initializes App42API and the push plugin you added above. Dont forget to replace API_KEY and SECRET_KEY with the values you got from AppHQ Console.

  • Now add following delegate method to handle the push on receiving in AppDelegate.m class:
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
 	* Handles the geo based push messages and decides the eligibility of the push that should be shown to user or not
	[[App42PushManager sharedManager] handleGeoBasedPush:userInfo fetchCompletionHandler:completionHandler];

Sending Push from AppHQ Console:
Once you have done all the configuration and settings for push notification inside the application as well as on the AppHQ Console, then you just have to select the location and type your message to send it to the users residing on the desired location as shown in Figure-2 and Figure-3.

Figure 2