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.