From 32da65f910207f08f50b57ba59af9474eaad75d8 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 12 Feb 2016 11:39:27 +0100 Subject: client side support for XEP-0357: Push Notifications --- .../conversations/services/InstanceIdService.java | 15 +++++ .../services/PushManagementService.java | 78 ++++++++++++++++++++++ .../services/PushMessageReceiver.java | 20 ++++++ 3 files changed, 113 insertions(+) create mode 100644 src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java create mode 100644 src/playstore/java/eu/siacs/conversations/services/PushManagementService.java create mode 100644 src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java (limited to 'src/playstore/java/eu') diff --git a/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java new file mode 100644 index 00000000..dc080430 --- /dev/null +++ b/src/playstore/java/eu/siacs/conversations/services/InstanceIdService.java @@ -0,0 +1,15 @@ +package eu.siacs.conversations.services; + +import android.content.Intent; + +import com.google.android.gms.iid.InstanceIDListenerService; + +public class InstanceIdService extends InstanceIDListenerService { + + @Override + public void onTokenRefresh() { + Intent intent = new Intent(this, XmppConnectionService.class); + intent.setAction(XmppConnectionService.ACTION_GCM_TOKEN_REFRESH); + startService(intent); + } +} diff --git a/src/playstore/java/eu/siacs/conversations/services/PushManagementService.java b/src/playstore/java/eu/siacs/conversations/services/PushManagementService.java new file mode 100644 index 00000000..d65170ee --- /dev/null +++ b/src/playstore/java/eu/siacs/conversations/services/PushManagementService.java @@ -0,0 +1,78 @@ +package eu.siacs.conversations.services; + +import android.provider.Settings; +import android.util.Log; + +import com.google.android.gms.common.ConnectionResult; +import com.google.android.gms.common.GoogleApiAvailability; +import com.google.android.gms.gcm.GoogleCloudMessaging; +import com.google.android.gms.iid.InstanceID; + +import java.io.IOException; + +import eu.siacs.conversations.Config; +import eu.siacs.conversations.R; +import eu.siacs.conversations.entities.Account; +import eu.siacs.conversations.xmpp.OnIqPacketReceived; +import eu.siacs.conversations.xmpp.jid.InvalidJidException; +import eu.siacs.conversations.xmpp.jid.Jid; +import eu.siacs.conversations.xmpp.stanzas.IqPacket; + +public class PushManagementService { + + private static final String APP_SERVER = "push.conversations.im"; + + protected final XmppConnectionService mXmppConnectionService; + + public PushManagementService(XmppConnectionService service) { + this.mXmppConnectionService = service; + } + + public void registerPushTokenOnServer(final Account account) { + Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": has push support"); + retrieveGcmInstanceToken(new OnGcmInstanceTokenRetrieved() { + @Override + public void onGcmInstanceTokenRetrieved(String token) { + try { + final String deviceId = Settings.Secure.getString(mXmppConnectionService.getContentResolver(), Settings.Secure.ANDROID_ID); + IqPacket packet = mXmppConnectionService.getIqGenerator().pushTokenToAppServer(Jid.fromString(APP_SERVER), token, deviceId); + mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() { + @Override + public void onIqPacketReceived(Account account, IqPacket packet) { + Log.d(Config.LOGTAG, "push to app server result: " + packet.toString()); + } + }); + } catch (InvalidJidException ignored) { + + } + } + }); + } + + private void retrieveGcmInstanceToken(final OnGcmInstanceTokenRetrieved instanceTokenRetrieved) { + new Thread(new Runnable() { + @Override + public void run() { + InstanceID instanceID = InstanceID.getInstance(mXmppConnectionService); + try { + String token = instanceID.getToken(mXmppConnectionService.getString(R.string.gcm_defaultSenderId), GoogleCloudMessaging.INSTANCE_ID_SCOPE, null); + instanceTokenRetrieved.onGcmInstanceTokenRetrieved(token); + } catch (IOException e) { + } + } + }).start(); + + } + + public boolean available() { + return GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mXmppConnectionService) == ConnectionResult.SUCCESS; + } + + public boolean pushAvailable(Account account) { + return account.getXmppConnection().getFeatures().push() && available(); + } + + interface OnGcmInstanceTokenRetrieved { + void onGcmInstanceTokenRetrieved(String token); + } +} diff --git a/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java new file mode 100644 index 00000000..37c95e13 --- /dev/null +++ b/src/playstore/java/eu/siacs/conversations/services/PushMessageReceiver.java @@ -0,0 +1,20 @@ +package eu.siacs.conversations.services; + +import android.content.Intent; +import android.os.Bundle; +import android.util.Log; + +import com.google.android.gms.gcm.GcmListenerService; + +import eu.siacs.conversations.Config; + +public class PushMessageReceiver extends GcmListenerService { + + @Override + public void onMessageReceived(String from, Bundle data) { + Intent intent = new Intent(this, XmppConnectionService.class); + intent.setAction(XmppConnectionService.ACTION_GCM_MESSAGE_RECEIVED); + intent.replaceExtras(data); + startService(intent); + } +} -- cgit v1.2.3