OneSignal get User Id from android

2020-06-26 19:10发布

问题:

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.

回答1:

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

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