From ddaab49976ca7451fc93500402dd3495229aa53a Mon Sep 17 00:00:00 2001 From: Grishka Date: Wed, 6 Nov 2024 10:25:21 +0300 Subject: [PATCH] Support "new post" push notifications fixes #910 --- .../joinmastodon/android/PushNotificationReceiver.java | 4 +++- .../settings/SettingsNotificationsFragment.java | 9 ++++++--- .../org/joinmastodon/android/model/PushNotification.java | 4 +++- .../org/joinmastodon/android/model/PushSubscription.java | 8 +++++--- mastodon/src/main/res/values/strings.xml | 1 + 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java b/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java index 6764259e..4ca8440b 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java +++ b/mastodon/src/main/java/org/joinmastodon/android/PushNotificationReceiver.java @@ -130,6 +130,7 @@ public class PushNotificationReceiver extends BroadcastReceiver{ Notification.Builder builder; if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){ boolean hasGroup=false; + int version=AccountSessionManager.get(accountID).getRawLocalPreferences().getInt("notificationChannelsVersion", 1); List channelGroups=nm.getNotificationChannelGroups(); for(NotificationChannelGroup group:channelGroups){ if(group.getId().equals(accountID)){ @@ -137,7 +138,7 @@ public class PushNotificationReceiver extends BroadcastReceiver{ break; } } - if(!hasGroup){ + if(!hasGroup || version!=2){ NotificationChannelGroup group=new NotificationChannelGroup(accountID, accountName); nm.createNotificationChannelGroup(group); List channels=Arrays.stream(PushNotification.Type.values()) @@ -150,6 +151,7 @@ public class PushNotificationReceiver extends BroadcastReceiver{ }) .collect(Collectors.toList()); nm.createNotificationChannels(channels); + AccountSessionManager.get(accountID).getRawLocalPreferences().edit().putInt("notificationChannelsVersion", 2).apply(); } builder=new Notification.Builder(context, accountID+"_"+pn.notificationType); }else{ diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsNotificationsFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsNotificationsFragment.java index be0d0837..dbcc45d0 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsNotificationsFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsNotificationsFragment.java @@ -42,7 +42,7 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment{ private TextView bannerText; private Button bannerButton; - private CheckableListItem mentionsItem, boostsItem, favoritesItem, followersItem, pollsItem; + private CheckableListItem mentionsItem, boostsItem, favoritesItem, followersItem, pollsItem, statusesItem; private List> typeItems; private boolean needUpdateNotificationSettings; private boolean notificationsAllowed=true; @@ -62,7 +62,8 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment{ boostsItem=new CheckableListItem<>(R.string.notification_type_reblog, 0, CheckableListItem.Style.CHECKBOX, pushSubscription.alerts.reblog, this::toggleCheckableItem), favoritesItem=new CheckableListItem<>(R.string.notification_type_favorite, 0, CheckableListItem.Style.CHECKBOX, pushSubscription.alerts.favourite, this::toggleCheckableItem), followersItem=new CheckableListItem<>(R.string.notification_type_follow, 0, CheckableListItem.Style.CHECKBOX, pushSubscription.alerts.follow, this::toggleCheckableItem), - pollsItem=new CheckableListItem<>(R.string.notification_type_poll, 0, CheckableListItem.Style.CHECKBOX, pushSubscription.alerts.poll, this::toggleCheckableItem) + pollsItem=new CheckableListItem<>(R.string.notification_type_poll, 0, CheckableListItem.Style.CHECKBOX, pushSubscription.alerts.poll, this::toggleCheckableItem), + statusesItem=new CheckableListItem<>(R.string.notification_type_status, 0, CheckableListItem.Style.CHECKBOX, pushSubscription.alerts.status, this::toggleCheckableItem) )); typeItems=List.of(mentionsItem, boostsItem, favoritesItem, followersItem, pollsItem); @@ -82,13 +83,15 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment{ || boostsItem.checked!=ps.alerts.reblog || favoritesItem.checked!=ps.alerts.favourite || followersItem.checked!=ps.alerts.follow - || pollsItem.checked!=ps.alerts.poll; + || pollsItem.checked!=ps.alerts.poll + || statusesItem.checked!=ps.alerts.status; if(needUpdateNotificationSettings && PushSubscriptionManager.arePushNotificationsAvailable()){ ps.alerts.mention=mentionsItem.checked; ps.alerts.reblog=boostsItem.checked; ps.alerts.favourite=favoritesItem.checked; ps.alerts.follow=followersItem.checked; ps.alerts.poll=pollsItem.checked; + ps.alerts.status=statusesItem.checked; AccountSessionManager.getInstance().getAccount(accountID).getPushSubscriptionManager().updatePushSettings(pushSubscription); } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/PushNotification.java b/mastodon/src/main/java/org/joinmastodon/android/model/PushNotification.java index a39ceff0..c9785ec9 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/PushNotification.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/PushNotification.java @@ -43,7 +43,9 @@ public class PushNotification extends BaseModel{ @SerializedName("follow") FOLLOW(R.string.notification_type_follow), @SerializedName("poll") - POLL(R.string.notification_type_poll); + POLL(R.string.notification_type_poll), + @SerializedName("status") + STATUS(R.string.notification_type_status); @StringRes public final int localizedName; diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/PushSubscription.java b/mastodon/src/main/java/org/joinmastodon/android/model/PushSubscription.java index fd0cc2a1..329890dc 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/PushSubscription.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/PushSubscription.java @@ -41,21 +41,23 @@ public class PushSubscription extends BaseModel implements Cloneable{ public boolean reblog; public boolean mention; public boolean poll; + public boolean status; public static Alerts ofAll(){ Alerts alerts=new Alerts(); - alerts.follow=alerts.favourite=alerts.reblog=alerts.mention=alerts.poll=true; + alerts.follow=alerts.favourite=alerts.reblog=alerts.mention=alerts.poll=alerts.status=true; return alerts; } @Override public String toString(){ return "Alerts{"+ - "follow="+follow+ - ", favourite="+favourite+ + "favourite="+favourite+ + ", follow="+follow+ ", reblog="+reblog+ ", mention="+mention+ ", poll="+poll+ + ", status="+status+ '}'; } diff --git a/mastodon/src/main/res/values/strings.xml b/mastodon/src/main/res/values/strings.xml index 0562a094..b9566ac7 100644 --- a/mastodon/src/main/res/values/strings.xml +++ b/mastodon/src/main/res/values/strings.xml @@ -822,6 +822,7 @@ Manage account Switch to this account Delete account + New posts %1$s and %2$,d other followed you %1$s and %2$,d others followed you