Fix issue #215 - Only last notification is displayed

This commit is contained in:
Thomas 2022-07-07 18:19:55 +02:00
parent b8addbc3f1
commit 17ed03af91
5 changed files with 33 additions and 33 deletions

View File

@ -117,6 +117,7 @@ import java.security.Security;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -1379,6 +1380,10 @@ public class Helper {
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
} }
private static final List<String> present = new ArrayList<>();
private static int notificationId = 1;
/** /**
* Sends notification with intent * Sends notification with intent
* *
@ -1389,7 +1394,7 @@ public class Helper {
* @param message String message for the notification * @param message String message for the notification
*/ */
@SuppressLint("UnspecifiedImmutableFlag") @SuppressLint("UnspecifiedImmutableFlag")
public static void notify_user(Context context, int notificationId, BaseAccount account, Intent intent, Bitmap icon, NotifType notifType, String title, String message) { public static void notify_user(Context context, BaseAccount account, Intent intent, Bitmap icon, NotifType notifType, String title, String message) {
final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); final SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
// prepare intent which is triggered if the user click on the notification // prepare intent which is triggered if the user click on the notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context); NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
@ -1440,14 +1445,11 @@ public class Helper {
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)
.setSmallIcon(R.drawable.ic_notification).setTicker(message) .setSmallIcon(R.drawable.ic_notification).setTicker(message);
.setWhen(System.currentTimeMillis())
.setAutoCancel(true);
if (notifType == NotifType.MENTION) { if (notifType == NotifType.MENTION) {
if (message.length() > 500) { if (message.length() > 500) {
message = message.substring(0, 499) + ""; message = message.substring(0, 499) + "";
} }
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
} }
notificationBuilder.setGroup(account.mastodon_account.acct + "@" + account.instance) notificationBuilder.setGroup(account.mastodon_account.acct + "@" + account.instance)
.setContentIntent(pIntent) .setContentIntent(pIntent)
@ -1515,19 +1517,28 @@ public class Helper {
} }
notificationBuilder.setContentTitle(title); notificationBuilder.setContentTitle(title);
notificationBuilder.setLargeIcon(icon); notificationBuilder.setLargeIcon(icon);
notificationManager.notify(notificationId, notificationBuilder.build());
Notification summaryNotification =
new NotificationCompat.Builder(context, channelId) Notification summaryNotification = null;
.setContentTitle(title)
.setContentText(channelTitle) if (!present.contains(account.mastodon_account.acct + "@" + account.instance)) {
.setContentIntent(pIntent) summaryNotification = new NotificationCompat.Builder(context, channelId)
.setLargeIcon(icon) .setContentTitle(title)
.setSmallIcon(R.drawable.ic_notification) .setContentText(channelTitle)
.setGroup(account.mastodon_account.acct + "@" + account.instance) .setContentIntent(pIntent)
.setGroupSummary(true) .setLargeIcon(icon)
.build(); .setSmallIcon(R.drawable.ic_notification)
notificationManager.notify(notificationId, summaryNotification); .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setGroup(account.mastodon_account.acct + "@" + account.instance)
.setGroupSummary(true)
.build();
present.add(account.mastodon_account.acct + "@" + account.instance);
}
notificationManager.notify(notificationId++, notificationBuilder.build());
if (summaryNotification != null) {
notificationManager.notify(0, summaryNotification);
}
} }
public static void transfertIfExist(Context context) { public static void transfertIfExist(Context context) {

View File

@ -163,7 +163,7 @@ public class MediaHelper {
Uri uri = Uri.fromFile(backupFile); Uri uri = Uri.fromFile(backupFile);
intent.setDataAndType(uri, mime); intent.setDataAndType(uri, mime);
if (!share) { if (!share) {
notify_user(context, Helper.NOTIFICATION_MEDIA, currentAccount, intent, BitmapFactory.decodeResource(context.getResources(), notify_user(context, currentAccount, intent, BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher), Helper.NotifType.STORE, context.getString(R.string.save_over), context.getString(R.string.download_from, fileName)); R.mipmap.ic_launcher), Helper.NotifType.STORE, context.getString(R.string.save_over), context.getString(R.string.download_from, fileName));
Toasty.success(context, context.getString(R.string.save_over), Toasty.LENGTH_LONG).show(); Toasty.success(context, context.getString(R.string.save_over), Toasty.LENGTH_LONG).show();
} else { } else {

View File

@ -317,7 +317,7 @@ public class NotificationsHelper {
since_ids.put(account.user_id + "@" + account.instance, lastNotif); since_ids.put(account.user_id + "@" + account.instance, lastNotif);
editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id); editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id);
editor.apply(); editor.apply();
notify_user(context, Helper.NOTIFICATION_USER_NOTIF, account, intent, BitmapFactory.decodeResource(context.getResources(), notify_user(context, account, intent, BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher), finalNotifType, finalTitle, finalMessage); R.mipmap.ic_launcher), finalNotifType, finalTitle, finalMessage);
} }
return false; return false;
@ -332,7 +332,7 @@ public class NotificationsHelper {
editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id); editor.putString(context.getString(R.string.LAST_NOTIFICATION_ID) + account.user_id + "@" + account.instance, notifications.get(0).id);
editor.apply(); editor.apply();
since_ids.put(account.user_id + "@" + account.instance, lastNotif); since_ids.put(account.user_id + "@" + account.instance, lastNotif);
notify_user(context, Helper.NOTIFICATION_USER_NOTIF, account, intent, resource, finalNotifType, finalTitle, finalMessage); notify_user(context, account, intent, resource, finalNotifType, finalTitle, finalMessage);
} }
} }

View File

@ -571,7 +571,7 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
Uri uri = Uri.parse("file://" + fullPath); Uri uri = Uri.parse("file://" + fullPath);
intentOpen.setDataAndType(uri, "text/csv"); intentOpen.setDataAndType(uri, "text/csv");
String title = getString(R.string.data_export_theme); String title = getString(R.string.data_export_theme);
Helper.notify_user(getActivity(), Helper.NOTIFICATION_THEMING, currentAccount, intentOpen, BitmapFactory.decodeResource(requireActivity().getResources(), Helper.notify_user(getActivity(), currentAccount, intentOpen, BitmapFactory.decodeResource(requireActivity().getResources(),
R.mipmap.ic_launcher), Helper.NotifType.BACKUP, title, message); R.mipmap.ic_launcher), Helper.NotifType.BACKUP, title, message);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -14,8 +14,6 @@ package app.fedilab.android.ui.fragment.timeline;
* You should have received a copy of the GNU General Public License along with Fedilab; if not, * You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
@ -32,7 +30,6 @@ import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.R; import app.fedilab.android.R;
import app.fedilab.android.client.entities.api.Announcement; import app.fedilab.android.client.entities.api.Announcement;
import app.fedilab.android.databinding.FragmentPaginationBinding; import app.fedilab.android.databinding.FragmentPaginationBinding;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.ThemeHelper; import app.fedilab.android.helper.ThemeHelper;
import app.fedilab.android.ui.drawer.AnnouncementAdapter; import app.fedilab.android.ui.drawer.AnnouncementAdapter;
import app.fedilab.android.viewmodel.mastodon.AnnouncementsVM; import app.fedilab.android.viewmodel.mastodon.AnnouncementsVM;
@ -43,7 +40,6 @@ public class FragmentMastodonAnnouncement extends Fragment {
private FragmentPaginationBinding binding; private FragmentPaginationBinding binding;
private AnnouncementsVM announcementsVM; private AnnouncementsVM announcementsVM;
private AnnouncementAdapter announcementAdapter;
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
@ -65,13 +61,6 @@ public class FragmentMastodonAnnouncement extends Fragment {
return root; return root;
} }
@Override
public void onResume() {
super.onResume();
NotificationManager notificationManager = (NotificationManager) requireActivity().getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(Helper.NOTIFICATION_USER_NOTIF);
}
/** /**
* Intialize the view for announcements * Intialize the view for announcements
* *
@ -92,7 +81,7 @@ public class FragmentMastodonAnnouncement extends Fragment {
} }
announcementAdapter = new AnnouncementAdapter(announcements); AnnouncementAdapter announcementAdapter = new AnnouncementAdapter(announcements);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(requireActivity()); LinearLayoutManager mLayoutManager = new LinearLayoutManager(requireActivity());
binding.recyclerView.setLayoutManager(mLayoutManager); binding.recyclerView.setLayoutManager(mLayoutManager);
binding.recyclerView.setAdapter(announcementAdapter); binding.recyclerView.setAdapter(announcementAdapter);