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.


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

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

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).

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 user for notifications:

if (!Pushy.isRegistered(this)) {
    new 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) {
        // Registration failed?
        if (result instanceof Exception) {
            // Display error as toast message
            Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
            return;
        }

        // Registration succeeded, display an alert with the device token
        new android.app.AlertDialog.Builder(this.mActivity)
                .setTitle("Registration success")
                .setMessage("Pushy device token: " + result.toString() + "\n\n(copy from logcat)")
                .setPositiveButton(android.R.string.ok, null)
                .show();
    }
}