Skip to content

Instantly share code, notes, and snippets.

@holysheep
Created September 20, 2016 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holysheep/b37e9faf2addee0a775e1774f62c6a33 to your computer and use it in GitHub Desktop.
Save holysheep/b37e9faf2addee0a775e1774f62c6a33 to your computer and use it in GitHub Desktop.
public class DialogGcmListenerService extends GcmListenerService {
private static final String TAG = "DialogGcmListenerService";
@Override
public void onMessageReceived(String from, Bundle data) {
Log.d(TAG, "Message received #" + data.toString());
Log.d(TAG, "from #" + from);
if (!data.isEmpty()) {
System.out.println(data + "=== data");
ActorSDK.sharedActor().waitForReady();
// if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
if (data.containsKey("seq")) {
int seq = Integer.parseInt(data.getString("seq"));
Log.d(TAG, "Push received #" + seq);
ActorSDK.sharedActor().getMessenger().onPushReceived(seq);
} else if (data.containsKey("callId")) {
long callId = Long.parseLong(data.getString("callId"));
int attempt = 0;
if (data.containsKey("attemptIndex")) {
attempt = Integer.parseInt(data.getString("attemptIndex"));
}
Log.d(TAG, "Received Call #" + callId + " (" + attempt + ")");
ActorSDK.sharedActor().getMessenger().checkCall(callId, attempt);
}
}
super.onMessageReceived(from, data);
}
}
public class DialogInstanceIDListenerService extends InstanceIDListenerService {
private static final String TAG = "InstanceIDLS";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised.
*/
@Override
public void onTokenRefresh() {
Log.d(TAG, "On token refresh");
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
public class PushReceiver extends WakefulBroadcastReceiver {
private static final String TAG = "ActorPushReceiver";
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("ON received PUSHRECEIVER ===");
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
Bundle extras = intent.getExtras();
String messageType = gcm.getMessageType(intent);
System.out.println("03===");
if (!extras.isEmpty()) {
ActorSDK.sharedActor().waitForReady();
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
if (extras.containsKey("seq")) {
int seq = Integer.parseInt(extras.getString("seq"));
Log.d(TAG, "Push received #" + seq);
ActorSDK.sharedActor().getMessenger().onPushReceived(seq);
setResultCode(Activity.RESULT_OK);
} else if (extras.containsKey("callId")) {
long callId = Long.parseLong(extras.getString("callId"));
int attempt = 0;
if (extras.containsKey("attemptIndex")) {
attempt = Integer.parseInt(extras.getString("attemptIndex"));
}
Log.d(TAG, "Received Call #" + callId + " (" + attempt + ")");
ActorSDK.sharedActor().getMessenger().checkCall(callId, attempt);
setResultCode(Activity.RESULT_OK);
}
}
}
WakefulBroadcastReceiver.completeWakefulIntent(intent);
}
}
public class RegistrationIntentService extends IntentService {
private static final String TAG = "RegIntentService";
public RegistrationIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
final ActorPushManager pushManager;
Log.d(TAG, "On handle Intent ===");
try {
pushManager = (ActorPushManager) Class.forName("im.actor.push.PushManager").newInstance();
pushManager.registerPush(getApplicationContext());
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
PushReceiver.completeWakefulIntent(intent);
}
}
public class PushManager implements ActorPushManager {
private static final String TAG = "im.actor.push.PushManager";
private boolean isRegistered = false;
@Override
public void registerPush(final Context context) {
System.out.println("Register push process ===");
if (!isRegistered) {
Log.d(TAG, "Requesting push token...===");
new Thread() {
@Override
public void run() {
ExponentialBackoff exponentialBackoff = new ExponentialBackoff();
while (true) {
try {
String regId = tryRegisterPush(context);
if (regId != null) {
Log.d(TAG, "Token loaded");
onPushRegistered(regId);
return;
} else {
Log.d(TAG, "Unable to load Token");
exponentialBackoff.onFailure();
}
} catch (Exception e) {
e.printStackTrace();
exponentialBackoff.onFailure();
}
long waitTime = exponentialBackoff.exponentialWait();
Log.d(TAG, "Next attempt in " + waitTime + " ms");
try {
Thread.sleep(waitTime);
} catch (InterruptedException e1) {
e1.printStackTrace();
return;
}
}
}
}.start();
} else {
Log.d(TAG, "Already registered token");
}
}
private void onPushRegistered(String token) {
System.out.println("Registered push ===");
isRegistered = true;
ActorSDK.sharedActor().getMessenger().getPreferences().putBool("push_registered", true);
ActorSDK.sharedActor().getMessenger().registerGooglePush(ActorSDK.sharedActor().getPushId(), token);
}
private static String tryRegisterPush(Context context) {
InstanceID instanceID = InstanceID.getInstance(context);
Log.d(TAG, "Requesting push token iteration...");
try {
return instanceID.getToken("" + ActorSDK.sharedActor().getPushId(),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
<service
android:name="im.actor.push.DialogInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID" />
</intent-filter>
</service>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment