Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Issue #2266 / Defensive code for notifications issues
Browse files Browse the repository at this point in the history
  • Loading branch information
BillCarsonFr committed Dec 10, 2018
1 parent 2a1b21a commit 32540c9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Bugfix:
- Use same "Call Anyway" string from iOS (#2695)
- Improve `/markdown` command (#2673)
- Display thumbnail for encrypted files without a remote thumbnail (#2734)
- Defensive code for notifications issues + check play services as per FCM recommendation (#2266)

Translations:
-
Expand Down
58 changes: 49 additions & 9 deletions vector/src/app/java/im/vector/push/fcm/FcmHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,25 @@
package im.vector.push.fcm;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;

import org.matrix.androidsdk.util.Log;

import im.vector.R;

/**
* This class store the FCM token in SharedPrefs and ensure this token is retrieved.
* It has an alter ego in the fdroid variant.
Expand Down Expand Up @@ -70,16 +77,49 @@ public static void storeFcmToken(@NonNull Context context,
*/
public static void ensureFcmTokenIsRetrieved(final Activity activity) {
if (TextUtils.isEmpty(getFcmToken(activity))) {
try {
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(activity, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
storeFcmToken(activity, instanceIdResult.getToken());
}
});
} catch (Exception e) {
Log.e(LOG_TAG, "## ensureFcmTokenIsRetrieved() : failed " + e.getMessage(), e);


//vfe: according to firebase doc
//'app should always check the device for a compatible Google Play services APK before accessing Google Play services features'
if (checkPlayServices(activity)) {
try {
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(activity, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
storeFcmToken(activity, instanceIdResult.getToken());
}
})
.addOnFailureListener(activity, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(LOG_TAG, "## ensureFcmTokenIsRetrieved() : failed " + e.getMessage(), e);
}
});
} catch (Throwable e) {
Log.e(LOG_TAG, "## ensureFcmTokenIsRetrieved() : failed " + e.getMessage(), e);
}
} else {
Toast.makeText(activity, R.string.no_valid_google_play_services_apk, Toast.LENGTH_SHORT).show();
Log.e(LOG_TAG, "No valid Google Play Services found. Cannot use FCM.");
}
}
}

/**
* Check the device to make sure it has the Google Play Services APK. If
* it doesn't, display a dialog that allows users to download the APK from
* the Google Play Store or enable it in the device's system settings.
*/
private static boolean checkPlayServices(Activity activity) {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(activity);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(activity, resultCode, 9000 /*hey does the magic number*/)
.show();
}
return false;
}
return true;
}
}
24 changes: 21 additions & 3 deletions vector/src/main/java/im/vector/activity/SplashActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import im.vector.VectorApp;
import im.vector.analytics.TrackingEvent;
import im.vector.push.PushManager;
import im.vector.push.fcm.FcmHelper;
import im.vector.receiver.VectorUniversalLinkReceiver;
import im.vector.services.EventStreamService;
import im.vector.ui.themes.ActivityOtherThemes;
Expand Down Expand Up @@ -328,10 +329,27 @@ public void onInitialSyncComplete(String toToken) {
// trigger the push registration if required
PushManager pushManager = Matrix.getInstance(getApplicationContext()).getPushManager();

if (!pushManager.isFcmRegistered()) {
pushManager.checkRegistrations();
if (pushManager.isFcmRegistered()) {

//Issue #2266 It might be possible that the FCMHelper saved token is different
//than the push manager saved token, and that the pushManager is not aware.
//And as per current code the pushMgr saved token is sent at each startup (resume?)
//So anyway, might be a good thing to check that it is synced?
//Very defensive code but, ya know :/
String fcmToken = FcmHelper.getFcmToken(this);
String pushMgrSavedToken = pushManager.getCurrentRegistrationToken();

boolean savedTokenAreDifferent = pushMgrSavedToken == null ? fcmToken != null : !pushMgrSavedToken.equals(fcmToken);
if (savedTokenAreDifferent) {
Log.e(LOG_TAG, "SAVED NOTIFICATION TOKEN NOT IN SYNC");
pushManager.resetFCMRegistration(fcmToken);
} else {
pushManager.forceSessionsRegistration(null);
}


} else {
pushManager.forceSessionsRegistration(null);
pushManager.checkRegistrations();
}

boolean noUpdate;
Expand Down
6 changes: 6 additions & 0 deletions vector/src/main/java/im/vector/push/PushManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,17 @@ public void onNetworkError(Exception e) {
@Override
public void onMatrixError(MatrixError e) {
Log.d(LOG_TAG, "resetFCMRegistration : un-registration failed.");
//we can assume that it may have succeeded anyway
setAndStoreRegistrationState(RegistrationState.FCM_REGISTERED);
resetFCMRegistration(newToken);
}

@Override
public void onUnexpectedError(Exception e) {
Log.d(LOG_TAG, "resetFCMRegistration : un-registration failed.");
//we can assume that it may have succeeded anyway
setAndStoreRegistrationState(RegistrationState.FCM_REGISTERED);
resetFCMRegistration(newToken);
}
});
} else {
Expand Down
1 change: 1 addition & 0 deletions vector/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1091,5 +1091,6 @@
<string name="generic_label_and_value">%1$s: %2$s</string>
<string name="plus_x">+%d</string>
<string name="x_plus">%d+</string>
<string name="no_valid_google_play_services_apk">No valid Google Play Services APK found. Notifications won\'t work properly</string>

</resources>

0 comments on commit 32540c9

Please sign in to comment.