feat(notifications/action): remove notfication after action

This commit is contained in:
FineFindus
2023-02-26 21:56:18 +01:00
parent a051e636e6
commit 4187da9168
2 changed files with 15 additions and 12 deletions

View File

@@ -100,6 +100,12 @@ public class PushNotificationReceiver extends BroadcastReceiver{
} }
if(intent.getBooleanExtra("fromNotificationAction", false)) { if(intent.getBooleanExtra("fromNotificationAction", false)) {
String accountID=intent.getStringExtra("accountID"); String accountID=intent.getStringExtra("accountID");
int notificationId=intent.getIntExtra("notificationId", -1);
if ( notificationId >= 0) {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
}
if(intent.hasExtra("notification")){ if(intent.hasExtra("notification")){
org.joinmastodon.android.model.Notification notification=Parcels.unwrap(intent.getParcelableExtra("notification")); org.joinmastodon.android.model.Notification notification=Parcels.unwrap(intent.getParcelableExtra("notification"));
@@ -194,35 +200,33 @@ public class PushNotificationReceiver extends BroadcastReceiver{
builder.setSubText(accountName); builder.setSubText(accountName);
} }
int id = GlobalUserPreferences.keepOnlyLatestNotification ? NOTIFICATION_ID : notificationId++;
if (notification != null) { if (notification != null) {
switch (pn.notificationType) { switch (pn.notificationType) {
case MENTION, STATUS -> { case MENTION, STATUS -> {
if(!notification.status.favourited) builder.addAction(buildNotificationAction(context, id, accountID, notification, context.getString(R.string.sk_notification_action_favorite), NotificationAction.FAVORITE));
builder.addAction(buildNotificationAction(context, accountID, notification, context.getString(R.string.sk_notification_action_favorite), NotificationAction.FAVORITE)); builder.addAction(buildNotificationAction(context, id, accountID, notification, context.getString(R.string.sk_notification_action_bookmark), NotificationAction.BOOKMARK));
if(!notification.status.bookmarked)
builder.addAction(buildNotificationAction(context, accountID, notification, context.getString(R.string.sk_notification_action_bookmark), NotificationAction.BOOKMARK));
if(notification.status.visibility != StatusPrivacy.DIRECT) if(notification.status.visibility != StatusPrivacy.DIRECT)
builder.addAction(buildNotificationAction(context, accountID, notification, context.getString(R.string.sk_notification_action_boost), NotificationAction.BOOST)); builder.addAction(buildNotificationAction(context, id, accountID, notification, context.getString(R.string.sk_notification_action_boost), NotificationAction.BOOST));
} }
case UPDATE -> { case UPDATE -> {
if(notification.status.reblogged) if(notification.status.reblogged)
builder.addAction(buildNotificationAction(context, accountID, notification, context.getString(R.string.sk_notification_action_unboost), NotificationAction.UNBOOST)); builder.addAction(buildNotificationAction(context, id, accountID, notification, context.getString(R.string.sk_notification_action_unboost), NotificationAction.UNBOOST));
} }
} }
} }
nm.notify(accountID, GlobalUserPreferences.keepOnlyLatestNotification ? NOTIFICATION_ID : notificationId++, builder.build()); nm.notify(accountID, id, builder.build());
} }
private Notification.Action buildNotificationAction(Context context, String accountID, org.joinmastodon.android.model.Notification notification, String title, NotificationAction action){ private Notification.Action buildNotificationAction(Context context, int notificationId, String accountID, org.joinmastodon.android.model.Notification notification, String title, NotificationAction action){
Intent notificationIntent=new Intent(context, PushNotificationReceiver.class); Intent notificationIntent=new Intent(context, PushNotificationReceiver.class);
notificationIntent.putExtra("notificationId", notificationId);
notificationIntent.putExtra("fromNotificationAction", true); notificationIntent.putExtra("fromNotificationAction", true);
notificationIntent.putExtra("accountID", accountID); notificationIntent.putExtra("accountID", accountID);
notificationIntent.putExtra("notificationAction", action.ordinal()); notificationIntent.putExtra("notificationAction", action.ordinal());
if(notification!=null){
notificationIntent.putExtra("notification", Parcels.wrap(notification)); notificationIntent.putExtra("notification", Parcels.wrap(notification));
}
PendingIntent actionPendingIntent = PendingIntent actionPendingIntent =
PendingIntent.getBroadcast(context, 1, notificationIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent.getBroadcast(context, 1, notificationIntent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);

View File

@@ -5,5 +5,4 @@ public enum NotificationAction {
BOOST, BOOST,
UNBOOST, UNBOOST,
BOOKMARK, BOOKMARK,
REPLY,
} }