0

am using pushy for push notifications but am not able to store the device token in the database.

    private class RegisterForPushNotificationsAsync extends AsyncTask<Void, Void, Exception> {
    protected Exception doInBackground(Void... params) {
        try {
            // Assign a unique token to this device
            String deviceToken = Pushy.register(getApplicationContext());


            // Log it for debugging purposes
            Log.d("MyApp", "Pushy device token: " + deviceToken);

            // Send the token to your backend server via an HTTP GET request
            new URL("https://key}/register/device?token=" + deviceToken).openConnection();
        } catch (Exception exc) {
            // Return exc to onPostExecute
            return exc;
        }

        // Success
        return null;
    }

    @Override
    protected void onPostExecute(Exception exc) {
        // Failed?
        if (exc != null) {
            // Show error as toast message
            Toast.makeText(getApplicationContext(), exc.toString(), Toast.LENGTH_LONG).show();
            return;
        }

        // Succeeded, optionally do something to alert the user
    }
}

I am using retrofit for the http requests and am not using any kind of backend system

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52

1 Answers1

0

What you're doing is well enough to get you a Device Token from Pushy service.

If you want to capture the returned device token and make it accessible to the AsyncTask class and the enclosing class in general (as you stated in the comments), then you can declare a global/instance String variable, say pushy_device_token, in the enclosing class.

Then in doInBackground() method of the AsyncTask, go ahead and assign the global variable as follows:

pushy_device_token = Pushy.register(getApplicationContext());

Complete code:

public class EnclosingClass {
  
  String pushy_device_token;
  
  
  //  Additional class code
  
  
  private class RegisterForPushNotificationsAsync extends AsyncTask<Void, Void, Exception> {
  
    @Override
    protected Exception doInBackground(Void... params) {
        try {
            // Assign a unique token to this device
            pushy_device_token = Pushy.register(getApplicationContext());


            // Log it for debugging purposes
            Log.d("MyApp", "Pushy device token: " + deviceToken);

            // Send the token to your backend server via an HTTP GET request
            new URL("https://key}/register/device?token=" + deviceToken).openConnection();
        } catch (Exception exc) {
            // Return exc to onPostExecute
            return exc;
        }

        // Success
        return null;
    }

    @Override
    protected void onPostExecute(Exception exc) {
        // Failed?
        if (exc != null) {
            // Show error as toast message
            Toast.makeText(getApplicationContext(), exc.toString(), Toast.LENGTH_LONG).show();
            return;
        }

        // Succeeded, optionally do something to alert the user
    }
  }
  
}

Best practice recommendation:

It's best to have the result of processing in doInBackground(), returned in the onPostExecute() method, especially if you're going to do some UI work. So from onPostExecute(), you can do anything you want with the result, e.g. display to the user, report an error, etc.

To do this, you'll have to modify your doInBackground() method to return something as generic as Object. And so onPostExecute() will take in an Object as a parameter variable.

You'll modify by:

private class RegisterForPushNotificationsAsync extends AsyncTask<Void, Void, Object> { . . .

From this, you can check if the Object taken in by onPostExecute() is of type Exception, in which case, you'll display an error notification, or check if it's of type String, in which case you'll have the device token which you can then proceed to save in your DB (Firebase, SQLite, etc.).

Edward Quixote
  • 350
  • 5
  • 16