Skip to content

Commit

Permalink
Merge pull request apptentive#107 from apptentive/AND-270
Browse files Browse the repository at this point in the history
AND-270
  • Loading branch information
skykelsey committed Sep 2, 2015
2 parents 4f61cda + ea92078 commit b52b2c7
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 140 deletions.
249 changes: 115 additions & 134 deletions apptentive/src/com/apptentive/android/sdk/Apptentive.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,32 @@ public static void onStop(Activity activity) {
* communication, and to help provide more context about this user. This email will be the definitive email address
* for this user, unless one is provided directly by the user through an Apptentive UI. Calls to this method are
* idempotent. Calls to this method will overwrite any previously entered email, so if you don't want to overwrite
* the email provided by the user, make sure to check the value before you call this method.
* the email provided by the user, make sure to check the value with {@link #getPersonEmail(Context)} before you call this method.
*
* @param context The context from which this method is called.
* @param context The Context from which this method is called.
* @param email The user's email address.
*/
public static void setPersonEmail(Context context, String email) {
PersonManager.storePersonEmail(context, email);
}

/**
* Retrieves the user's email address. This address may be set via {@link #setPersonEmail(Context, String)},
* or by the user through Message Center.
*
* @param context The Context from which this method is called.
* @return
*/
public static String getPersonEmail(Context context) {
return PersonManager.loadPersonEmail(context);
}

/**
* Sets the user's name. This name will be sent to the Apptentive server and displayed in conversations you have
* with this person. This name will be the definitive username for this user, unless one is provided directly by the
* user through an Apptentive UI. Calls to this method are idempotent. Calls to this method will overwrite any
* previously entered email, so if you don't want to overwrite the email provided by the user, make sure to check
* the value before you call this method.
* the value with {@link #getPersonName(Context)} before you call this method.
*
* @param context The context from which this method is called.
* @param name The user's name.
Expand All @@ -142,6 +153,18 @@ public static void setPersonName(Context context, String name) {
PersonManager.storePersonName(context, name);
}

/**
* Retrieves the user's name. This name may be set via {@link #setPersonName(Context, String)},
* or by the user through Message Center.
*
* @param context The Context from which this method is called.
* @return
*/
public static String getPersonName(Context context) {
return PersonManager.loadPersonName(context);
}


/**
* <p>Allows you to pass arbitrary string data to the server along with this device's info. This method will replace all
* custom device data that you have set for this app. Calls to this method are idempotent.</p>
Expand Down Expand Up @@ -265,66 +288,14 @@ public static void removeCustomPersonData(Context context, String key) {
// THIRD PARTY INTEGRATIONS
// ****************************************************************************************

/**
* The key to use to store a Map of Apptentive Push integration settings.
*/
public static final String INTEGRATION_APPTENTIVE_PUSH = "apptentive_push";

/**
* The key to use to specify the GCM Registration ID within Apptentive Push integration settings.
*/
public static final String INTEGRATION_APPTENTIVE_PUSH_REGISTRATION_ID = "token";

/**
* The key to use to store a Map of Urban Airship configuration settings.
*/
public static final String INTEGRATION_URBAN_AIRSHIP = "urban_airship";

/**
* The key to use to specify the Urban Airship Channel ID within Apptentive's push integration Map.
*/
public static final String INTEGRATION_URBAN_AIRSHIP_CHANNEL_ID = "token";

/**
* The key to use to store a Map of Amazon SNS configuration settings.
*/
public static final String INTEGRATION_AWS_SNS = "aws_sns";

/**
* The key to use to specify the Amazon SNS Registration ID within Apptentive's push integration Map.
*/
public static final String INTEGRATION_AWS_SNS_TOKEN = "token";

/**
* The key to use to store a Map of Parse configuration settings.
*/
public static final String INTEGRATION_PARSE = "parse";
private static final String INTEGRATION_APPTENTIVE_PUSH = "apptentive_push";
private static final String INTEGRATION_PARSE = "parse";
private static final String INTEGRATION_URBAN_AIRSHIP = "urban_airship";
private static final String INTEGRATION_AWS_SNS = "aws_sns";

/**
* The key to use to specify the Parse channel ID within Apptentive's push integration Map.
*/
public static final String INTEGRATION_PARSE_DEVICE_TOKEN = "token";
private static final String INTEGRATION_PUSH_TOKEN = "token";

/**
* Allows you to pass in third party integration details. Supported push providers:
* <ul>
* <li>
* Urban Airship
* </li>
* <li>
* Amazon SNS via GCM
* </li>
* <li>
* Parse
* </li>
* </ul>
*
* @param context The Context from which this method is called.
* @param integration The name of the integration. Integrations known at the time this SDK was released are listed below.
* @param config A String to String Map of key/value pairs representing all necessary configuration data Apptentive needs
* to use the specific third party integration.
*/
public static void addIntegration(Context context, String integration, Map<String, String> config) {
private static void addIntegration(Context context, String integration, Map<String, String> config) {
if (integration == null || config == null) {
return;
}
Expand All @@ -349,89 +320,99 @@ public static void addIntegration(Context context, String integration, Map<Strin
}

/**
* Configures your app to receive Apptentive push notifications. Use this method if you do not
* currently use a push provider, and would like to receive push notifications sent from
* Apptentive. You must first set up your app to GCM, per our documentation.
*
* @param context The Context from which this method is called.
* @param registrationId The GCM Registration ID.
* Call {@link #setPushNotificationIntegration(Context, int, String)} with this value to allow Apptentive to send pushes
* to this device without a third party push provider. Requires a valid GCM configuration.
*/
public static void addApptentivePushIntegration(Context context, String registrationId) {
if (registrationId != null) {
Log.d("Setting up Apptentive Push Notifications with Registration ID: %s", registrationId);
Map<String, String> config = new HashMap<String, String>();
config.put(Apptentive.INTEGRATION_APPTENTIVE_PUSH_REGISTRATION_ID, registrationId);
addIntegration(context, Apptentive.INTEGRATION_APPTENTIVE_PUSH, config);
}
}
public static final int PUSH_PROVIDER_APPTENTIVE = 0;

/**
* Call {@link #setPushNotificationIntegration(Context, int, String)} with this value to allow Apptentive to send pushes
* to this device through your existing Parse Push integration. Requires a valid Parse integration.
*/
public static final int PUSH_PROVIDER_PARSE = 1;

/**
* Configures Apptentive to work with Urban Airship push notifications. You must first set up your app to work with
* Urban Airship to use this integration. This method must be called when you finish initializing Urban Airship. Since
* Urban Airship creates a Channel ID after it connects to its server, the Channel ID may be null at first. The
* preferred method of retrieving the Channel ID is to listen to the
* <code>PushManager.ACTION_REGISTRATION_FINISHED</code> Intent in your {@link android.content.BroadcastReceiver}. You
* can alternately find the Channel ID by calling <a href="http://docs.urbanairship.com/reference/libraries/android/latest/reference/com/urbanairship/push/PushManager.html#getChannelId%28%29">PushManager.shared().getChannelId()</a>
* <p/>
* Push notifications will not be delivered to this app install until our server receives the APID.
*
* @param context The Context from which this method is called.
* @param channelId The Android Channel ID.
* Call {@link #setPushNotificationIntegration(Context, int, String)} with this value to allow Apptentive to send pushes
* to this device through your existing Urban Airship Push integration. Requires a valid Urban
* Airship Push integration.
*/
public static void addUrbanAirshipPushIntegration(Context context, String channelId) {
if (channelId != null) {
Log.d("Setting Urban Airship Channel ID: %s", channelId);
Map<String, String> config = new HashMap<String, String>();
config.put(Apptentive.INTEGRATION_URBAN_AIRSHIP_CHANNEL_ID, channelId);
addIntegration(context, Apptentive.INTEGRATION_URBAN_AIRSHIP, config);
}
}
public static final int PUSH_PROVIDER_URBAN_AIRSHIP = 2;

/**
* Configures Apptentive to work with Amazon Web Services (AWS) Simple Notification Service (SNS) push notifications.
* You must first set up your app to work with AWS SNS to use this integration. This method must be called when you
* finish initializing AWS SNS using
* <a href="http://developer.android.com/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#register%28java.lang.String...%29">
* GoogleCloudMessaging.register(String... senderIds)</a>,
* which returns the Registration ID. You will need to pass this returned Registration ID into this method.
* <p/>
* Push notifications will not be delivered to this app install until our server receives the Registration ID.
*
* @param context The Context from which this method was called.
* @param registrationId The registrationId returned from
* <a href="http://developer.android.com/reference/com/google/android/gms/gcm/GoogleCloudMessaging.html#register%28java.lang.String...%29">
* GoogleCloudMessaging.register(String... senderIds)</a>.
* Call {@link #setPushNotificationIntegration(Context, int, String)} with this value to allow Apptentive to send pushes
* to this device through your existing Amazon AWS SNS integration. Requires a valid Amazon AWS SNS
* integration.
*/
public static void addAmazonSnsPushIntegration(Context context, String registrationId) {
if (registrationId != null) {
Log.d("Setting Amazon AWS token: %s", registrationId);
Map<String, String> config = new HashMap<String, String>();
config.put(Apptentive.INTEGRATION_AWS_SNS_TOKEN, registrationId);
addIntegration(context, Apptentive.INTEGRATION_AWS_SNS, config);
}
}
public static final int PUSH_PROVIDER_AMAZON_AWS_SNS = 3;

/**
* Configures Apptentive to work with Parse push notifications. You must first set up your app to work with Parse to
* use this integration. You must call this method, and pass in the Parse deviceToken when you finish initializing
* Parse push notifications in your app. Please see our <a href="http://www.apptentive.com/docs/android/integration/">
* integration guide</a> for instructions.
* <p/>
* Push notifications will not be delivered to this app install until our server receives the deviceToken.
* Sends push provider information to our server to allow us to send pushes to this device when
* you reply to your customers. Only one push provider is allowed to be active at a time, so you
* should only call this method once. Please see our
* <a href="http://www.apptentive.com/docs/android/integration/#push-notifications">integration guide</a> for
* instructions.
*
* @param context The Context from which this method was called.
* @param deviceToken The deviceToken returned from
* @param pushProvider One of the following:
* <ul>
* <li>{@link #PUSH_PROVIDER_APPTENTIVE}</li>
* <li>{@link #PUSH_PROVIDER_PARSE}</li>
* <li>{@link #PUSH_PROVIDER_URBAN_AIRSHIP}</li>
* <li>{@link #PUSH_PROVIDER_AMAZON_AWS_SNS}</li>
* </ul>
* @param token The push provider token you receive from your push provider. The format is push provider specific.
* <dl>
* <dt>Apptentive</dt>
* <dd>If you are using Apptentive to send pushes directly to GCM, pass in the GCM Registration ID, which you can
* <a href="https://github.com/googlesamples/google-services/blob/73f8a4fcfc93da08a40b96df3537bb9b6ef1b0fa/android/gcm/app/src/main/java/gcm/play/android/samples/com/gcmquickstart/RegistrationIntentService.java#L51">access like this</a>.
* </dd>
* <dt>Parse</dt>
* <dd>The Parse <a href="https://parse.com/docs/android/guide#push-notifications">deviceToken</a></dd>
* <dt>Urban Airship</dt>
* <dd>The Urban Airship Channel ID, which you can
* <a href="https://github.com/urbanairship/android-samples/blob/8ad77e5e81a1b0507c6a2c45a5c30a1e2da851e9/PushSample/src/com/urbanairship/push/sample/IntentReceiver.java#L43">access like this</a>.
* </dd>
* <dt>Amazon AWS SNS</dt>
* <dd>The GCM Registration ID, which you can <a href="http://docs.aws.amazon.com/sns/latest/dg/mobile-push-gcm.html#registration-id-gcm">access like this</a>.</dd>
* </dl>
*/
public static void addParsePushIntegration(Context context, String deviceToken) {
if (deviceToken != null) {
Log.d("Setting Parse Device Token: %s", deviceToken);
Map<String, String> config = new HashMap<String, String>();
config.put(Apptentive.INTEGRATION_PARSE_DEVICE_TOKEN, deviceToken);
addIntegration(context, Apptentive.INTEGRATION_PARSE, config);
public static void setPushNotificationIntegration(Context context, int pushProvider, String token) {
try {
CustomData integrationConfig = getIntegrationConfigurationWithoutPushProviders(context);
JSONObject pushObject = new JSONObject();
pushObject.put(INTEGRATION_PUSH_TOKEN, token);
switch (pushProvider) {
case PUSH_PROVIDER_APPTENTIVE:
integrationConfig.put(INTEGRATION_APPTENTIVE_PUSH, pushObject);
break;
case PUSH_PROVIDER_PARSE:
integrationConfig.put(INTEGRATION_PARSE, pushObject);
break;
case PUSH_PROVIDER_URBAN_AIRSHIP:
integrationConfig.put(INTEGRATION_URBAN_AIRSHIP, pushObject);
break;
case PUSH_PROVIDER_AMAZON_AWS_SNS:
integrationConfig.put(INTEGRATION_AWS_SNS, pushObject);
break;
default:
Log.e("Invalid pushProvider: %d", pushProvider);
return;
}
DeviceManager.storeIntegrationConfig(context, integrationConfig);
syncDevice(context);
} catch (JSONException e) {
Log.e("Error setting push integration.", e);
return;
}
}

private static CustomData getIntegrationConfigurationWithoutPushProviders(Context context) {
CustomData integrationConfig = DeviceManager.loadIntegrationConfig(context);
integrationConfig.remove(INTEGRATION_APPTENTIVE_PUSH);
integrationConfig.remove(INTEGRATION_PARSE);
integrationConfig.remove(INTEGRATION_URBAN_AIRSHIP);
integrationConfig.remove(INTEGRATION_AWS_SNS);
return integrationConfig;
}

// ****************************************************************************************
// PUSH NOTIFICATIONS
Expand Down Expand Up @@ -498,8 +479,8 @@ public static boolean setPendingPushNotification(Context context, Bundle bundle)

/**
* Launches Apptentive features based on a push notification Intent. Before you call this, you
* must call {@link Apptentive#setPendingPushNotification(Context, Intent)} or
* {@link Apptentive#setPendingPushNotification(Context, Bundle)} in your Broadcast receiver when
* must call {@link #setPendingPushNotification(Context, Intent)} or
* {@link #setPendingPushNotification(Context, Bundle)} in your Broadcast receiver when
* a push notification is opened by the user. This method must be called from the Activity that
* you launched from the BroadcastReceiver. This method will only handle Apptentive originated
* push notifications, so you can and should call it any time your push notification launches an
Expand Down Expand Up @@ -604,7 +585,7 @@ public static boolean canShowMessageCenter(Context context) {
* Set a listener to be notified when the number of unread messages in the Message Center changes.
*
* @param listener An UnreadMessageListener that you instantiate.
* @deprecated use {@link #addUnreadMessagesListener(UnreadMessagesListener listener)} instead.
* @deprecated use {@link #addUnreadMessagesListener(UnreadMessagesListener)} instead.
*/
@Deprecated
public static void setUnreadMessagesListener(UnreadMessagesListener listener) {
Expand Down Expand Up @@ -797,15 +778,15 @@ public static synchronized boolean engage(Activity activity, String event, Map<S
* the ability to target interactions to run after the user uploads a file in your app. You may then
* call <strong><code>engage(activity, "finished_upload");</code></strong>
* @return true if an immediate call to engage() with the same event name would result in an Interaction being displayed, otherwise false.
* @deprecated Use {@link Apptentive#canShowInteraction}() instead. The behavior is identical. Only the name has changed.
* @deprecated Use {@link #canShowInteraction(Context, String)}() instead. The behavior is identical. Only the name has changed.
*/
public static synchronized boolean willShowInteraction(Context context, String event) {
return canShowInteraction(context, event);
}

/**
* This method can be used to determine if a call to one of the <strong><code>engage()</code></strong> methods such as
* {@link com.apptentive.android.sdk.Apptentive#engage(android.app.Activity, String)} using the same event name will
* {@link #engage(Activity, String)} using the same event name will
* result in the display of an Interaction. This is useful if you need to know whether an Interaction will be
* displayed before you create a UI Button, etc.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Apptentive, Inc. All Rights Reserved.
* Copyright (c) 2015, Apptentive, Inc. All Rights Reserved.
* Please refer to the LICENSE file for the terms and conditions
* under which redistribution and use of this file is permitted.
*/
Expand Down Expand Up @@ -90,7 +90,7 @@ public static CustomData loadCustomDeviceData(Context context) {
public static void storeCustomDeviceData(Context context, CustomData deviceData) {
SharedPreferences prefs = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
String deviceDataString = deviceData.toString();
prefs.edit().putString(Constants.PREF_KEY_DEVICE_DATA, deviceDataString).commit();
prefs.edit().putString(Constants.PREF_KEY_DEVICE_DATA, deviceDataString).apply();
}

public static CustomData loadIntegrationConfig(Context context) {
Expand All @@ -112,7 +112,7 @@ public static CustomData loadIntegrationConfig(Context context) {
public static void storeIntegrationConfig(Context context, CustomData integrationConfig) {
SharedPreferences prefs = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
String integrationConfigString = integrationConfig.toString();
prefs.edit().putString(Constants.PREF_KEY_DEVICE_INTEGRATION_CONFIG, integrationConfigString).commit();
prefs.edit().putString(Constants.PREF_KEY_DEVICE_INTEGRATION_CONFIG, integrationConfigString).apply();
}

private static Device generateNewDevice(Context context) {
Expand Down Expand Up @@ -164,7 +164,7 @@ public static Device getStoredDevice(Context context) {

private static void storeDevice(Context context, Device device) {
SharedPreferences prefs = context.getSharedPreferences(Constants.PREF_NAME, Context.MODE_PRIVATE);
prefs.edit().putString(Constants.PREF_KEY_DEVICE, device.toString()).commit();
prefs.edit().putString(Constants.PREF_KEY_DEVICE, device.toString()).apply();
}

public static void onSentDeviceInfo(Context appContext) {
Expand Down
Loading

0 comments on commit b52b2c7

Please sign in to comment.