Support "new post" push notifications

fixes #910
This commit is contained in:
Grishka 2024-11-06 10:25:21 +03:00
parent 1b17600835
commit ddaab49976
5 changed files with 18 additions and 8 deletions

View File

@ -130,6 +130,7 @@ public class PushNotificationReceiver extends BroadcastReceiver{
Notification.Builder builder; Notification.Builder builder;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){ if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
boolean hasGroup=false; boolean hasGroup=false;
int version=AccountSessionManager.get(accountID).getRawLocalPreferences().getInt("notificationChannelsVersion", 1);
List<NotificationChannelGroup> channelGroups=nm.getNotificationChannelGroups(); List<NotificationChannelGroup> channelGroups=nm.getNotificationChannelGroups();
for(NotificationChannelGroup group:channelGroups){ for(NotificationChannelGroup group:channelGroups){
if(group.getId().equals(accountID)){ if(group.getId().equals(accountID)){
@ -137,7 +138,7 @@ public class PushNotificationReceiver extends BroadcastReceiver{
break; break;
} }
} }
if(!hasGroup){ if(!hasGroup || version!=2){
NotificationChannelGroup group=new NotificationChannelGroup(accountID, accountName); NotificationChannelGroup group=new NotificationChannelGroup(accountID, accountName);
nm.createNotificationChannelGroup(group); nm.createNotificationChannelGroup(group);
List<NotificationChannel> channels=Arrays.stream(PushNotification.Type.values()) List<NotificationChannel> channels=Arrays.stream(PushNotification.Type.values())
@ -150,6 +151,7 @@ public class PushNotificationReceiver extends BroadcastReceiver{
}) })
.collect(Collectors.toList()); .collect(Collectors.toList());
nm.createNotificationChannels(channels); nm.createNotificationChannels(channels);
AccountSessionManager.get(accountID).getRawLocalPreferences().edit().putInt("notificationChannelsVersion", 2).apply();
} }
builder=new Notification.Builder(context, accountID+"_"+pn.notificationType); builder=new Notification.Builder(context, accountID+"_"+pn.notificationType);
}else{ }else{

View File

@ -42,7 +42,7 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment<Void>{
private TextView bannerText; private TextView bannerText;
private Button bannerButton; private Button bannerButton;
private CheckableListItem<Void> mentionsItem, boostsItem, favoritesItem, followersItem, pollsItem; private CheckableListItem<Void> mentionsItem, boostsItem, favoritesItem, followersItem, pollsItem, statusesItem;
private List<CheckableListItem<Void>> typeItems; private List<CheckableListItem<Void>> typeItems;
private boolean needUpdateNotificationSettings; private boolean needUpdateNotificationSettings;
private boolean notificationsAllowed=true; private boolean notificationsAllowed=true;
@ -62,7 +62,8 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment<Void>{
boostsItem=new CheckableListItem<>(R.string.notification_type_reblog, 0, CheckableListItem.Style.CHECKBOX, pushSubscription.alerts.reblog, this::toggleCheckableItem), 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), 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), 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); typeItems=List.of(mentionsItem, boostsItem, favoritesItem, followersItem, pollsItem);
@ -82,13 +83,15 @@ public class SettingsNotificationsFragment extends BaseSettingsFragment<Void>{
|| boostsItem.checked!=ps.alerts.reblog || boostsItem.checked!=ps.alerts.reblog
|| favoritesItem.checked!=ps.alerts.favourite || favoritesItem.checked!=ps.alerts.favourite
|| followersItem.checked!=ps.alerts.follow || followersItem.checked!=ps.alerts.follow
|| pollsItem.checked!=ps.alerts.poll; || pollsItem.checked!=ps.alerts.poll
|| statusesItem.checked!=ps.alerts.status;
if(needUpdateNotificationSettings && PushSubscriptionManager.arePushNotificationsAvailable()){ if(needUpdateNotificationSettings && PushSubscriptionManager.arePushNotificationsAvailable()){
ps.alerts.mention=mentionsItem.checked; ps.alerts.mention=mentionsItem.checked;
ps.alerts.reblog=boostsItem.checked; ps.alerts.reblog=boostsItem.checked;
ps.alerts.favourite=favoritesItem.checked; ps.alerts.favourite=favoritesItem.checked;
ps.alerts.follow=followersItem.checked; ps.alerts.follow=followersItem.checked;
ps.alerts.poll=pollsItem.checked; ps.alerts.poll=pollsItem.checked;
ps.alerts.status=statusesItem.checked;
AccountSessionManager.getInstance().getAccount(accountID).getPushSubscriptionManager().updatePushSettings(pushSubscription); AccountSessionManager.getInstance().getAccount(accountID).getPushSubscriptionManager().updatePushSettings(pushSubscription);
} }
} }

View File

@ -43,7 +43,9 @@ public class PushNotification extends BaseModel{
@SerializedName("follow") @SerializedName("follow")
FOLLOW(R.string.notification_type_follow), FOLLOW(R.string.notification_type_follow),
@SerializedName("poll") @SerializedName("poll")
POLL(R.string.notification_type_poll); POLL(R.string.notification_type_poll),
@SerializedName("status")
STATUS(R.string.notification_type_status);
@StringRes @StringRes
public final int localizedName; public final int localizedName;

View File

@ -41,21 +41,23 @@ public class PushSubscription extends BaseModel implements Cloneable{
public boolean reblog; public boolean reblog;
public boolean mention; public boolean mention;
public boolean poll; public boolean poll;
public boolean status;
public static Alerts ofAll(){ public static Alerts ofAll(){
Alerts alerts=new Alerts(); 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; return alerts;
} }
@Override @Override
public String toString(){ public String toString(){
return "Alerts{"+ return "Alerts{"+
"follow="+follow+ "favourite="+favourite+
", favourite="+favourite+ ", follow="+follow+
", reblog="+reblog+ ", reblog="+reblog+
", mention="+mention+ ", mention="+mention+
", poll="+poll+ ", poll="+poll+
", status="+status+
'}'; '}';
} }

View File

@ -822,6 +822,7 @@
<string name="manage_account">Manage account</string> <string name="manage_account">Manage account</string>
<string name="switch_to_this_account">Switch to this account</string> <string name="switch_to_this_account">Switch to this account</string>
<string name="delete_account">Delete account</string> <string name="delete_account">Delete account</string>
<string name="notification_type_status">New posts</string>
<plurals name="user_and_x_more_followed"> <plurals name="user_and_x_more_followed">
<item quantity="one">%1$s and %2$,d other followed you</item> <item quantity="one">%1$s and %2$,d other followed you</item>
<item quantity="other">%1$s and %2$,d others followed you</item> <item quantity="other">%1$s and %2$,d others followed you</item>