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:
Simply add the following blocking code to your application when you're ready to register the user for notifications:
String deviceToken = Pushy.register(MainActivity.this);
val deviceToken = Pushy.register(this@MainActivity)
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). Also, please make sure to pass an Activity
context into this method for Android 13 notification runtime permission support.
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(MainActivity.this).execute();
}
if (!Pushy.isRegistered(this)) {
registerForPushNotifications(this@MainActivity)
}
Copy the following 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 (replace MainActivity with your Activity class name)
String deviceToken = Pushy.register(MainActivity.this);
// 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();
}
}
fun registerForPushNotifications(activity: Activity) {
CoroutineScope(Dispatchers.Main).launch {
val result = withContext(Dispatchers.IO) {
try {
// Register the device for notifications
val deviceToken = Pushy.register(activity)
// 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()
// Return the token as the result
deviceToken
} catch (exc: Exception) {
// Return the exception as the result
exc
}
}
// Show the result on the main thread
val message = when (result) {
is Exception -> {
Log.e("Pushy", result.message ?: "Error registering")
result.message ?: "Unknown error occurred"
}
else -> "Pushy device token: $result\n\n(copy from logcat)"
}
// Display dialog
android.app.AlertDialog.Builder(activity)
.setTitle("Pushy")
.setMessage(message)
.setPositiveButton(android.R.string.ok, null)
.show()
}
}
Note: To use Kotin coroutines, please make sure that your app/build.gradle
contains an import for implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:+'
.