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. In addition, you should only call this method once per app installation, as it performs a network request when called and may waste precious device resources if called excessively.

Additional Note: If you'd like to avoid paying multiple times for the same device when your app is reinstalled, make sure to only call this method after your app has been granted the WRITE_EXTERNAL_STORAGE permission, so that our SDK can reuse the previously-assigned token.

Asynchronous Implementation

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

if (!Pushy.isRegistered(getApplicationContext())) {
    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;

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

        // Succeeded, optionally do something to alert the user