Documentation

Register Devices


Devices need to be uniquely identified to receive push notifications.

Every device is assigned a unique token that you can use to push it at any given time. Once the device has been assigned a token, it should be stored in your application's backend database. When you're ready to push the device, your backend server will send its token to our REST API, and we'll deliver the push notification to the corresponding device.

Device Registration

Simply add the following blocking code to your application when you're ready to register the device:

String deviceToken = Pushy.register(getApplicationContext());

Note: Pushy.register(Context) is a synchronous, blocking call, so make sure to only execute it from within a background thread.

Asynchronous Implementation

Add the following code to your application when you're ready to register the device:

new RegisterForPushNotificationsAsync().execute();

Copy the following RegisterForPushNotificationsAsync() class implementation and modify it accordingly to send device tokens to your backend server. You can skip the backend implementation for now, we'll get to that later.

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://{YOUR_API_HOSTNAME}/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, do something to alert the user
    }
}