I am using Pushwoosh inside my application to receive push notifications. I am using the latest version of Pushwoosh library 3.1.14.
I have a screen structure like this.
Login Activity -> Main Activity with multiple tabs.
So I am implementing my pushwoosh related logic inside MainActivity. And I want to unregister from push on Logout, and go back to Login Activity.
My code is given below. I have filtered out all other sections unrelated to Pushwoosh. To be frank, this code is exactly similar to the code in Pushwoosh documentation here. The only difference is in the onLogout() method where I try to unregister from pushwoosh and go back to LoginActivity.
TabbarActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Pushwoosh Registration
registerReceivers();
PushManager pushManager = PushManager.getInstance(this);
pushManager.setNotificationFactory(new PushNotificationFactory());
try {
pushManager.onStartup(this);
} catch(Exception e) {}
//Register for push!
pushManager.registerForPushNotifications();
checkMessage(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
checkMessage(intent);
}
@Override
protected void onResume() {
super.onResume();
registerReceivers();
}
@Override
protected void onPause() {
super.onPause();
unregisterReceivers();
}
BroadcastReceiver mBroadcastReceiver = new BaseRegistrationReceiver() {
@Override
public void onRegisterActionReceive(Context context, Intent intent) {
checkMessage(intent);
}
};
private BroadcastReceiver mReceiver = new BasePushMessageReceiver() {
@Override protected void onMessageReceive(Intent intent) {
//JSON_DATA_KEY contains JSON payload of push notification.
}
};
public void registerReceivers() {
IntentFilter intentFilter = new IntentFilter(
getPackageName() + ".action.PUSH_MESSAGE_RECEIVE");
registerReceiver(mReceiver, intentFilter,
getPackageName() +".permission.C2D_MESSAGE", null);
registerReceiver(mBroadcastReceiver, new IntentFilter(
getPackageName() + "." + PushManager.REGISTER_BROAD_CAST_ACTION));
}
public void unregisterReceivers() {
try {
unregisterReceiver(mReceiver);
} catch (Exception e) {
e.printStackTrace();
}
try {
unregisterReceiver(mBroadcastReceiver);
} catch (Exception e) {
e.printStackTrace();
}
}
private void checkMessage(Intent intent) {
if (null != intent) {
if (intent.hasExtra(PushManager.REGISTER_EVENT)) {
uploadPushTokenToServer(PushManager.getPushToken(this));
}
resetIntentValues();
}
}
private void resetIntentValues() {
Intent mainAppIntent = getIntent();
if (mainAppIntent.hasExtra(PushManager.PUSH_RECEIVE_EVENT)) {
mainAppIntent.removeExtra(PushManager.PUSH_RECEIVE_EVENT);
} else if (mainAppIntent.hasExtra(PushManager.REGISTER_EVENT)) {
mainAppIntent.removeExtra(PushManager.REGISTER_EVENT);
} else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_EVENT)) {
mainAppIntent.removeExtra(PushManager.UNREGISTER_EVENT);
} else if (mainAppIntent.hasExtra(PushManager.REGISTER_ERROR_EVENT)) {
mainAppIntent.removeExtra(PushManager.REGISTER_ERROR_EVENT);
} else if (mainAppIntent.hasExtra(PushManager.UNREGISTER_ERROR_EVENT)) {
mainAppIntent.removeExtra(PushManager.UNREGISTER_ERROR_EVENT);
}
setIntent(mainAppIntent);
}
//Finally on logout
private void onLogout() {
//other cleanup
//pushwoosh
PushManager.getInstance(this).unregisterForPushNotifications();
//goback to login activity
}
I am getting pushes from server without any issue. The only problem I face is after I logout and go back to LoginActivity, TabbarActivity remains in memory, which in turns hold on to many other fragments and views. I tried debugging using MAT and this is what it shows up.
Class Name | Ref. Objects | Shallow Heap | Ref. Shallow Heap | Retained Heap
--------------------------------------------------------------------------------------------------------------------------------------------------
com.pushwoosh.internal.request.RequestManager$1 @ 0x12f89ce0 Thread-1737 Thread| 1 | 88 | 360 | 536
'- val$context in.myproject.activities.TabbarActivity @ 0x12d8ac40 | 1 | 360 | 360 | 18,520
--------------------------------------------------------------------------------------------------------------------------------------------------
I have also cross checked the same with LeakCanary tool, that also indicates that Pushwoosh is holding on to my activity.
So my question is, how can I cleanup pushwoosh to avoid my activity getting leaked?