Renew FCM token on app update (#12)

This commit is contained in:
Grishka 2022-04-15 03:50:40 +03:00
parent 4566edcc4e
commit 886ae789f4
1 changed files with 8 additions and 6 deletions

View File

@ -96,10 +96,12 @@ public class PushSubscriptionManager{
public static void tryRegisterFCM(){ public static void tryRegisterFCM(){
deviceToken=getPrefs().getString("deviceToken", null); deviceToken=getPrefs().getString("deviceToken", null);
if(!TextUtils.isEmpty(deviceToken)){ int tokenVersion=getPrefs().getInt("version", 0);
registerAllAccountsForPush(); if(!TextUtils.isEmpty(deviceToken) && tokenVersion==BuildConfig.VERSION_CODE){
registerAllAccountsForPush(false);
return; return;
} }
Log.i(TAG, "tryRegisterFCM: no token found or app was updated. Trying to get push token...");
Intent intent = new Intent("com.google.iid.TOKEN_REQUEST"); Intent intent = new Intent("com.google.iid.TOKEN_REQUEST");
intent.setPackage(GSF_PACKAGE); intent.setPackage(GSF_PACKAGE);
intent.putExtra(EXTRA_APPLICATION_PENDING_INTENT, intent.putExtra(EXTRA_APPLICATION_PENDING_INTENT,
@ -354,9 +356,9 @@ public class PushSubscriptionManager{
return info.toByteArray(); return info.toByteArray();
} }
private static void registerAllAccountsForPush(){ private static void registerAllAccountsForPush(boolean forceReRegister){
for(AccountSession session:AccountSessionManager.getInstance().getLoggedInAccounts()){ for(AccountSession session:AccountSessionManager.getInstance().getLoggedInAccounts()){
if(session.pushSubscription==null) if(session.pushSubscription==null || forceReRegister)
session.getPushSubscriptionManager().registerAccountForPush(); session.getPushSubscriptionManager().registerAccountForPush();
else if(session.needUpdatePushSettings) else if(session.needUpdatePushSettings)
session.getPushSubscriptionManager().updatePushSettings(session.pushSubscription); session.getPushSubscriptionManager().updatePushSettings(session.pushSubscription);
@ -371,9 +373,9 @@ public class PushSubscriptionManager{
deviceToken=intent.getStringExtra("registration_id"); deviceToken=intent.getStringExtra("registration_id");
if(deviceToken.startsWith(KID_VALUE)) if(deviceToken.startsWith(KID_VALUE))
deviceToken=deviceToken.substring(KID_VALUE.length()+1); deviceToken=deviceToken.substring(KID_VALUE.length()+1);
getPrefs().edit().putString("deviceToken", deviceToken).apply(); getPrefs().edit().putString("deviceToken", deviceToken).putInt("version", BuildConfig.VERSION_CODE).apply();
Log.i(TAG, "Successfully registered for FCM"); Log.i(TAG, "Successfully registered for FCM");
registerAllAccountsForPush(); registerAllAccountsForPush(true);
}else{ }else{
Log.e(TAG, "FCM registration intent did not contain registration_id: "+intent); Log.e(TAG, "FCM registration intent did not contain registration_id: "+intent);
Bundle extras=intent.getExtras(); Bundle extras=intent.getExtras();