attempt to unregister when receiving push for channel no longer joined

when receiving a FCM push message for a channel the user is no longer in (this can happen when the disable command failed) an attempt will be made to explicitly unregister from the app server (which in turn will then send item-not-found on next push)
This commit is contained in:
Daniel Gultsch 2019-06-26 17:40:05 +02:00
parent 7809af9b57
commit 49224335fc
12 changed files with 80 additions and 25 deletions

View file

@ -19,6 +19,10 @@ public class PushManagementService {
//stub implementation. only affects playstore flavor
}
void unregisterChannel(Account account, String hash) {
//stub implementation. only affects playstore flavor
}
void disablePushOnServer(Conversation conversation) {
//stub implementation. only affects playstore flavor
}

View file

@ -2,7 +2,6 @@ package eu.siacs.conversations.crypto;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Parcelable;
import android.support.annotation.StringRes;
import android.util.Log;
@ -94,7 +93,7 @@ public class PgpEngine {
break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
callback.userInputRequired(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
break;
case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
@ -133,7 +132,7 @@ public class PgpEngine {
callback.success(message);
break;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
callback.userInputRequired(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), message);
break;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(conversation.getAccount(), result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
@ -200,7 +199,7 @@ public class PgpEngine {
callback.success(account);
return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), account);
callback.userInputRequired(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), account);
return;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(account, result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));
@ -249,7 +248,7 @@ public class PgpEngine {
callback.success(signatureBuilder.toString());
return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), status);
callback.userInputRequired(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), status);
return;
case OpenPgpApi.RESULT_CODE_ERROR:
OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR);
@ -276,7 +275,7 @@ public class PgpEngine {
callback.success(contact);
return;
case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
callback.userInputRequried(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), contact);
callback.userInputRequired(result.getParcelableExtra(OpenPgpApi.RESULT_INTENT), contact);
return;
case OpenPgpApi.RESULT_CODE_ERROR:
logError(contact.getAccount(), result.getParcelableExtra(OpenPgpApi.RESULT_ERROR));

View file

@ -443,6 +443,20 @@ public class IqGenerator extends AbstractGenerator {
return packet;
}
public IqPacket unregisterChannelOnAppServer(Jid appServer, String deviceId, String channel) {
final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
packet.setTo(appServer);
final Element command = packet.addChild("command", Namespace.COMMANDS);
command.setAttribute("node", "unregister-push-fcm");
command.setAttribute("action", "execute");
final Data data = new Data();
data.put("channel", channel);
data.put("android-id", deviceId);
data.submit();
command.addChild(data);
return packet;
}
public IqPacket enablePush(final Jid jid, final String node, final String secret) {
IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
Element enable = packet.addChild("enable", Namespace.PUSH);

View file

@ -592,6 +592,7 @@ public class XmppConnectionService extends Service {
toggleForegroundService(true);
}
String pushedAccountHash = null;
String pushedChannelHash = null;
boolean interactive = false;
if (action != null) {
final String uuid = intent.getStringExtra("uuid");
@ -704,6 +705,7 @@ public class XmppConnectionService extends Service {
break;
case ACTION_FCM_MESSAGE_RECEIVED:
pushedAccountHash = intent.getStringExtra("account");
pushedChannelHash = intent.getStringExtra("channel");
Log.d(Config.LOGTAG, "push message arrived in service. account=" + pushedAccountHash);
break;
case Intent.ACTION_SEND:
@ -717,13 +719,18 @@ public class XmppConnectionService extends Service {
synchronized (this) {
WakeLockHelper.acquire(wakeLock);
boolean pingNow = ConnectivityManager.CONNECTIVITY_ACTION.equals(action) || (Config.POST_CONNECTIVITY_CHANGE_PING_INTERVAL > 0 && ACTION_POST_CONNECTIVITY_CHANGE.equals(action));
HashSet<Account> pingCandidates = new HashSet<>();
final HashSet<Account> pingCandidates = new HashSet<>();
final String androidId = PhoneHelper.getAndroidId(this);
for (Account account : accounts) {
final boolean pushWasMeantForThisAccount = CryptoHelper.getAccountFingerprint(account, androidId).equals(pushedAccountHash);
pingNow |= processAccountState(account,
interactive,
"ui".equals(action),
CryptoHelper.getAccountFingerprint(account, PhoneHelper.getAndroidId(this)).equals(pushedAccountHash),
pushWasMeantForThisAccount,
pingCandidates);
if (pushWasMeantForThisAccount && pushedChannelHash != null) {
checkMucStillJoined(account, pushedAccountHash, androidId);
}
}
if (pingNow) {
for (Account account : pingCandidates) {
@ -816,6 +823,20 @@ public class XmppConnectionService extends Service {
return pingNow;
}
private void checkMucStillJoined(final Account account, final String hash, final String androidId) {
for(final Conversation conversation : this.conversations) {
if (conversation.getAccount() == account && conversation.getMode() == Conversational.MODE_MULTI) {
Jid jid = conversation.getJid().asBareJid();
final String currentHash = CryptoHelper.getFingerprint(jid, androidId);
if (currentHash.equals(hash)) {
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": received cloud push notification for MUC "+jid);
return;
}
}
}
mPushManagementService.unregisterChannel(account, hash);
}
public void reinitializeMuclumbusService() {
mChannelDiscoveryService.initializeMuclumbusService();
}
@ -854,7 +875,7 @@ public class XmppConnectionService extends Service {
}
@Override
public void userInputRequried(PendingIntent pi, Message object) {
public void userInputRequired(PendingIntent pi, Message object) {
}
});

View file

@ -83,7 +83,7 @@ public class ConferenceDetailsActivity extends XmppActivity implements OnConvers
}
@Override
public void userInputRequried(PendingIntent pi, Conversation object) {
public void userInputRequired(PendingIntent pi, Conversation object) {
}
};

View file

@ -632,7 +632,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
}
@Override
public void userInputRequried(PendingIntent pi, Message object) {
public void userInputRequired(PendingIntent pi, Message object) {
}
});
@ -666,7 +666,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
}
@Override
public void userInputRequried(PendingIntent pi, Message message) {
public void userInputRequired(PendingIntent pi, Message message) {
hidePrepareFileToast(prepareFileToast);
}
});
@ -688,7 +688,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
new UiCallback<Message>() {
@Override
public void userInputRequried(PendingIntent pi, Message object) {
public void userInputRequired(PendingIntent pi, Message object) {
hidePrepareFileToast(prepareFileToast);
}
@ -1326,7 +1326,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
new UiCallback<Contact>() {
@Override
public void userInputRequried(PendingIntent pi, Contact contact) {
public void userInputRequired(PendingIntent pi, Contact contact) {
startPendingIntent(pi, attachmentChoice);
}
@ -2456,7 +2456,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
new UiCallback<Contact>() {
@Override
public void userInputRequried(PendingIntent pi, Contact contact) {
public void userInputRequired(PendingIntent pi, Contact contact) {
startPendingIntent(pi, REQUEST_ENCRYPT_MESSAGE);
}
@ -2512,7 +2512,7 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
new UiCallback<Message>() {
@Override
public void userInputRequried(PendingIntent pi, Message message) {
public void userInputRequired(PendingIntent pi, Message message) {
startPendingIntent(pi, REQUEST_SEND_MESSAGE);
}

View file

@ -104,7 +104,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
private final UiCallback<Avatar> mAvatarFetchCallback = new UiCallback<Avatar>() {
@Override
public void userInputRequried(final PendingIntent pi, final Avatar avatar) {
public void userInputRequired(final PendingIntent pi, final Avatar avatar) {
finishInitialSetup(avatar);
}
@ -917,7 +917,7 @@ public class EditAccountActivity extends OmemoActivity implements OnAccountUpdat
}
@Override
public void userInputRequried(PendingIntent pi, String object) {
public void userInputRequired(PendingIntent pi, String object) {
mPendingPresenceTemplate.push(template);
try {
startIntentSenderForResult(pi.getIntentSender(), REQUEST_CHANGE_STATUS, null, 0, 0, 0);

View file

@ -174,7 +174,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
}
@Override
public void userInputRequried(PendingIntent pi, Conversation object) {
public void userInputRequired(PendingIntent pi, Conversation object) {
}
};
@ -1085,7 +1085,7 @@ public class StartConversationActivity extends XmppActivity implements XmppConne
}
@Override
public void userInputRequried(PendingIntent pi, Conversation object) {
public void userInputRequired(PendingIntent pi, Conversation object) {
}
});

View file

@ -7,5 +7,5 @@ public interface UiCallback<T> {
void error(int errorCode, T object);
void userInputRequried(PendingIntent pi, T object);
void userInputRequired(PendingIntent pi, T object);
}

View file

@ -137,7 +137,7 @@ public abstract class XmppActivity extends ActionBarActivity {
}
@Override
public void userInputRequried(PendingIntent pi, Conversation object) {
public void userInputRequired(PendingIntent pi, Conversation object) {
}
};
@ -565,7 +565,7 @@ public abstract class XmppActivity extends ActionBarActivity {
xmppConnectionService.getPgpEngine().generateSignature(intent, account, status, new UiCallback<String>() {
@Override
public void userInputRequried(PendingIntent pi, String signature) {
public void userInputRequired(PendingIntent pi, String signature) {
try {
startIntentSenderForResult(pi.getIntentSender(), REQUEST_ANNOUNCE_PGP, null, 0, 0, 0);
} catch (final SendIntentException ignored) {
@ -625,7 +625,7 @@ public abstract class XmppActivity extends ActionBarActivity {
}
@Override
public void userInputRequried(PendingIntent pi, Account object) {
public void userInputRequired(PendingIntent pi, Account object) {
try {
startIntentSenderForResult(pi.getIntentSender(),
REQUEST_CHOOSE_PGP_ID, null, 0, 0, 0);

View file

@ -246,8 +246,12 @@ public final class CryptoHelper {
return prettifyFingerprintCert(bytesToHex(fingerprint));
}
public static String getFingerprint(Jid jid, String androidId) {
return getFingerprint(jid.toEscapedString() + "\00" + androidId);
}
public static String getAccountFingerprint(Account account, String androidId) {
return getFingerprint(account.getJid().asBareJid().toEscapedString() + "\00" + androidId);
return getFingerprint(account.getJid().asBareJid(), androidId);
}
public static String getFingerprint(String value) {

View file

@ -63,6 +63,19 @@ public class PushManagementService {
});
}
public void unregisterChannel(final Account account, final String channel) {
final String androidId = PhoneHelper.getAndroidId(mXmppConnectionService);
final Jid appServer = getAppServer();
final IqPacket packet = mXmppConnectionService.getIqGenerator().unregisterChannelOnAppServer(appServer, androidId, channel);
mXmppConnectionService.sendIqPacket(account, packet, (a, response) -> {
if (response.getType() == IqPacket.TYPE.RESULT) {
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": successfully unregistered channel");
} else if (response.getType() == IqPacket.TYPE.ERROR) {
Log.d(Config.LOGTAG, a.getJid().asBareJid()+": unable to unregister channel with hash "+channel);
}
});
}
void registerPushTokenOnServer(final Conversation conversation) {
Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": room "+conversation.getJid().asBareJid()+" has push support");
retrieveFcmInstanceToken(token -> {