Onesignal-android-sdk: onCreate() method of Application class not working as expected with OneSignal

Created on 3 Sep 2016  路  5Comments  路  Source: OneSignal/OneSignal-Android-SDK

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();

        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);

            }
        });

    }


}

OnCreate() method of the Application works when app first launched. But it is not called 2nd, 3rd, 4th... launches. It called just after I press the home button and clear my app from working apps.

But when I do not call any of OneSignal methods, onCreate() method works properly. It called my app every launches (after clearing my app from working apps) and did not called after deleting app from working apps. That is normal working of onCreate() method. But if I call any of OneSignal methods, onCreate() method is not called while app launching, it is called just after I clear my app from working apps.

I use last OneSignal Android SDK. 谋 think there may be a bug on OneSignal Android SDK.

Most helpful comment

Please accept my apologies for the fact that I interfere in your fascinating conversation.

As @cimenmus said:

But onCreate() method of Application class is not called while app is launching, it is called just after clearing my application from working applications.

This situation is absolutely normal.
@cimenmus You can find here, here and here more information about that.

Once again I offer my apologies for my inappropriate behavior.

All 5 comments

The onCreate in your Application class is called when your app's process is started. This will happen any time a Service or BroadcastReceiver is started in-addition to your app being opened by the user.

You most likely will only want to call OneSignal.idsAvailable from your Activity.

But onCreate() method of Application class is not called while app is launching, it is called just after clearing my application from working applications. Normally, it is called while application is launching.

I think i should call OneSignal.idsAvailable method in main activity of the application, like you said.

Please accept my apologies for the fact that I interfere in your fascinating conversation.

As @cimenmus said:

But onCreate() method of Application class is not called while app is launching, it is called just after clearing my application from working applications.

This situation is absolutely normal.
@cimenmus You can find here, here and here more information about that.

Once again I offer my apologies for my inappropriate behavior.

But if I do not call any method of OneSignal class, onCreate() method works normally; called when app is launching. But if I call any of OneSignal class method, onCreate() is not called while app is launching, it is called after clear my app from working apps. I think there is a bug or some logic on OneSignal.

@cimenmus

I have replied to your stackoverflow post as well. Adding the contains of it below for reference.

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 add 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

Thanks.

Was this page helpful?
0 / 5 - 0 ratings