@@ -24,13 +24,12 @@ public class IterableApi {
2424
2525 protected static IterableApi sharedInstance = null ;
2626
27- private Context _context ;
27+ private Context _applicationContext ;
2828 private String _apiKey ;
2929 private String _email ;
3030
3131 private Bundle _payloadData ;
3232 private IterableNotificationData _notificationData ;
33- private String _pushToken ;
3433
3534 private IterableApi (Context context , String apiKey , String email ){
3635 updateData (context , apiKey , email );
@@ -39,40 +38,35 @@ private IterableApi(Context context, String apiKey, String email){
3938 /**
4039 * Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
4140 * Should be called whenever the app is opened.
42- * @param context The current activity
41+ * @param currentActivity The current activity
4342 * @return stored instance of IterableApi
4443 */
45- public static IterableApi sharedInstanceWithApiKey (Context context , String apiKey , String email )
44+ public static IterableApi sharedInstanceWithApiKey (Activity currentActivity , String apiKey , String email )
4645 {
46+ Context applicationContext = currentActivity .getApplicationContext ();
47+
4748 if (sharedInstance == null )
4849 {
49- sharedInstance = new IterableApi (context , apiKey , email );
50+ sharedInstance = new IterableApi (applicationContext , apiKey , email );
5051 } else {
51- sharedInstance .updateData (context , apiKey , email );
52+ sharedInstance .updateData (applicationContext , apiKey , email );
5253 }
5354
54- if (context instanceof Activity ) {
55- Activity currentActivity = (Activity ) context ;
56- Intent calledIntent = currentActivity .getIntent ();
57- sharedInstance .tryTrackNotifOpen (calledIntent );
58- }
59- else {
60- Log .d (TAG , "Notification Opens will not be tracked: " +
61- "sharedInstanceWithApiKey called with a Context that is not an instance of Activity. " +
62- "Pass in an Activity to IterableApi.sharedInstanceWithApiKey to enable open tracking." );
63- }
55+ Intent calledIntent = currentActivity .getIntent ();
56+ sharedInstance .setPayloadData (calledIntent );
57+ sharedInstance .tryTrackNotifOpen (calledIntent );
6458
6559 return sharedInstance ;
6660 }
6761
6862 private void updateData (Context context , String apiKey , String email ) {
69- this ._context = context ;
63+ this ._applicationContext = context ;
7064 this ._apiKey = apiKey ;
7165 this ._email = email ;
7266 }
7367
7468 protected Context getMainActivityContext () {
75- return _context ;
69+ return _applicationContext ;
7670 }
7771
7872 /**
@@ -81,13 +75,11 @@ protected Context getMainActivityContext() {
8175 * @param iconName
8276 */
8377 public void setNotificationIcon (String iconName ) {
84- setNotificationIcon (_context , iconName );
78+ setNotificationIcon (_applicationContext , iconName );
8579 }
8680
87- protected void setPushToken (String token ) { _pushToken = token ; }
88-
8981 protected static void setNotificationIcon (Context context , String iconName ) {
90- SharedPreferences sharedPref = (( Activity ) context ) .getSharedPreferences (NOTIFICATION_ICON_NAME , Context .MODE_PRIVATE );
82+ SharedPreferences sharedPref = context .getSharedPreferences (NOTIFICATION_ICON_NAME , Context .MODE_PRIVATE );
9183 SharedPreferences .Editor editor = sharedPref .edit ();
9284 editor .putString (NOTIFICATION_ICON_NAME , iconName );
9385 editor .commit ();
@@ -107,21 +99,26 @@ protected static String getNotificationIcon(Context context) {
10799 * - https://console.developers.google.com/iam-admin/settings
108100 */
109101 public void registerForPush (String iterableAppId , String gcmProjectId ) {
110- Intent pushRegistrationIntent = new Intent (_context , IterablePushReceiver .class );
102+ registerForPush (iterableAppId , gcmProjectId , false );
103+ }
104+
105+ protected void registerForPush (String iterableAppId , String gcmProjectId , boolean disableAfterRegistration ) {
106+ Intent pushRegistrationIntent = new Intent (_applicationContext , IterablePushReceiver .class );
111107 pushRegistrationIntent .setAction (IterableConstants .ACTION_PUSH_REGISTRATION );
112108 pushRegistrationIntent .putExtra (IterableConstants .PUSH_APPID , iterableAppId );
113109 pushRegistrationIntent .putExtra (IterableConstants .PUSH_PROJECTID , gcmProjectId );
114- _context .sendBroadcast (pushRegistrationIntent );
110+ pushRegistrationIntent .putExtra (IterableConstants .PUSH_DISABLE_AFTER_REGISTRATION , disableAfterRegistration );
111+ _applicationContext .sendBroadcast (pushRegistrationIntent );
115112 }
116113
117114 private void tryTrackNotifOpen (Intent calledIntent ) {
118115 Bundle extras = calledIntent .getExtras ();
119116 if (extras != null ) {
120117 Intent intent = new Intent ();
121- intent .setClass (_context , IterablePushOpenReceiver .class );
118+ intent .setClass (_applicationContext , IterablePushOpenReceiver .class );
122119 intent .setAction (IterableConstants .ACTION_NOTIF_OPENED );
123120 intent .putExtras (extras );
124- _context .sendBroadcast (intent );
121+ _applicationContext .sendBroadcast (intent );
125122 }
126123 }
127124
@@ -314,18 +311,23 @@ public void updateUser(JSONObject dataFields) {
314311 }
315312
316313 public void disablePush (String iterableAppId , String gcmProjectId ) {
317- registerForPush (iterableAppId , gcmProjectId );
314+ registerForPush (iterableAppId , gcmProjectId , true );
315+ }
318316
317+ /**
318+ * Internal api call made from IterablePushRegistrionGCM after a registration is completed.
319+ * @param token
320+ */
321+ protected void disablePush (String token ) {
319322 JSONObject requestJSON = new JSONObject ();
320323 try {
321- requestJSON .put (IterableConstants .KEY_TOKEN , _pushToken );
324+ requestJSON .put (IterableConstants .KEY_TOKEN , token );
322325 requestJSON .put (IterableConstants .KEY_EMAIL , _email );
323326 }
324327 catch (JSONException e ) {
325328 e .printStackTrace ();
326329 }
327330 sendRequest (IterableConstants .ENDPOINT_DISABLEDEVICE , requestJSON );
328-
329331 }
330332
331333 /**
@@ -342,6 +344,13 @@ public String getPayloadData(String key) {
342344 return dataString ;
343345 }
344346
347+ void setPayloadData (Intent intent ) {
348+ Bundle extras = intent .getExtras ();
349+ if (extras != null && !extras .isEmpty () && extras .containsKey (IterableConstants .ITERABLE_DATA_KEY )) {
350+ setPayloadData (extras );
351+ }
352+ }
353+
345354 void setPayloadData (Bundle bundle ) {
346355 _payloadData = bundle ;
347356 }
0 commit comments