implement FCM push for group chats
This commit is contained in:
parent
e467fe341e
commit
7809af9b57
|
@ -1,6 +1,7 @@
|
||||||
package eu.siacs.conversations.services;
|
package eu.siacs.conversations.services;
|
||||||
|
|
||||||
import eu.siacs.conversations.entities.Account;
|
import eu.siacs.conversations.entities.Account;
|
||||||
|
import eu.siacs.conversations.entities.Conversation;
|
||||||
|
|
||||||
public class PushManagementService {
|
public class PushManagementService {
|
||||||
|
|
||||||
|
@ -10,7 +11,15 @@ public class PushManagementService {
|
||||||
this.mXmppConnectionService = service;
|
this.mXmppConnectionService = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerPushTokenOnServer(Account account) {
|
void registerPushTokenOnServer(Account account) {
|
||||||
|
//stub implementation. only affects playstore flavor
|
||||||
|
}
|
||||||
|
|
||||||
|
void registerPushTokenOnServer(Conversation conversation) {
|
||||||
|
//stub implementation. only affects playstore flavor
|
||||||
|
}
|
||||||
|
|
||||||
|
void disablePushOnServer(Conversation conversation) {
|
||||||
//stub implementation. only affects playstore flavor
|
//stub implementation. only affects playstore flavor
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,6 +54,7 @@ public class Conversation extends AbstractEntity implements Blockable, Comparabl
|
||||||
|
|
||||||
public static final String ATTRIBUTE_MUTED_TILL = "muted_till";
|
public static final String ATTRIBUTE_MUTED_TILL = "muted_till";
|
||||||
public static final String ATTRIBUTE_ALWAYS_NOTIFY = "always_notify";
|
public static final String ATTRIBUTE_ALWAYS_NOTIFY = "always_notify";
|
||||||
|
public static final String ATTRIBUTE_PUSH_NODE = "push_node";
|
||||||
public static final String ATTRIBUTE_LAST_CLEAR_HISTORY = "last_clear_history";
|
public static final String ATTRIBUTE_LAST_CLEAR_HISTORY = "last_clear_history";
|
||||||
static final String ATTRIBUTE_MUC_PASSWORD = "muc_password";
|
static final String ATTRIBUTE_MUC_PASSWORD = "muc_password";
|
||||||
private static final String ATTRIBUTE_NEXT_MESSAGE = "next_message";
|
private static final String ATTRIBUTE_NEXT_MESSAGE = "next_message";
|
||||||
|
|
|
@ -423,14 +423,21 @@ public class IqGenerator extends AbstractGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public IqPacket pushTokenToAppServer(Jid appServer, String token, String deviceId) {
|
public IqPacket pushTokenToAppServer(Jid appServer, String token, String deviceId) {
|
||||||
IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
|
return pushTokenToAppServer(appServer, token, deviceId, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IqPacket pushTokenToAppServer(Jid appServer, String token, String deviceId, Jid muc) {
|
||||||
|
final IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
|
||||||
packet.setTo(appServer);
|
packet.setTo(appServer);
|
||||||
Element command = packet.addChild("command", "http://jabber.org/protocol/commands");
|
final Element command = packet.addChild("command", Namespace.COMMANDS);
|
||||||
command.setAttribute("node", "register-push-fcm");
|
command.setAttribute("node", "register-push-fcm");
|
||||||
command.setAttribute("action", "execute");
|
command.setAttribute("action", "execute");
|
||||||
Data data = new Data();
|
final Data data = new Data();
|
||||||
data.put("token", token);
|
data.put("token", token);
|
||||||
data.put("android-id", deviceId);
|
data.put("android-id", deviceId);
|
||||||
|
if (muc != null) {
|
||||||
|
data.put("muc", muc.toEscapedString());
|
||||||
|
}
|
||||||
data.submit();
|
data.submit();
|
||||||
command.addChild(data);
|
command.addChild(data);
|
||||||
return packet;
|
return packet;
|
||||||
|
@ -454,7 +461,7 @@ public class IqGenerator extends AbstractGenerator {
|
||||||
public IqPacket disablePush(final Jid jid, final String node) {
|
public IqPacket disablePush(final Jid jid, final String node) {
|
||||||
IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
|
IqPacket packet = new IqPacket(IqPacket.TYPE.SET);
|
||||||
Element disable = packet.addChild("disable", Namespace.PUSH);
|
Element disable = packet.addChild("disable", Namespace.PUSH);
|
||||||
disable.setAttribute("jid", jid.toString());
|
disable.setAttribute("jid", jid.toEscapedString());
|
||||||
disable.setAttribute("node", node);
|
disable.setAttribute("node", node);
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2583,31 +2583,35 @@ public class XmppConnectionService extends Service {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enableMucPush(final Conversation conversation) {
|
private void enableDirectMucPush(final Conversation conversation) {
|
||||||
final Account account = conversation.getAccount();
|
final Account account = conversation.getAccount();
|
||||||
final Jid room = conversation.getJid().asBareJid();
|
final Jid room = conversation.getJid().asBareJid();
|
||||||
final IqPacket enable = mIqGenerator.enablePush(conversation.getAccount().getJid(), conversation.getUuid(), null);
|
final IqPacket enable = mIqGenerator.enablePush(conversation.getAccount().getJid(), conversation.getUuid(), null);
|
||||||
enable.setTo(room);
|
enable.setTo(room);
|
||||||
sendIqPacket(account, enable, (a, response) -> {
|
sendIqPacket(account, enable, (a, response) -> {
|
||||||
if (response.getType() == IqPacket.TYPE.RESULT) {
|
if (response.getType() == IqPacket.TYPE.RESULT) {
|
||||||
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": enabled push for muc "+room);
|
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": enabled direct push for muc "+room);
|
||||||
} else if (response.getType() == IqPacket.TYPE.ERROR) {
|
} else if (response.getType() == IqPacket.TYPE.ERROR) {
|
||||||
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": unable to enable push for muc "+room+" "+response.getError());
|
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": unable to enable direct push for muc "+room+" "+response.getError());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void disableMucPush(final Conversation conversation) {
|
private void enableMucPush(final Conversation conversation) {
|
||||||
|
enableDirectMucPush(conversation);
|
||||||
|
mPushManagementService.registerPushTokenOnServer(conversation);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void disableDirectMucPush(final Conversation conversation) {
|
||||||
final Account account = conversation.getAccount();
|
final Account account = conversation.getAccount();
|
||||||
final Jid room = conversation.getJid().asBareJid();
|
final Jid room = conversation.getJid().asBareJid();
|
||||||
final IqPacket disable = mIqGenerator.disablePush(conversation.getAccount().getJid(), conversation.getUuid());
|
final IqPacket disable = mIqGenerator.disablePush(conversation.getAccount().getJid(), conversation.getUuid());
|
||||||
disable.setTo(room);
|
disable.setTo(room);
|
||||||
sendIqPacket(account, disable, (a, response) -> {
|
sendIqPacket(account, disable, (a, response) -> {
|
||||||
if (response.getType() == IqPacket.TYPE.RESULT) {
|
if (response.getType() == IqPacket.TYPE.RESULT) {
|
||||||
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": disabled push for muc "+room);
|
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": disabled direct push for muc "+room);
|
||||||
} else if (response.getType() == IqPacket.TYPE.ERROR) {
|
} else if (response.getType() == IqPacket.TYPE.ERROR) {
|
||||||
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": unable to disable push for muc "+room+" "+response.getError());
|
Log.d(Config.LOGTAG,a.getJid().asBareJid()+": unable to disable direct push for muc "+room+" "+response.getError());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -2792,7 +2796,8 @@ public class XmppConnectionService extends Service {
|
||||||
account.pendingConferenceLeaves.remove(conversation);
|
account.pendingConferenceLeaves.remove(conversation);
|
||||||
if (account.getStatus() == Account.State.ONLINE || now) {
|
if (account.getStatus() == Account.State.ONLINE || now) {
|
||||||
if (conversation.getMucOptions().push()) {
|
if (conversation.getMucOptions().push()) {
|
||||||
disableMucPush(conversation);
|
disableDirectMucPush(conversation);
|
||||||
|
mPushManagementService.disablePushOnServer(conversation);
|
||||||
}
|
}
|
||||||
sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions()));
|
sendPresencePacket(conversation.getAccount(), mPresenceGenerator.leave(conversation.getMucOptions()));
|
||||||
conversation.getMucOptions().setOffline();
|
conversation.getMucOptions().setOffline();
|
||||||
|
@ -4063,6 +4068,7 @@ public class XmppConnectionService extends Service {
|
||||||
for (Account account : getAccounts()) {
|
for (Account account : getAccounts()) {
|
||||||
if (account.isOnlineAndConnected() && mPushManagementService.available(account)) {
|
if (account.isOnlineAndConnected() && mPushManagementService.available(account)) {
|
||||||
mPushManagementService.registerPushTokenOnServer(account);
|
mPushManagementService.registerPushTokenOnServer(account);
|
||||||
|
//TODO renew mucs
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,4 +29,5 @@ public final class Namespace {
|
||||||
public static final String JINGLE_TRANSPORTS_IBB = "urn:xmpp:jingle:transports:ibb:1";
|
public static final String JINGLE_TRANSPORTS_IBB = "urn:xmpp:jingle:transports:ibb:1";
|
||||||
public static final String PING = "urn:xmpp:ping";
|
public static final String PING = "urn:xmpp:ping";
|
||||||
public static final String PUSH = "urn:xmpp:push:0";
|
public static final String PUSH = "urn:xmpp:push:0";
|
||||||
|
public static final String COMMANDS = "http://jabber.org/protocol/commands";
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,16 @@ import android.util.Log;
|
||||||
import com.google.android.gms.common.ConnectionResult;
|
import com.google.android.gms.common.ConnectionResult;
|
||||||
import com.google.android.gms.common.GoogleApiAvailability;
|
import com.google.android.gms.common.GoogleApiAvailability;
|
||||||
import com.google.firebase.iid.FirebaseInstanceId;
|
import com.google.firebase.iid.FirebaseInstanceId;
|
||||||
|
import com.google.firebase.iid.InstanceIdResult;
|
||||||
|
|
||||||
import eu.siacs.conversations.Config;
|
import eu.siacs.conversations.Config;
|
||||||
import eu.siacs.conversations.R;
|
import eu.siacs.conversations.R;
|
||||||
import eu.siacs.conversations.entities.Account;
|
import eu.siacs.conversations.entities.Account;
|
||||||
|
import eu.siacs.conversations.entities.Conversation;
|
||||||
import eu.siacs.conversations.utils.PhoneHelper;
|
import eu.siacs.conversations.utils.PhoneHelper;
|
||||||
import eu.siacs.conversations.xml.Element;
|
import eu.siacs.conversations.xml.Element;
|
||||||
import eu.siacs.conversations.xml.Namespace;
|
import eu.siacs.conversations.xml.Namespace;
|
||||||
|
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
|
||||||
import eu.siacs.conversations.xmpp.XmppConnection;
|
import eu.siacs.conversations.xmpp.XmppConnection;
|
||||||
import eu.siacs.conversations.xmpp.forms.Data;
|
import eu.siacs.conversations.xmpp.forms.Data;
|
||||||
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
|
||||||
|
@ -25,18 +28,24 @@ public class PushManagementService {
|
||||||
this.mXmppConnectionService = service;
|
this.mXmppConnectionService = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Data findResponseData(IqPacket response) {
|
||||||
|
final Element command = response.findChild("command", Namespace.COMMANDS);
|
||||||
|
final Element x = command == null ? null : command.findChild("x", Namespace.DATA);
|
||||||
|
return x == null ? null : Data.parse(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Jid getAppServer() {
|
||||||
|
return Jid.of(mXmppConnectionService.getString(R.string.app_server));
|
||||||
|
}
|
||||||
|
|
||||||
void registerPushTokenOnServer(final Account account) {
|
void registerPushTokenOnServer(final Account account) {
|
||||||
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has push support");
|
Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": has push support");
|
||||||
retrieveFcmInstanceToken(token -> {
|
retrieveFcmInstanceToken(token -> {
|
||||||
final String androidId = PhoneHelper.getAndroidId(mXmppConnectionService);
|
final String androidId = PhoneHelper.getAndroidId(mXmppConnectionService);
|
||||||
final Jid appServer = Jid.of(mXmppConnectionService.getString(R.string.app_server));
|
final IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(getAppServer(), token, androidId);
|
||||||
IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(appServer, token, androidId);
|
mXmppConnectionService.sendIqPacket(account, packet, (a, response) -> {
|
||||||
mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> {
|
final Data data = findResponseData(response);
|
||||||
Element command = p.findChild("command", "http://jabber.org/protocol/commands");
|
if (response.getType() == IqPacket.TYPE.RESULT && data != null) {
|
||||||
if (p.getType() == IqPacket.TYPE.RESULT && command != null) {
|
|
||||||
Element x = command.findChild("x", Namespace.DATA);
|
|
||||||
if (x != null) {
|
|
||||||
Data data = Data.parse(x);
|
|
||||||
try {
|
try {
|
||||||
String node = data.getValue("node");
|
String node = data.getValue("node");
|
||||||
String secret = data.getValue("secret");
|
String secret = data.getValue("secret");
|
||||||
|
@ -47,6 +56,32 @@ public class PushManagementService {
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": invalid response from app server");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void registerPushTokenOnServer(final Conversation conversation) {
|
||||||
|
Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": room "+conversation.getJid().asBareJid()+" has push support");
|
||||||
|
retrieveFcmInstanceToken(token -> {
|
||||||
|
final Jid muc = conversation.getJid().asBareJid();
|
||||||
|
final String androidId = PhoneHelper.getAndroidId(mXmppConnectionService);
|
||||||
|
final IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(getAppServer(), token, androidId, muc);
|
||||||
|
packet.setTo(muc);
|
||||||
|
mXmppConnectionService.sendIqPacket(conversation.getAccount(), packet, (a, response) -> {
|
||||||
|
final Data data = findResponseData(response);
|
||||||
|
if (response.getType() == IqPacket.TYPE.RESULT && data != null) {
|
||||||
|
try {
|
||||||
|
final String node = data.getValue("node");
|
||||||
|
final String secret = data.getValue("secret");
|
||||||
|
final Jid jid = Jid.of(data.getValue("jid"));
|
||||||
|
if (node != null && secret != null) {
|
||||||
|
enablePushOnServer(conversation, jid, node, secret);
|
||||||
|
}
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": invalid response from app server");
|
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": invalid response from app server");
|
||||||
|
@ -55,8 +90,8 @@ public class PushManagementService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void enablePushOnServer(final Account account, final Jid jid, final String node, final String secret) {
|
private void enablePushOnServer(final Account account, final Jid appServer, final String node, final String secret) {
|
||||||
IqPacket enable = mXmppConnectionService.getIqGenerator().enablePush(jid, node, secret);
|
final IqPacket enable = mXmppConnectionService.getIqGenerator().enablePush(appServer, node, secret);
|
||||||
mXmppConnectionService.sendIqPacket(account, enable, (a, p) -> {
|
mXmppConnectionService.sendIqPacket(account, enable, (a, p) -> {
|
||||||
if (p.getType() == IqPacket.TYPE.RESULT) {
|
if (p.getType() == IqPacket.TYPE.RESULT) {
|
||||||
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": successfully enabled push on server");
|
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": successfully enabled push on server");
|
||||||
|
@ -66,14 +101,48 @@ public class PushManagementService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void retrieveFcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) {
|
private void enablePushOnServer(final Conversation conversation, final Jid appServer, final String node, final String secret) {
|
||||||
new Thread(() -> {
|
final Jid muc = conversation.getJid().asBareJid();
|
||||||
try {
|
final IqPacket enable = mXmppConnectionService.getIqGenerator().enablePush(appServer, node, secret);
|
||||||
instanceTokenRetrieved.onGcmInstanceTokenRetrieved(FirebaseInstanceId.getInstance().getToken());
|
enable.setTo(muc);
|
||||||
} catch (Exception e) {
|
mXmppConnectionService.sendIqPacket(conversation.getAccount(), enable, (a, p) -> {
|
||||||
Log.d(Config.LOGTAG, "unable to get push token",e);
|
if (p.getType() == IqPacket.TYPE.RESULT) {
|
||||||
|
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": successfully enabled push on " + muc);
|
||||||
|
if (conversation.setAttribute(Conversation.ATTRIBUTE_ALWAYS_NOTIFY, node)) {
|
||||||
|
mXmppConnectionService.updateConversation(conversation);
|
||||||
}
|
}
|
||||||
}).start();
|
} else if (p.getType() == IqPacket.TYPE.ERROR) {
|
||||||
|
Log.d(Config.LOGTAG, a.getJid().asBareJid() + ": enabling push on " + muc + " failed");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disablePushOnServer(final Conversation conversation) {
|
||||||
|
final Jid muc = conversation.getJid().asBareJid();
|
||||||
|
final String node = conversation.getAttribute(Conversation.ATTRIBUTE_PUSH_NODE);
|
||||||
|
if (node != null) {
|
||||||
|
final IqPacket disable = mXmppConnectionService.getIqGenerator().disablePush(getAppServer(), node);
|
||||||
|
disable.setTo(muc);
|
||||||
|
mXmppConnectionService.sendIqPacket(conversation.getAccount(), disable, (account, response) -> {
|
||||||
|
if (response.getType() == IqPacket.TYPE.ERROR) {
|
||||||
|
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": unable to disable push for room "+muc);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
Log.d(Config.LOGTAG,conversation.getAccount().getJid().asBareJid()+": room "+muc+" has no stored node. unable to disable push");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void retrieveFcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) {
|
||||||
|
FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(task -> {
|
||||||
|
if (!task.isSuccessful()) {
|
||||||
|
Log.d(Config.LOGTAG, "unable to get Firebase instance token", task.getException());
|
||||||
|
}
|
||||||
|
final InstanceIdResult result = task.getResult();
|
||||||
|
if (result != null) {
|
||||||
|
instanceTokenRetrieved.onGcmInstanceTokenRetrieved(result.getToken());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue