fedilab-Android-App/app/src/main/java/app/fedilab/android/services/UnifiedPushService.java

95 lines
3.6 KiB
Java
Raw Normal View History

2021-02-21 17:18:37 +01:00
package app.fedilab.android.services;
2021-02-28 10:38:49 +01:00
/* Copyright 2021 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
2021-02-21 17:18:37 +01:00
import android.content.Context;
2021-02-23 18:34:58 +01:00
import android.content.SharedPreferences;
2021-02-28 11:46:34 +01:00
import android.database.sqlite.SQLiteDatabase;
2021-02-21 17:18:37 +01:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.unifiedpush.android.connector.MessagingReceiver;
import org.unifiedpush.android.connector.MessagingReceiverHandler;
2021-02-28 11:46:34 +01:00
import java.util.List;
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.GNUAPI;
import app.fedilab.android.fragments.DisplayNotificationsFragment;
2021-02-22 16:56:04 +01:00
import app.fedilab.android.helper.Helper;
2021-02-28 11:46:34 +01:00
import app.fedilab.android.helper.NotificationsHelper;
2021-02-21 17:18:37 +01:00
import app.fedilab.android.helper.PushNotifications;
2021-02-28 11:46:34 +01:00
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.Sqlite;
2021-02-23 18:34:58 +01:00
import static android.content.Context.MODE_PRIVATE;
class handler implements MessagingReceiverHandler {
2021-02-23 18:34:58 +01:00
2021-02-21 17:18:37 +01:00
@Override
2021-02-28 11:46:34 +01:00
public void onMessage(@Nullable Context context, @NotNull String s) {
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
final List<Account> accounts = new AccountDAO(context, db).getPushNotificationAccounts();
new Thread(() -> {
for (Account account : accounts) {
APIResponse apiResponse;
if (account.getSocial().compareTo("FRIENDICA") != 0 && account.getSocial().compareTo("GNU") != 0) {
API api = new API(context, account.getInstance(), account.getToken());
apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null, false);
} else {
GNUAPI gnuApi = new GNUAPI(context, account.getInstance(), account.getToken());
apiResponse = gnuApi.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null);
}
NotificationsHelper.onRetrieveNotifications(context, apiResponse, account);
}
}).start();
2021-02-28 10:38:49 +01:00
}
@Override
2021-02-28 11:46:34 +01:00
public void onNewEndpoint(@Nullable Context context, @NotNull String endpoint) {
2021-02-23 18:34:58 +01:00
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.SERVER_ENDPOINT, endpoint);
2021-02-23 18:34:58 +01:00
editor.apply();
new PushNotifications()
.registerPushNotifications(context, endpoint);
2021-02-21 17:18:37 +01:00
}
@Override
2021-02-28 11:46:34 +01:00
public void onRegistrationFailed(@Nullable Context context) {
2021-02-21 17:18:37 +01:00
}
@Override
2021-02-28 11:46:34 +01:00
public void onRegistrationRefused(@Nullable Context context) {
2021-02-28 10:38:49 +01:00
2021-02-21 17:18:37 +01:00
}
@Override
2021-02-28 11:46:34 +01:00
public void onUnregistered(@Nullable Context context) {
2021-02-28 10:38:49 +01:00
2021-02-23 18:34:58 +01:00
}
2021-02-21 17:18:37 +01:00
}
2021-02-23 18:34:58 +01:00
public class UnifiedPushService extends MessagingReceiver {
2021-02-21 17:18:37 +01:00
public UnifiedPushService() {
super(new handler());
}
}