Register Devices


Users need to be uniquely identified to receive push notifications.

Every user is assigned a unique device token that you can use to push it at any given time. Once the user has been assigned a device token, it should be stored in your application's backend database.


Please select your Android project type for the appropriate instructions:

Java
Kotlin

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

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

This method returns a unique device token you can use to send notifications to this specific user's device.

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

Asynchronous Implementation

Add the following code to your application when you're ready to register the user for notifications:

if (!Pushy.isRegistered(this)) {
    new RegisterForPushNotificationsAsync(this).execute();
}
if (!Pushy.isRegistered(this)) {
    RegisterForPushNotificationsAsync(this).execute()
}

Copy the following RegisterForPushNotificationsAsync() async implementation and modify it accordingly to send device tokens to your backend server.

private class RegisterForPushNotificationsAsync extends AsyncTask<Void, Void, Object> {
    Activity mActivity;

    public RegisterForPushNotificationsAsync(Activity activity) {
        this.mActivity = activity;
    }

    protected Object doInBackground(Void... params) {
        try {
            // Register the device for notifications
            String deviceToken = Pushy.register(getApplicationContext());

            // Registration succeeded, log token to logcat
            Log.d("Pushy", "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();

            // Provide token to onPostExecute()
            return deviceToken;
        }
        catch (Exception exc) {
            // Registration failed, provide exception to onPostExecute()
            return exc;
        }
    }

    @Override
    protected void onPostExecute(Object result) {
        String message;

        // Registration failed?
        if (result instanceof Exception) {
            // Log to console
            Log.e("Pushy", result.toString());

            // Display error in alert
            message = ((Exception) result).getMessage();
        }
        else {
            message = "Pushy device token: " + result.toString() + "\n\n(copy from logcat)";
        }

        // Registration succeeded, display an alert with the device token
        new android.app.AlertDialog.Builder(this.mActivity)
                .setTitle("Pushy")
                .setMessage(message)
                .setPositiveButton(android.R.string.ok, null)
                .show();
    }
}
class RegisterForPushNotificationsAsync(activity: Activity) : AsyncTask<Void, Void, Any>() {
    var activity: Activity = activity;

    override fun doInBackground(vararg params: Void): Any {
        try {
            // Register the device for notifications
            val deviceToken = Pushy.register(activity)

            // Registration succeeded, log token to logcat
            Log.d("Pushy", "Pushy device token: " + deviceToken)

            // Send the token to your backend server via an HTTP GET request
            URL("https://{YOUR_API_HOSTNAME}/register/device?token=" + deviceToken).openConnection()

            // Provide token to onPostExecute()
            return deviceToken
        } catch (exc: Exception) {
            // Registration failed, provide exception to onPostExecute()
            return exc
        }
    }

    override fun onPostExecute(result: Any) {
        var message: String

        // Registration failed?
        if (result is Exception) {
            // Log to console
            Log.e("Pushy", result.message)

            // Display error in alert
            message = result.message.toString()
        }
        else {
            // Registration success, result is device token
            message = "Pushy device token: " + result.toString() + "\n\n(copy from logcat)"
        }

        // Display dialog
        android.app.AlertDialog.Builder(activity)
                .setTitle("Pushy")
                .setMessage(message)
                .setPositiveButton(android.R.string.ok, null)
                .show()
    }
}