cimenmus cimenmus - 3 months ago 65
Android Question

onCreate() method of Application class not working as expected with OneSignal

I use OneSignal to send push notifications in my Android app. Here is my onCreate() method of Application class :

public class App extends Application {
@Override
public void onCreate() {
super.onCreate();

OneSignal.startInit(this)
.setNotificationOpenedHandler(new MyNotificationOpenedHelper())
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.init();

Log.i("onesignaal", "app onCreate()");
}
}


But the problem is here: When I run the app first time, onCreate() method called as expected. But when I press home button and clear my app from working app, the onCreate() method recalled. And whenever I run the app after first run, onCreate() method does not called when app launches, it called everytime I completely close the app. (everytime I completely close the app and then run)

When I delete OneSignal initialization :

public class App extends Application {
@Override
public void onCreate() {
super.onCreate();

Log.i("onesignaal", "app onCreate()");
}
}


onCreate() method runs as aspected : it called everytime app launches and does not called closing app completely. Where is the problem?

EDIT :

I found the reason. I have a method to get OneSignal user id and save it my database. When I call that method on onCreate() method of Application, the problem occurs. But when i do not call that method, onCreate() method works as aspected. Here that function :

private void saveOnesignalId(){

OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
@Override
public void idsAvailable(final String userId, String registrationId) {

Log.i("onesignaal", "userId : " + userId);
Log.i("onesignaal", "registrationId : " + registrationId);

}
});

}


But i still need solution.

EDIT : Here all of the Application class:

public class App extends Application {

@Override
public void onCreate() {
super.onCreate();

SugarContext.init(this);

OneSignal.startInit(this)
.setNotificationOpenedHandler(new MyNotificationOpenedHelper())
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.init();

Log.i("onesignaal", "app onCreate()");

saveOnesignalId();

}

@Override
public void onTerminate() {
super.onTerminate();
SugarContext.terminate();
}


private void saveOnesignalId(){

OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
@Override
public void idsAvailable(final String userId, String registrationId) {

Log.i("onesignaal", "userId : " + userId);
Log.i("onesignaal", "registrationId : " + registrationId);

}
});

}


}


EDIT : I think the problem is on OneSignal SDK. When I delete all OneSignal methods, there is no problem. onCreate() method works properly. But when I any of OneSignal methods, the onCreate() method of the applications do not work properly.

Answer

Application.onCreate is called when your app's process is started just before any Activities, BroadcastReceivers , or Services are started. You will need to consider this when placing any code in the method as it is not always related directly to a user interaction with your app.

The reason you are seeing onCreate being fired when the app is swiped away after you added OneSignal is it adds a SyncService Service class. It is started in a sticky state with your app to ensure any pending tags and other session fields are sent. When your app is swiped away Android kills your process, then starts it up again with OneSignal's SyncService. The service will check for any pending changes and will stop it self if there is not.

Any Intent sent to your app (like a received GCM message) will also cause onCreate to be called. For more details on the Application and process lifecycle (different from the Activity lifecycle) I recommend reading the following.

https://developer.android.com/guide/topics/processes/process-lifecycle.html https://developer.android.com/reference/android/app/Application.html#onCreate() https://developer.android.com/guide/components/processes-and-threads.html