@@ -24,13 +24,12 @@ public class IterableApi {
24
24
25
25
protected static IterableApi sharedInstance = null ;
26
26
27
- private Context _context ;
27
+ private Context _applicationContext ;
28
28
private String _apiKey ;
29
29
private String _email ;
30
30
31
31
private Bundle _payloadData ;
32
32
private IterableNotificationData _notificationData ;
33
- private String _pushToken ;
34
33
35
34
private IterableApi (Context context , String apiKey , String email ){
36
35
updateData (context , apiKey , email );
@@ -39,40 +38,35 @@ private IterableApi(Context context, String apiKey, String email){
39
38
/**
40
39
* Returns a shared instance of IterableApi. Updates the client data if an instance already exists.
41
40
* Should be called whenever the app is opened.
42
- * @param context The current activity
41
+ * @param currentActivity The current activity
43
42
* @return stored instance of IterableApi
44
43
*/
45
- public static IterableApi sharedInstanceWithApiKey (Context context , String apiKey , String email )
44
+ public static IterableApi sharedInstanceWithApiKey (Activity currentActivity , String apiKey , String email )
46
45
{
46
+ Context applicationContext = currentActivity .getApplicationContext ();
47
+
47
48
if (sharedInstance == null )
48
49
{
49
- sharedInstance = new IterableApi (context , apiKey , email );
50
+ sharedInstance = new IterableApi (applicationContext , apiKey , email );
50
51
} else {
51
- sharedInstance .updateData (context , apiKey , email );
52
+ sharedInstance .updateData (applicationContext , apiKey , email );
52
53
}
53
54
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 );
64
58
65
59
return sharedInstance ;
66
60
}
67
61
68
62
private void updateData (Context context , String apiKey , String email ) {
69
- this ._context = context ;
63
+ this ._applicationContext = context ;
70
64
this ._apiKey = apiKey ;
71
65
this ._email = email ;
72
66
}
73
67
74
68
protected Context getMainActivityContext () {
75
- return _context ;
69
+ return _applicationContext ;
76
70
}
77
71
78
72
/**
@@ -81,13 +75,11 @@ protected Context getMainActivityContext() {
81
75
* @param iconName
82
76
*/
83
77
public void setNotificationIcon (String iconName ) {
84
- setNotificationIcon (_context , iconName );
78
+ setNotificationIcon (_applicationContext , iconName );
85
79
}
86
80
87
- protected void setPushToken (String token ) { _pushToken = token ; }
88
-
89
81
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 );
91
83
SharedPreferences .Editor editor = sharedPref .edit ();
92
84
editor .putString (NOTIFICATION_ICON_NAME , iconName );
93
85
editor .commit ();
@@ -107,21 +99,26 @@ protected static String getNotificationIcon(Context context) {
107
99
* - https://console.developers.google.com/iam-admin/settings
108
100
*/
109
101
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 );
111
107
pushRegistrationIntent .setAction (IterableConstants .ACTION_PUSH_REGISTRATION );
112
108
pushRegistrationIntent .putExtra (IterableConstants .PUSH_APPID , iterableAppId );
113
109
pushRegistrationIntent .putExtra (IterableConstants .PUSH_PROJECTID , gcmProjectId );
114
- _context .sendBroadcast (pushRegistrationIntent );
110
+ pushRegistrationIntent .putExtra (IterableConstants .PUSH_DISABLE_AFTER_REGISTRATION , disableAfterRegistration );
111
+ _applicationContext .sendBroadcast (pushRegistrationIntent );
115
112
}
116
113
117
114
private void tryTrackNotifOpen (Intent calledIntent ) {
118
115
Bundle extras = calledIntent .getExtras ();
119
116
if (extras != null ) {
120
117
Intent intent = new Intent ();
121
- intent .setClass (_context , IterablePushOpenReceiver .class );
118
+ intent .setClass (_applicationContext , IterablePushOpenReceiver .class );
122
119
intent .setAction (IterableConstants .ACTION_NOTIF_OPENED );
123
120
intent .putExtras (extras );
124
- _context .sendBroadcast (intent );
121
+ _applicationContext .sendBroadcast (intent );
125
122
}
126
123
}
127
124
@@ -314,18 +311,23 @@ public void updateUser(JSONObject dataFields) {
314
311
}
315
312
316
313
public void disablePush (String iterableAppId , String gcmProjectId ) {
317
- registerForPush (iterableAppId , gcmProjectId );
314
+ registerForPush (iterableAppId , gcmProjectId , true );
315
+ }
318
316
317
+ /**
318
+ * Internal api call made from IterablePushRegistrionGCM after a registration is completed.
319
+ * @param token
320
+ */
321
+ protected void disablePush (String token ) {
319
322
JSONObject requestJSON = new JSONObject ();
320
323
try {
321
- requestJSON .put (IterableConstants .KEY_TOKEN , _pushToken );
324
+ requestJSON .put (IterableConstants .KEY_TOKEN , token );
322
325
requestJSON .put (IterableConstants .KEY_EMAIL , _email );
323
326
}
324
327
catch (JSONException e ) {
325
328
e .printStackTrace ();
326
329
}
327
330
sendRequest (IterableConstants .ENDPOINT_DISABLEDEVICE , requestJSON );
328
-
329
331
}
330
332
331
333
/**
@@ -342,6 +344,13 @@ public String getPayloadData(String key) {
342
344
return dataString ;
343
345
}
344
346
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
+
345
354
void setPayloadData (Bundle bundle ) {
346
355
_payloadData = bundle ;
347
356
}
0 commit comments