ibm-bluemix-mobile-services / bms-clientsdk-android-push

Android Push notifications SDK for IBM Cloud Mobile Services
Apache License 2.0
10 stars 12 forks source link

No documentation on how to retrieve the deviceId once registered #92

Open l2fprod opened 6 years ago

l2fprod commented 6 years ago

I'm using the following to register a device

        //Register Android devices
        push.registerDevice(new MFPPushResponseListener<String>() {
            @Override
            public void onSuccess(String response) {
                //handle successful device registration here
            }

            @Override
            public void onFailure(MFPPushException ex) {
                //handle failure in device registration here
            }
        });

The javadoc for MFPPush says:

*    // Use Push Service APIs
*    push.registerDevice(new MFPPushResponseListener&lt;String&gt;() {
*      {@literal @}Override
*      public void onSuccess(String deviceId) {
*        ...
*      }
*      {@literal @}Override
*      public void onFailure(MFPPushException ex) {
*        ...
*      }
*    });

It says "onSuccess" would provide the deviceId but it does not look like it. It is a "toString()" of the Response object instead. So how does one retrieve the deviceId?

AnanthaKrish commented 6 years ago

@l2fprod It is a string in response. So you can use anything there.
To get the deviceID please use this,

 public void onSuccess(String response) {
                updateTextView("Device is registered with Push Service.");
                try {
                    response = response.substring(response.indexOf("Response Text: ") + ("Response Text: ").length() , response.length());
                    JSONObject jsonOBJ = new JSONObject(response);
                    String retDeviceId = jsonOBJ.getString("deviceId");
                } catch (JSONException e){
                    Log.e("Error",e.getMessage());
                }
   }

I will update the doc to remove that deviceId part

l2fprod commented 6 years ago

It would be great if the SDK was providing a helper to parse this String. what if you change the format? A proper POJO with fields would be more robust to changes.