From 1accbe1f7124d277d4e2699e86d9b8c07244679a Mon Sep 17 00:00:00 2001 From: sim Date: Sat, 28 Oct 2023 18:52:45 +0200 Subject: [PATCH 1/2] Register all account when enabling UnifiedPush --- .../SettingsNotificationsFragment.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) 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 2b240629b7..9c6d096320 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 @@ -334,13 +334,15 @@ private void onUnifiedPushClick(){ return; } - UnifiedPush.unregisterApp( - getContext(), - accountID - ); - - //re-register to fcm - AccountSessionManager.getInstance().getAccount(accountID).getPushSubscriptionManager().registerAccountForPush(getPushSubscription()); + for (AccountSession accountSession : AccountSessionManager.getInstance().getLoggedInAccounts()) { + UnifiedPush.unregisterApp( + getContext(), + accountSession.getID() + ); + + //re-register to fcm + accountSession.getPushSubscriptionManager().registerAccountForPush(getPushSubscription()); + } unifiedPushItem.toggle(); rebindItem(unifiedPushItem); } @@ -350,12 +352,14 @@ private void showUnifiedPushRegisterDialog(List distributors){ (dialog, which)->{ String userDistrib = distributors.get(which); UnifiedPush.saveDistributor(getContext(), userDistrib); - UnifiedPush.registerApp( - getContext(), - accountID, - new ArrayList<>(), - getContext().getPackageName() - ); + for (AccountSession accountSession : AccountSessionManager.getInstance().getLoggedInAccounts()){ + UnifiedPush.registerApp( + getContext(), + accountSession.getID(), + new ArrayList<>(), + getContext().getPackageName() + ); + } unifiedPushItem.toggle(); rebindItem(unifiedPushItem); }).setOnCancelListener(d->rebindItem(unifiedPushItem)).show(); From 843f182e0a67091f4aff89dee4eb02e57696a4be Mon Sep 17 00:00:00 2001 From: sim Date: Sat, 28 Oct 2023 18:52:58 +0200 Subject: [PATCH 2/2] Register new account to UnifiedPush if enabled --- .../android/api/session/AccountSessionManager.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java b/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java index 56b411a6ef..cd959a85ae 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java +++ b/mastodon/src/main/java/org/joinmastodon/android/api/session/AccountSessionManager.java @@ -1,5 +1,7 @@ package org.joinmastodon.android.api.session; +import static org.unifiedpush.android.connector.UnifiedPush.getDistributor; + import android.app.Activity; import android.app.NotificationManager; import android.content.ComponentName; @@ -34,6 +36,7 @@ import org.joinmastodon.android.model.LegacyFilter; import org.joinmastodon.android.model.Instance; import org.joinmastodon.android.model.Token; +import org.unifiedpush.android.connector.UnifiedPush; import java.io.File; import java.io.FileInputStream; @@ -101,6 +104,7 @@ private AccountSessionManager(){ } public void addAccount(Instance instance, Token token, Account self, Application app, AccountActivationInfo activationInfo){ + Context context = MastodonApp.context; instances.put(instance.uri, instance); AccountSession session=new AccountSession(token, self, app, instance.uri, activationInfo==null, activationInfo); sessions.put(session.getID(), session); @@ -113,7 +117,14 @@ public void addAccount(Instance instance, Token token, Account self, Application MastodonAPIController.runInBackground(()->writeInstanceInfoFile(wrapper, instance.uri)); updateMoreInstanceInfo(instance, instance.uri); - if(PushSubscriptionManager.arePushNotificationsAvailable()){ + if (!UnifiedPush.getDistributor(context).isEmpty()) { + UnifiedPush.registerApp( + context, + session.getID(), + new ArrayList<>(), + context.getPackageName() + ); + } else if(PushSubscriptionManager.arePushNotificationsAvailable()){ session.getPushSubscriptionManager().registerAccountForPush(null); } maybeUpdateShortcuts();