94 lines
3.8 KiB
Java
94 lines
3.8 KiB
Java
package app.fedilab.android.services;
|
|
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
import android.util.Log;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.Nullable;
|
|
import org.unifiedpush.android.connector.MessagingReceiver;
|
|
import org.unifiedpush.android.connector.MessagingReceiverHandler;
|
|
|
|
import java.util.List;
|
|
|
|
import app.fedilab.android.activities.BaseMainActivity;
|
|
import app.fedilab.android.activities.LiveNotificationSettingsAccountsActivity;
|
|
import app.fedilab.android.asynctasks.PostSubscriptionAsyncTask;
|
|
import app.fedilab.android.client.APIResponse;
|
|
import app.fedilab.android.client.Entities.Account;
|
|
import app.fedilab.android.helper.Helper;
|
|
import app.fedilab.android.helper.PushNotifications;
|
|
import app.fedilab.android.interfaces.OnPostSubscription;
|
|
import app.fedilab.android.sqlite.AccountDAO;
|
|
import app.fedilab.android.sqlite.Sqlite;
|
|
|
|
import static android.content.Context.MODE_PRIVATE;
|
|
|
|
class handler implements MessagingReceiverHandler, OnPostSubscription {
|
|
|
|
private Context context;
|
|
private String endpoint;
|
|
|
|
@Override
|
|
public void onNewEndpoint(@Nullable Context context, @NotNull String s) {
|
|
Log.v(Helper.TAG, "onNewEndpoint: " + s);
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
editor.putString(Helper.SERVER_ENDPOINT, s);
|
|
editor.apply();
|
|
endpoint = s;
|
|
|
|
this.context = context;
|
|
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
List<Account> accountPush = new AccountDAO(context, db).getPushNotificationAccounts();
|
|
for (Account account : accountPush) {
|
|
new PostSubscriptionAsyncTask(context, account, s, this);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onRegistrationFailed(@Nullable Context context) {
|
|
Log.v(Helper.TAG, "onRegistrationFailed: ");
|
|
// Toast ?
|
|
}
|
|
|
|
@Override
|
|
public void onRegistrationRefused(@Nullable Context context) {
|
|
// Toast ?
|
|
Log.v(Helper.TAG, "onRegistrationRefused: ");
|
|
}
|
|
|
|
@Override
|
|
public void onUnregistered(@Nullable Context context) {
|
|
Log.v(Helper.TAG, "onUnregistered: ");
|
|
// Remove endpoint & ServerKey
|
|
}
|
|
|
|
@Override
|
|
public void onMessage(@Nullable Context context, @NotNull String s) {
|
|
PushNotifications push = new PushNotifications();
|
|
Log.v(Helper.TAG, "Message: " + s);
|
|
push.displayNotification(context, null, s);
|
|
}
|
|
|
|
@Override
|
|
public void onSubscription(APIResponse apiResponse) {
|
|
if (apiResponse != null && apiResponse.getPushSubscription() != null && apiResponse.getAccounts() != null) {
|
|
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
Log.v(Helper.TAG, "OK: " + apiResponse.getAccounts().get(0).getId() + apiResponse.getAccounts().get(0).getInstance() + " --- " + apiResponse.getPushSubscription().getServer_key());
|
|
editor.putString(Helper.SERVER_KEY + apiResponse.getAccounts().get(0).getId() + apiResponse.getAccounts().get(0).getInstance(), apiResponse.getPushSubscription().getServer_key());
|
|
editor.apply();
|
|
PushNotifications push = new PushNotifications();
|
|
push.registerPushNotifications(context, apiResponse.getAccounts().get(0), endpoint, apiResponse.getPushSubscription().getServer_key());
|
|
}
|
|
}
|
|
}
|
|
|
|
public class UnifiedPushService extends MessagingReceiver {
|
|
public UnifiedPushService() {
|
|
super(new handler());
|
|
}
|
|
}
|