OneSignal从Android获取用户ID

时间:2016-05-16 08:11:42

标签: android google-cloud-messaging onesignal

我正在尝试实施OneSignal Android sdk。一切都好,除了userId 在OneSignal文档中,here包含文档。 步骤是:

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"
    }

将此gradle文件添加为依赖项:

 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:+"

在menifest.xml中添加Application类和此权限

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

应用程序类是这样的:

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是这样的:

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

现在在我的Activity onCreate回调中使用以下代码获取userId。

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

我的问题是永远不会调用idsAvailable回调。

注意:

  1. 我设置GCM权限,获取项目编号,设置打开信号项目一切正常
  2. 请你明白我的缺点。提前谢谢。

1 个答案:

答案 0 :(得分:2)

最后,我通过从Application类中删除这些行来解决它:

CREATE OR REPLACE PROCEDURE INSERTPRODUCTRECORD9 (p_PNAME IN VARCHAR2, p_CATEGORY IN VARCHAR2, p_PRICE IN NUMBER) 
AS 
BEGIN 
    INSERT INTO PRODUCT (PNAME,PRICE,CATEGORY) VALUES(p_PNAME, p_PRICE,     p_CATEGORY ); 
COMMIT;
END;

begin
    INSERTPRODUCTRECORD9('TAB','GADGETS',30000);
END;