Fix double notifications
This commit is contained in:
parent
c4481e8279
commit
6d9f7357df
|
@ -84,14 +84,62 @@ public class LiveNotificationDelayedService extends Service {
|
||||||
public static HashMap<String, String> since_ids = new HashMap<>();
|
public static HashMap<String, String> since_ids = new HashMap<>();
|
||||||
private static Thread thread;
|
private static Thread thread;
|
||||||
private boolean fetch;
|
private boolean fetch;
|
||||||
public boolean zeroAccounts;
|
|
||||||
|
|
||||||
|
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
if (Build.VERSION.SDK_INT >= 26) {
|
||||||
|
channel = new NotificationChannel(CHANNEL_ID,
|
||||||
|
"Live notifications",
|
||||||
|
NotificationManager.IMPORTANCE_DEFAULT);
|
||||||
|
|
||||||
|
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
|
||||||
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||||
|
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
|
||||||
|
totalAccount = 0;
|
||||||
|
for (Account account : accountStreams) {
|
||||||
|
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA") || account.getSocial().equals("PIXELFED")) {
|
||||||
|
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||||
|
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
|
||||||
|
if (allowStream) {
|
||||||
|
totalAccount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( totalAccount > 0) {
|
||||||
|
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getActivity(
|
||||||
|
getApplicationContext(),
|
||||||
|
0,
|
||||||
|
myIntent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
|
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
|
.setShowWhen(false)
|
||||||
|
.setContentIntent(pendingIntent)
|
||||||
|
.setContentTitle(getString(R.string.top_notification))
|
||||||
|
.setSmallIcon(R.drawable.fedilab_notification_icon)
|
||||||
|
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
|
||||||
|
|
||||||
|
startForeground(1, notification);
|
||||||
|
}else{
|
||||||
|
stopSelf();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
startStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||||
|
if( totalAccount > 0) {
|
||||||
|
return START_STICKY;
|
||||||
|
}
|
||||||
|
return START_NOT_STICKY;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void startStream() {
|
private void startStream() {
|
||||||
|
|
||||||
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||||
|
@ -125,54 +173,6 @@ public class LiveNotificationDelayedService extends Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
||||||
zeroAccounts = false;
|
|
||||||
if (Build.VERSION.SDK_INT >= 26) {
|
|
||||||
channel = new NotificationChannel(CHANNEL_ID,
|
|
||||||
"Live notifications",
|
|
||||||
NotificationManager.IMPORTANCE_DEFAULT);
|
|
||||||
|
|
||||||
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
|
|
||||||
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
||||||
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
|
|
||||||
totalAccount = 0;
|
|
||||||
for (Account account : accountStreams) {
|
|
||||||
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA") || account.getSocial().equals("PIXELFED")) {
|
|
||||||
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
||||||
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
|
|
||||||
if (allowStream) {
|
|
||||||
totalAccount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if( totalAccount > 0) {
|
|
||||||
|
|
||||||
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(
|
|
||||||
getApplicationContext(),
|
|
||||||
0,
|
|
||||||
myIntent,
|
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
||||||
|
|
||||||
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
|
||||||
.setShowWhen(false)
|
|
||||||
.setContentIntent(pendingIntent)
|
|
||||||
.setContentTitle(getString(R.string.top_notification))
|
|
||||||
.setSmallIcon(R.drawable.fedilab_notification_icon)
|
|
||||||
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
|
|
||||||
|
|
||||||
startForeground(1, notification);
|
|
||||||
}else{
|
|
||||||
stopSelf();
|
|
||||||
zeroAccounts = true;
|
|
||||||
return START_NOT_STICKY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
startStream();
|
|
||||||
return START_STICKY;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@Override
|
@Override
|
||||||
|
@ -183,7 +183,7 @@ public class LiveNotificationDelayedService extends Service {
|
||||||
@Override
|
@Override
|
||||||
public void onTaskRemoved(Intent rootIntent) {
|
public void onTaskRemoved(Intent rootIntent) {
|
||||||
super.onTaskRemoved(rootIntent);
|
super.onTaskRemoved(rootIntent);
|
||||||
if( !zeroAccounts) {
|
if( totalAccount > 0) {
|
||||||
restart();
|
restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -107,7 +107,6 @@ public class LiveNotificationService extends Service implements NetworkStateRece
|
||||||
public static int totalAccount = 0;
|
public static int totalAccount = 0;
|
||||||
public static int eventsCount = 0;
|
public static int eventsCount = 0;
|
||||||
public static int liveNotifBadge = 0;
|
public static int liveNotifBadge = 0;
|
||||||
public boolean zeroAccounts;
|
|
||||||
|
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
@ -116,6 +115,42 @@ public class LiveNotificationService extends Service implements NetworkStateRece
|
||||||
networkStateReceiver.addListener(this);
|
networkStateReceiver.addListener(this);
|
||||||
registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
|
registerReceiver(networkStateReceiver, new IntentFilter(android.net.ConnectivityManager.CONNECTIVITY_ACTION));
|
||||||
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= 26) {
|
||||||
|
channel = new NotificationChannel(CHANNEL_ID,
|
||||||
|
"Live notifications",
|
||||||
|
NotificationManager.IMPORTANCE_DEFAULT);
|
||||||
|
|
||||||
|
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
|
||||||
|
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||||
|
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
|
||||||
|
totalAccount = 0;
|
||||||
|
for (Account account : accountStreams) {
|
||||||
|
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA")) {
|
||||||
|
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
|
||||||
|
if (allowStream) {
|
||||||
|
totalAccount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if( totalAccount > 0) {
|
||||||
|
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getActivity(
|
||||||
|
getApplicationContext(),
|
||||||
|
0,
|
||||||
|
myIntent,
|
||||||
|
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
||||||
|
.setContentTitle(getString(R.string.top_notification))
|
||||||
|
.setContentIntent(pendingIntent)
|
||||||
|
.setSmallIcon(getNotificationIcon(getApplicationContext()))
|
||||||
|
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
|
||||||
|
|
||||||
|
startForeground(1, notification);
|
||||||
|
}else{
|
||||||
|
stopSelf();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startStream() {
|
private void startStream() {
|
||||||
|
@ -141,46 +176,10 @@ public class LiveNotificationService extends Service implements NetworkStateRece
|
||||||
if (intent == null || intent.getBooleanExtra("stop", false)) {
|
if (intent == null || intent.getBooleanExtra("stop", false)) {
|
||||||
stopSelf();
|
stopSelf();
|
||||||
}
|
}
|
||||||
zeroAccounts = false;
|
if( totalAccount > 0) {
|
||||||
if (Build.VERSION.SDK_INT >= 26) {
|
return START_STICKY;
|
||||||
channel = new NotificationChannel(CHANNEL_ID,
|
}
|
||||||
"Live notifications",
|
return START_NOT_STICKY;
|
||||||
NotificationManager.IMPORTANCE_DEFAULT);
|
|
||||||
|
|
||||||
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
|
|
||||||
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
||||||
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
|
|
||||||
totalAccount = 0;
|
|
||||||
for (Account account : accountStreams) {
|
|
||||||
if (account.getSocial() == null || account.getSocial().equals("MASTODON") || account.getSocial().equals("PLEROMA")) {
|
|
||||||
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
||||||
boolean allowStream = sharedpreferences.getBoolean(Helper.SET_ALLOW_STREAM + account.getId() + account.getInstance(), true);
|
|
||||||
if (allowStream) {
|
|
||||||
totalAccount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if( totalAccount > 0) {
|
|
||||||
Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
|
|
||||||
PendingIntent pendingIntent = PendingIntent.getActivity(
|
|
||||||
getApplicationContext(),
|
|
||||||
0,
|
|
||||||
myIntent,
|
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
|
||||||
android.app.Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
|
|
||||||
.setContentTitle(getString(R.string.top_notification))
|
|
||||||
.setContentIntent(pendingIntent)
|
|
||||||
.setSmallIcon(getNotificationIcon(getApplicationContext()))
|
|
||||||
.setContentText(getString(R.string.top_notification_message, String.valueOf(totalAccount), String.valueOf(eventsCount))).build();
|
|
||||||
|
|
||||||
startForeground(1, notification);
|
|
||||||
}else{
|
|
||||||
zeroAccounts = true;
|
|
||||||
stopSelf();
|
|
||||||
return START_NOT_STICKY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return START_STICKY;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -200,7 +199,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece
|
||||||
@Override
|
@Override
|
||||||
public void onTaskRemoved(Intent rootIntent) {
|
public void onTaskRemoved(Intent rootIntent) {
|
||||||
super.onTaskRemoved(rootIntent);
|
super.onTaskRemoved(rootIntent);
|
||||||
if( !zeroAccounts) {
|
if( totalAccount > 0) {
|
||||||
restart();
|
restart();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue