A few code fixes

This commit is contained in:
Mike Schwörer 2020-11-04 10:08:06 +01:00
parent ce641bf7d2
commit f3b5b09ed0
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 11 additions and 13 deletions

View File

@ -10,7 +10,7 @@ import com.blackforestbytes.simplecloudnotifier.SCNApp;
import com.blackforestbytes.simplecloudnotifier.lib.datatypes.Tuple3; import com.blackforestbytes.simplecloudnotifier.lib.datatypes.Tuple3;
import com.blackforestbytes.simplecloudnotifier.lib.string.Str; import com.blackforestbytes.simplecloudnotifier.lib.string.Str;
import com.blackforestbytes.simplecloudnotifier.service.IABService; import com.blackforestbytes.simplecloudnotifier.service.IABService;
import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.installations.FirebaseInstallations;
public class SCNSettings public class SCNSettings
{ {
@ -182,13 +182,13 @@ public class SCNSettings
return base + "index.php?preset_user_id="+user_id+"&preset_user_key="+user_key; return base + "index.php?preset_user_id="+user_id+"&preset_user_key="+user_key;
} }
public void setServerToken(String token, View loader) public void setServerToken(String token, View loader, boolean force)
{ {
if (isConnected()) if (isConnected())
{ {
fcm_token_local = token; fcm_token_local = token;
save(); save();
if (!fcm_token_local.equals(fcm_token_server)) ServerCommunication.updateFCMToken(user_id, user_key, fcm_token_local, loader); if (!fcm_token_local.equals(fcm_token_server) || force) ServerCommunication.updateFCMToken(user_id, user_key, fcm_token_local, loader);
} }
else else
{ {
@ -200,13 +200,12 @@ public class SCNSettings
} }
// called at app start // called at app start
public void work(Activity a) public void work(Activity a, boolean force)
{ {
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(a, instanceIdResult -> FirebaseInstallations.getInstance().getId().addOnSuccessListener(a, newToken ->
{ {
String newToken = instanceIdResult.getToken();
Log.d("FB::GetInstanceId", newToken); Log.d("FB::GetInstanceId", newToken);
SCNSettings.inst().setServerToken(newToken, null); SCNSettings.inst().setServerToken(newToken, null, force);
}).addOnCompleteListener(r -> }).addOnCompleteListener(r ->
{ {
if (isConnected()) ServerCommunication.info(user_id, user_key, null); if (isConnected()) ServerCommunication.info(user_id, user_key, null);
@ -232,16 +231,15 @@ public class SCNSettings
if (promode_server != promode_local) updateProState(loader); if (promode_server != promode_local) updateProState(loader);
if (!Str.equals(fcm_token_local, fcm_token_server)) work(a); if (!Str.equals(fcm_token_local, fcm_token_server)) work(a, false);
} }
else else
{ {
// get token then register // get token then register
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(a, instanceIdResult -> FirebaseInstallations.getInstance().getId().addOnSuccessListener(a, newToken ->
{ {
String newToken = instanceIdResult.getToken();
Log.d("FB::GetInstanceId", newToken); Log.d("FB::GetInstanceId", newToken);
SCNSettings.inst().setServerToken(newToken, loader); // does register in here SCNSettings.inst().setServerToken(newToken, loader, false); // does register in here
}).addOnCompleteListener(r -> }).addOnCompleteListener(r ->
{ {
if (isConnected()) ServerCommunication.info(user_id, user_key, null); // info again for safety if (isConnected()) ServerCommunication.info(user_id, user_key, null); // info again for safety

View File

@ -79,7 +79,7 @@ public class MainActivity extends AppCompatActivity
SCNApp.register(this); SCNApp.register(this);
IABService.startup(this); IABService.startup(this);
SCNSettings.inst().work(this); SCNSettings.inst().work(this, true);
} }
@Override @Override
@ -207,7 +207,7 @@ public class MainActivity extends AppCompatActivity
tabLayout.setupWithViewPager(viewPager); tabLayout.setupWithViewPager(viewPager);
SCNSettings.inst().work(this); SCNSettings.inst().work(this, true);
SCNApp.showToast("Backup imported", Toast.LENGTH_LONG); SCNApp.showToast("Backup imported", Toast.LENGTH_LONG);