6969import com .onesignal .OneSignalDbContract .NotificationTable ;
7070
7171public 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