I am trying to implement OneSignal Android sdk. Everything is OK excepts get userId In OneSignal Documentation, here has the documentation. the steps are:

defaultConfig {
        applicationId "com.nuspay.onesignaldemo"

        manifestPlaceholders = [manifestApplicationId: "${applicationId}",
                                onesignal_app_id: "OPEN_SIGNAL_ID",
                                onesignal_google_project_number: "GOOGLE_PLAY_PROJECT_NUMBER"]
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

And add this gradle file as dependencies:

 compile 'com.onesignal:OneSignal:2.+@aar'
    compile 'com.google.android.gms:play-services-gcm:+'
    compile 'com.google.android.gms:play-services-analytics:+'
    compile "com.google.android.gms:play-services-location:+"

In menifest.xml add Application class and this permission

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Application Class is like this:

public class DemoApplication extends Application{
    public static final String TAG = DemoApplication.class.getSimpleName();
    private static DemoApplication instace;

    @Override
    public void onCreate() {
        super.onCreate();
        instace=this;
        initialization();
    }

    private void initialization() {
        OneSignal.startInit(this)
        .setAutoPromptLocation(true)
        .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler()).
        init();

        OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
            @Override
            public void idsAvailable(String userId, String registrationId) {
                Log.d("debug", "User:" + userId);
                if (registrationId != null)
                    Log.d("debug", "registrationId:" + registrationId);

            }
        });
        OneSignal.enableNotificationsWhenActive(true);
        OneSignal.enableInAppAlertNotification(true);
        OneSignal.promptLocation();
    }
}

ExampleNotificationOpenedHandler is like this:

public class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {

    @Override
    public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
        try {

            Log.d("HWT","Message-->"+message);
            Log.d("HWT","additionalData-->"+additionalData);
            Log.d("HWT","isActive-->"+isActive);

            if (additionalData != null) {
                if (additionalData.has("actionSelected"))
                    Log.d("HWT", "OneSignal notification button with id " + additionalData.getString("actionSelected") + " pressed");

                Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    }

Now in my Activity onCreate callback using below code to get userId.

OneSignal.idsAvailable(new OneSignal.IdsAvailableHandler() {
            @Override
            public void idsAvailable(String userId, String registrationId) {
                String text = "OneSignal UserID:\n" + userId + "\n\n";
            }
        });

My problem is idsAvailable callback is never called.

Notes:

  1. I setUp GCM permission, get project number, set Open signal project all are ok

Can you please identify my drawbacks. Thanks in advance.

upvote
  flag
Are you receiving the userID inside DemoApplication ? or idsAvailable is never call both on your activity and application ? – agonist_
upvote
  flag
Also I faced the same issue, but it was me doing dumb things, I putted my User key in my graddle file instead of the App ID/Key the one you can find under > API KEYS menu. The notification worked also but not the idsAvailable, so maybe you did the same mistake ... + 'GOOGLE_PLAY_PROJECT_NUMBER' it's google cloud not google play, just to be sure too – agonist_
upvote
  flag
@agonist_ , thanks for your comment. I didn't getting call idsAvailable method anymore. What do you mean by " User key" instead of "App ID/Key" ? – Md. Sajedul Karim

1 Answers 11

up vote 2 down vote accepted

Finally, I solved it by removing this lines from Application class:

OneSignal.enableNotificationsWhenActive(true);
    OneSignal.enableInAppAlertNotification(true);
    OneSignal.promptLocation();
upvote
  flag
These lines shouldn't have any affect on the IdsAvailableHandler. There might be a 5 to 15 seconds delay for this callback to fire if it is the first time the app is run as it is registered with Google and OneSignal. Also on Android 6.0 devices it will not fire until the location permission prompt is accepted or declined. – jkasten
upvote
  flag
@jaksten, thanks for your comment. But unfortunately i didn't get result from IdsAvailableHandler . After removing this lines i get the result. – Md. Sajedul Karim
upvote
  flag
Is promptLocation the only method that is creating the issue? Or is enableInAppAlertNotificationand enableNotificationsWhenActive creating problems as well? Are you using the latest 2.3.0 SDK version? Can you check for any issues in the logcat? – jkasten

Not the answer you're looking for? Browse other questions tagged or ask your own question.