Last active
December 15, 2023 04:48
-
-
Save zackgilbert/c57a5a0164eb7c114a198d0bc7b74de8 to your computer and use it in GitHub Desktop.
Simple Example Pilgrim Setup for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.foursquare.pilgrim.sample"> | |
<!-- The following permissions are added to your manifest file automatically when you import the Pilgrim SDK library: --> | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> | |
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | |
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> | |
<uses-permission android:name="android.permission.WAKE_LOCK" /> | |
<!-- NOTE: In this example, we are using App.java as the main application file, | |
which is where Pilgrim needs to be initialize via the builder: --> | |
<application | |
android:name=".App" | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:theme="@style/AppTheme"> | |
<!-- This activity is where we might call PilgrimSdk.start(this); (once you've gotten location permissions from the user) --> | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
</application> | |
</manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// Make sure that the Pilgrim handlers are available from the Application and the Pilgrim initialization is called from the Application's onCreate: | |
public abstract class App extends Application { | |
// Set up the Pilgrim Notification Handler: | |
public final PilgrimNotificationHandler pilgrimNotificationHandler = new PilgrimNotificationHandler() { | |
// Primary visit handler | |
@Override | |
public void handleVisit(Context context, PilgrimSdkVisitNotification notification) { | |
Visit visit = notification.getVisit(); | |
Venue venue = visit.getVenue(); | |
// Do something with the place | |
} | |
// Optional: If visit occurred while in Doze mode or without network connectivity | |
@Override | |
public void handleBackfillVisit(Context context, PilgrimSdkBackfillNotification notification) { | |
super.handleBackfillVisit(context, notification); | |
Visit visit = notification.getVisit(); | |
Venue venue = visit.getVenue(); | |
// Do something with the place | |
} | |
// Optional: If visit occurred by triggering a geofence | |
@Override | |
public void handleGeofenceEventNotification(Context context, PilgrimSdkGeofenceEventNotification notification) { | |
List<GeofenceEvent> geofenceEvents = notification.getGeofenceEvents(); | |
// Do something with the events | |
} | |
}; | |
// This App.java extends Application, so this onCreate needs to initialize Pilgrim via the builder: | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
// Configure the Pilgrim SDK: | |
PilgrimSdk.Builder builder = new PilgrimSdk.Builder(this) | |
.consumer("CLIENT_ID", "CLIENT_SECRET") | |
.notificationHandler(pilgrimNotificationHandler) | |
.logLevel(LogLevel.DEBUG); | |
PilgrimSdk.with(builder); | |
// Set any custom user data you want to pass through: | |
PilgrimUserInfo userInfo = new PilgrimUserInfo(); | |
userInfo.setUserId(PilgrimSdk.getInstallId()); | |
// You can also set other custom fields using: | |
userInfo.put("myCustomField", "My custom value"); | |
// Starting in v2.1.2, you have the ability to persist userinfo across sessions: | |
PilgrimSdk.get().setUserInfo(userInfo, persisted: true); | |
// To unset persisted fields: | |
PilgrimSdk.get().setUserInfo(null, persisted: true) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment