本文介绍了OneSignal从android获取用户ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实施OneSignal Android sdk。除非获得 userId
,否则一切正常。在OneSignal文档中,有文档。
的步骤是:

  defaultConfig {
applicationIdcom.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
versionName1.0
}

文件作为依赖项:

  compile'c​​om.onesignal:OneSignal:2。+ @ aar'
compile'c​​om。 google.android.gms:play-services-gcm:+'
compile'c​​om.google.android.gms:play-services-analytics:+'
compilecom.google.android.gms: play-services-location:+

在menifest.xml中添加应用程序类和此权限

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

Application Class如下所示:

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


code
$ b $ ExampleNotificationOpenedHandler 是这样的:

  public class ExampleNotificationOpenedHandler实现了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.has(actionSelected))
Log.d(HWT,带有ID的OneSignal通知按钮 + additionalData.getString(actionSelected)+pressed);

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





$ p $现在在我的 Activity onCreate 使用以下代码进行回调以获取userId。

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

我的问题是 idsAvailable callback


  1. I setUp GCM 许可,获得项目编号,设置打开信号项目都可以

您能否确认我的缺点。

解决方案

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

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


I am trying to implement OneSignal Android sdk. Everything is OK excepts get userIdIn 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.

解决方案

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

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

这篇关于OneSignal从android获取用户ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 07:45