Skip to content

Commit 4567c4a

Browse files
committed
* Android Native SDK - app close fix.
* Fixed issue where notification callback would fire instead of showing a notification when init was called from onCreate of the Application class.
1 parent 3d79f18 commit 4567c4a

File tree

3 files changed

+64
-19
lines changed

3 files changed

+64
-19
lines changed

OneSignalSDK/app/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,27 @@ public void beforeEachTest() throws Exception {
118118
notificationOpenedMessage = null;
119119
}
120120

121+
@Test
122+
public void testInitFromApplicationContext() throws Exception {
123+
// Application.onCreate
124+
OneSignal.init(RuntimeEnvironment.application, "123456789", ONESIGNAL_APP_ID);
125+
threadAndTaskWait();
126+
Assert.assertNotNull(ShadowOneSignalRestClient.lastPost);
127+
128+
ShadowOneSignalRestClient.lastPost = null;
129+
StaticResetHelper.restSetStaticFields();
130+
131+
// Restart app, should not send onSession automatically
132+
OneSignal.init(RuntimeEnvironment.application, "123456789", ONESIGNAL_APP_ID);
133+
threadAndTaskWait();
134+
Assert.assertNull(ShadowOneSignalRestClient.lastPost);
135+
136+
// Starting of first Activity should trigger onSession
137+
blankActivityController.resume();
138+
threadAndTaskWait();
139+
Assert.assertNotNull(ShadowOneSignalRestClient.lastPost);
140+
}
141+
121142
@Test
122143
public void testOpenFromNotificationWhenAppIsDead() throws Exception {
123144
OneSignal.handleNotificationOpened(blankActivity, new JSONArray("[{ \"alert\": \"Robo test message\", \"custom\": { \"i\": \"UUID\" } }]"), false);

OneSignalSDK/onesignal/src/main/java/com/onesignal/ActivityLifecycleHandler.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
class ActivityLifecycleHandler {
3636

37+
static boolean nextResumeIsFirstActivity;
38+
3739
interface ActivityAvailableListener {
3840
void available(Activity activity);
3941
}
@@ -108,7 +110,8 @@ static private void handleLostFocus() {
108110
}
109111

110112
static private void handleFocus() {
111-
if (focusHandlerThread.hasBackgrounded()) {
113+
if (focusHandlerThread.hasBackgrounded() || nextResumeIsFirstActivity) {
114+
nextResumeIsFirstActivity = false;
112115
focusHandlerThread.resetBackgroundState();
113116
OneSignal.onAppFocus();
114117
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/OneSignal.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
import com.onesignal.OneSignalDbContract.NotificationTable;
7070

7171
public class OneSignal {
72-
72+
7373
public enum LOG_LEVEL {
7474
NONE, FATAL, ERROR, WARN, INFO, DEBUG, VERBOSE
7575
}
@@ -141,8 +141,10 @@ public void init() {
141141
*/
142142
static final String TAG = "OneSignal";
143143

144-
static String appId;
144+
static String appId, mGoogleProjectNumber;
145145
static Context appContext;
146+
147+
private static boolean startedRegistration;
146148

147149
private static LOG_LEVEL visualLogLevel = LOG_LEVEL.NONE;
148150
private static LOG_LEVEL logCatLevel = LOG_LEVEL.WARN;
@@ -153,7 +155,7 @@ public void init() {
153155
private static NotificationOpenedHandler notificationOpenedHandler;
154156

155157
static boolean initDone;
156-
private static boolean foreground = true;
158+
private static boolean foreground;
157159

158160
private static IdsAvailableHandler idsAvailableHandler;
159161

@@ -162,7 +164,7 @@ public void init() {
162164
private static TrackGooglePurchase trackGooglePurchase;
163165
private static TrackAmazonPurchase trackAmazonPurchase;
164166

165-
public static final String VERSION = "020003";
167+
public static final String VERSION = "020004";
166168

167169
private static AdvertisingIdentifierProvider mainAdIdProvider = new AdvertisingIdProviderGPS();
168170

@@ -213,11 +215,6 @@ public static void init(Context context, String googleProjectNumber, String oneS
213215
osUtils = new OSUtils();
214216

215217
deviceType = osUtils.getDeviceType();
216-
PushRegistrator pushRegistrator;
217-
if (deviceType == 2)
218-
pushRegistrator = new PushRegistratorADM();
219-
else
220-
pushRegistrator = new PushRegistratorGPS();
221218

222219
// START: Init validation
223220
try {
@@ -248,6 +245,8 @@ public static void init(Context context, String googleProjectNumber, String oneS
248245
}
249246
}
250247

248+
mGoogleProjectNumber = googleProjectNumber;
249+
251250
try {
252251
Class.forName("android.support.v4.view.MenuCompat");
253252
try {
@@ -278,11 +277,15 @@ public static void init(Context context, String googleProjectNumber, String oneS
278277
}
279278

280279
// END: Init validation
280+
boolean contextIsActivity = (context instanceof Activity);
281281

282+
foreground = contextIsActivity;
282283
appId = oneSignalAppId;
283284
appContext = context.getApplicationContext();
284-
if (context instanceof Activity)
285+
if (contextIsActivity)
285286
ActivityLifecycleHandler.curActivity = (Activity)context;
287+
else
288+
ActivityLifecycleHandler.nextResumeIsFirstActivity = true;
286289
notificationOpenedHandler = inNotificationOpenedHandler;
287290
lastTrackedTime = SystemClock.elapsedRealtime();
288291

@@ -311,7 +314,30 @@ public static void init(Context context, String googleProjectNumber, String oneS
311314
else
312315
SaveAppId(appId);
313316

314-
pushRegistrator.registerForPush(appContext, googleProjectNumber, new PushRegistrator.RegisteredHandler() {
317+
if (foreground || getUserId() == null)
318+
startRegistrationOrOnSession();
319+
320+
fireCallbackForOpenedNotifications();
321+
322+
if (TrackGooglePurchase.CanTrack(appContext))
323+
trackGooglePurchase = new TrackGooglePurchase(appContext);
324+
325+
initDone = true;
326+
}
327+
328+
private static void startRegistrationOrOnSession() {
329+
if (startedRegistration)
330+
return;
331+
332+
startedRegistration = true;
333+
334+
PushRegistrator pushRegistrator;
335+
if (deviceType == 2)
336+
pushRegistrator = new PushRegistratorADM();
337+
else
338+
pushRegistrator = new PushRegistratorGPS();
339+
340+
pushRegistrator.registerForPush(appContext, mGoogleProjectNumber, new PushRegistrator.RegisteredHandler() {
315341
@Override
316342
public void complete(String id) {
317343
lastRegistrationId = id;
@@ -328,13 +354,6 @@ public void complete(Double lat, Double log) {
328354
registerUser();
329355
}
330356
});
331-
332-
fireCallbackForOpenedNotifications();
333-
334-
if (TrackGooglePurchase.CanTrack(appContext))
335-
trackGooglePurchase = new TrackGooglePurchase(appContext);
336-
337-
initDone = true;
338357
}
339358

340359
private static void fireCallbackForOpenedNotifications() {
@@ -525,6 +544,8 @@ static void onAppFocus() {
525544
foreground = true;
526545
lastTrackedTime = SystemClock.elapsedRealtime();
527546

547+
startRegistrationOrOnSession();
548+
528549
if (trackGooglePurchase != null)
529550
trackGooglePurchase.trackIAP();
530551
}

0 commit comments

Comments
 (0)