Group notifications

This commit is contained in:
tom79 2019-09-26 18:58:43 +02:00
parent 95614fa69c
commit ea3bed1c7f
5 changed files with 81 additions and 68 deletions

View File

@ -1944,7 +1944,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_boost_new"); intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_boost");
startActivity(intent); startActivity(intent);
} }
}); });
@ -1954,7 +1954,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_fav_new"); intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_fav");
startActivity(intent); startActivity(intent);
} }
}); });
@ -1964,7 +1964,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_follow_new"); intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_follow");
startActivity(intent); startActivity(intent);
} }
}); });
@ -1974,7 +1974,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_mention_new"); intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_mention");
startActivity(intent); startActivity(intent);
} }
}); });
@ -1984,7 +1984,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_poll_new"); intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_poll");
startActivity(intent); startActivity(intent);
} }
}); });
@ -1994,7 +1994,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_backup_new"); intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_backup");
startActivity(intent); startActivity(intent);
} }
}); });
@ -2004,7 +2004,7 @@ public class ContentSettingsFragment extends Fragment implements OnRetrieveRemot
public void onClick(View v) { public void onClick(View v) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS); Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName()); intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_store_new"); intent.putExtra(Settings.EXTRA_CHANNEL_ID, "channel_store");
startActivity(intent); startActivity(intent);
} }
}); });

View File

@ -34,7 +34,6 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;

View File

@ -21,6 +21,7 @@ import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.app.DownloadManager; import android.app.DownloadManager;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
@ -1144,39 +1145,39 @@ public class Helper {
switch (notifType) { switch (notifType) {
case BOOST: case BOOST:
channelId = "channel_boost_new" + account.getAcct() + "@" + account.getInstance(); channelId = "channel_boost" + account.getAcct() + "@" + account.getInstance();
channelTitle = context.getString(R.string.channel_notif_boost); channelTitle = context.getString(R.string.channel_notif_boost);
break; break;
case FAV: case FAV:
channelId = "channel_fav_new"; channelId = "channel_fav";
channelTitle = context.getString(R.string.channel_notif_fav); channelTitle = context.getString(R.string.channel_notif_fav);
break; break;
case FOLLLOW: case FOLLLOW:
channelId = "channel_follow_new"; channelId = "channel_follow";
channelTitle = context.getString(R.string.channel_notif_follow); channelTitle = context.getString(R.string.channel_notif_follow);
break; break;
case MENTION: case MENTION:
channelId = "channel_mention_new"; channelId = "channel_mention";
channelTitle = context.getString(R.string.channel_notif_mention); channelTitle = context.getString(R.string.channel_notif_mention);
break; break;
case POLL: case POLL:
channelId = "channel_poll_new"; channelId = "channel_poll";
channelTitle = context.getString(R.string.channel_notif_poll); channelTitle = context.getString(R.string.channel_notif_poll);
break; break;
case BACKUP: case BACKUP:
channelId = "channel_backup_new"; channelId = "channel_backup";
channelTitle = context.getString(R.string.channel_notif_backup); channelTitle = context.getString(R.string.channel_notif_backup);
break; break;
case STORE: case STORE:
channelId = "channel_store_new"; channelId = "channel_store";
channelTitle = context.getString(R.string.channel_notif_media); channelTitle = context.getString(R.string.channel_notif_media);
break; break;
case TOOT: case TOOT:
channelId = "channel_toot_new"; channelId = "channel_toot";
channelTitle = context.getString(R.string.channel_notif_toot); channelTitle = context.getString(R.string.channel_notif_toot);
break; break;
default: default:
channelId = "channel_boost_new"; channelId = "channel_boost";
channelTitle = context.getString(R.string.channel_notif_boost); channelTitle = context.getString(R.string.channel_notif_boost);
} }
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId) NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, channelId)
@ -1250,6 +1251,16 @@ public class Helper {
notificationBuilder.setContentTitle(title); notificationBuilder.setContentTitle(title);
notificationBuilder.setLargeIcon(icon); notificationBuilder.setLargeIcon(icon);
notificationManager.notify(notificationId, notificationBuilder.build()); notificationManager.notify(notificationId, notificationBuilder.build());
Notification summaryNotification =
new NotificationCompat.Builder(context, channelId)
.setContentTitle(account.getAcct()+"@"+account.getInstance())
.setContentText(channelTitle)
.setSmallIcon(getNotificationIcon(context))
.setGroup(account.getAcct() + "@" + account.getInstance())
.setGroupSummary(true)
.build();
notificationManager.notify(0, summaryNotification);
} }

View File

@ -53,6 +53,8 @@ import java.lang.ref.WeakReference;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import app.fedilab.android.R; import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity; import app.fedilab.android.activities.MainActivity;
@ -87,9 +89,11 @@ public class LiveNotificationDelayedService extends Service {
public static int eventsCount = 0; public static int eventsCount = 0;
public static HashMap<String, String> since_ids = new HashMap<>(); public static HashMap<String, String> since_ids = new HashMap<>();
private boolean fetch; private boolean fetch;
private LiveNotificationDelayedService liveNotificationDelayedService;
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
liveNotificationDelayedService = this;
} }
private void startStream() { private void startStream() {
@ -99,20 +103,25 @@ public class LiveNotificationDelayedService extends Service {
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
if (accountStreams != null) { if (accountStreams != null) {
fetch = true; fetch = true;
Handler handler = new Handler(); Timer t = new Timer();
handler.postDelayed(new Runnable() { t.scheduleAtFixedRate(new TimerTask() {
public void run() {
for (final Account accountStream : accountStreams) { @Override
if (accountStream.getSocial() == null || accountStream.getSocial().equals("MASTODON") || accountStream.getSocial().equals("PLEROMA")) { public void run() {
new Fetch(new WeakReference<>(getApplicationContext()), accountStream).execute(); for (final Account accountStream : accountStreams) {
} if (accountStream.getSocial() == null || accountStream.getSocial().equals("MASTODON") || accountStream.getSocial().equals("PLEROMA")) {
} new Fetch(new WeakReference<>(liveNotificationDelayedService), accountStream).execute();
fetch = (Helper.liveNotifType(getApplicationContext()) == Helper.NOTIF_DELAYED); }
if (!fetch) { }
handler.removeMessages(0); fetch = (Helper.liveNotifType(getApplicationContext()) == Helper.NOTIF_DELAYED);
} if( !fetch){
} t.cancel();
}, 30000); }
}
},
0,
30000);
} }
} }
} }
@ -122,9 +131,9 @@ public class LiveNotificationDelayedService extends Service {
private Account accountFetch; private Account accountFetch;
private String key, last_notifid; private String key, last_notifid;
private WeakReference<Context> contextWeakReference; private WeakReference<LiveNotificationDelayedService> contextWeakReference;
Fetch(WeakReference<Context> contextWeakReference, Account account){ Fetch(WeakReference<LiveNotificationDelayedService> contextWeakReference, Account account){
this.accountFetch = account; this.accountFetch = account;
this.contextWeakReference =contextWeakReference; this.contextWeakReference =contextWeakReference;
key = account.getAcct() + "@" + account.getInstance(); key = account.getAcct() + "@" + account.getInstance();
@ -136,11 +145,13 @@ public class LiveNotificationDelayedService extends Service {
@Override @Override
protected APIResponse doInBackground(Void... params) { protected APIResponse doInBackground(Void... params) {
return new API(this.contextWeakReference.get(), accountFetch.getInstance(), accountFetch.getToken()).getNotificationsSince(DisplayNotificationsFragment.Type.ALL, last_notifid, false); return new API(this.contextWeakReference.get(), accountFetch.getInstance(), accountFetch.getToken()).getNotificationsSince(DisplayNotificationsFragment.Type.ALL, last_notifid, false);
} }
@Override @Override
protected void onPostExecute(APIResponse apiResponse) { protected void onPostExecute(APIResponse apiResponse) {
if( apiResponse.getNotifications() != null && apiResponse.getNotifications().size() > 0){ if( apiResponse.getNotifications() != null && apiResponse.getNotifications().size() > 0){
since_ids.put(key, apiResponse.getNotifications().get(0).getId()); since_ids.put(key, apiResponse.getNotifications().get(0).getId());
for (Notification notification : apiResponse.getNotifications()) { for (Notification notification : apiResponse.getNotifications()) {
@ -161,7 +172,7 @@ public class LiveNotificationDelayedService extends Service {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"Live notifications", "Live notifications",
NotificationManager.IMPORTANCE_DEFAULT); NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(false);
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel); ((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
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();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
@ -214,7 +225,7 @@ public class LiveNotificationDelayedService extends Service {
} }
private static void onRetrieveStreaming(WeakReference<Context> contextWeakReference, Account account, Notification notification) { private static void onRetrieveStreaming(WeakReference<LiveNotificationDelayedService> contextWeakReference, Account account, Notification notification) {
Bundle b = new Bundle(); Bundle b = new Bundle();
boolean canSendBroadCast = true; boolean canSendBroadCast = true;
@ -227,7 +238,6 @@ public class LiveNotificationDelayedService extends Service {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
"Live notifications", "Live notifications",
NotificationManager.IMPORTANCE_DEFAULT); NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(false);
((NotificationManager) Objects.requireNonNull(contextWeakReference.get().getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel); ((NotificationManager) Objects.requireNonNull(contextWeakReference.get().getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
android.app.Notification notificationChannel = new NotificationCompat.Builder(contextWeakReference.get(), CHANNEL_ID) android.app.Notification notificationChannel = new NotificationCompat.Builder(contextWeakReference.get(), CHANNEL_ID)
.setContentTitle(contextWeakReference.get().getString(R.string.top_notification)) .setContentTitle(contextWeakReference.get().getString(R.string.top_notification))
@ -336,39 +346,32 @@ public class LiveNotificationDelayedService extends Service {
intent.putExtra(Helper.INTENT_TARGETED_ACCOUNT, targeted_account); intent.putExtra(Helper.INTENT_TARGETED_ACCOUNT, targeted_account);
} }
final String finalMessage = message; final String finalMessage = message;
Handler mainHandler = new Handler(Looper.getMainLooper());
Helper.NotifType finalNotifType = notifType; Helper.NotifType finalNotifType = notifType;
Runnable myRunnable = new Runnable() { if (finalMessage != null) {
@Override Glide.with(contextWeakReference.get())
public void run() { .asBitmap()
if (finalMessage != null) { .load(notification.getAccount().getAvatar())
Glide.with(contextWeakReference.get()) .listener(new RequestListener<Bitmap>() {
.asBitmap() @Override
.load(notification.getAccount().getAvatar()) public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
.listener(new RequestListener<Bitmap>() { return false;
@Override }
public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
@Override @Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
Helper.notify_user(contextWeakReference.get(), account, intent, BitmapFactory.decodeResource(contextWeakReference.get().getResources(), Helper.notify_user(contextWeakReference.get(), account, intent, BitmapFactory.decodeResource(contextWeakReference.get().getResources(),
getMainLogo(contextWeakReference.get())), finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage); getMainLogo(contextWeakReference.get())), finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage);
return false; return false;
} }
}) })
.into(new SimpleTarget<Bitmap>() { .into(new SimpleTarget<Bitmap>() {
@Override @Override
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) { public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
Helper.notify_user(contextWeakReference.get(), account, intent, resource, finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage); Helper.notify_user(contextWeakReference.get(), account, intent, resource, finalNotifType, "@" + notification.getAccount().getAcct(), finalMessage);
} }
}); });
} }
}
};
mainHandler.post(myRunnable);
} }
} }
@ -384,6 +387,7 @@ public class LiveNotificationDelayedService extends Service {
editor.apply(); editor.apply();
} }
} catch (Exception ignored) { } catch (Exception ignored) {
ignored.printStackTrace();
} }
} }

View File

@ -144,7 +144,7 @@ public class LiveNotificationService extends Service implements NetworkStateRece
channel = new NotificationChannel(CHANNEL_ID, channel = new NotificationChannel(CHANNEL_ID,
"Live notifications", "Live notifications",
NotificationManager.IMPORTANCE_DEFAULT); NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(false);
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel); ((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
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();
List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction(); List<Account> accountStreams = new AccountDAO(getApplicationContext(), db).getAllAccountCrossAction();
@ -301,7 +301,6 @@ public class LiveNotificationService extends Service implements NetworkStateRece
channel = new NotificationChannel(CHANNEL_ID, channel = new NotificationChannel(CHANNEL_ID,
"Live notifications", "Live notifications",
NotificationManager.IMPORTANCE_DEFAULT); NotificationManager.IMPORTANCE_DEFAULT);
channel.setShowBadge(false);
((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel); ((NotificationManager) Objects.requireNonNull(getSystemService(Context.NOTIFICATION_SERVICE))).createNotificationChannel(channel);
android.app.Notification notificationChannel = new NotificationCompat.Builder(this, CHANNEL_ID) android.app.Notification notificationChannel = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle(getString(R.string.top_notification)) .setContentTitle(getString(R.string.top_notification))