Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDK-2445]: add support to register multiple push primer listeners #368

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.clevertap.android.sdk.pushnotification.CTPushNotificationListener;
import com.clevertap.android.sdk.pushnotification.amp.CTPushAmpListener;
import java.util.ArrayList;
import java.util.List;

public abstract class BaseCallbackManager {

Expand All @@ -28,7 +29,7 @@ public abstract class BaseCallbackManager {

public abstract InAppNotificationListener getInAppNotificationListener();

public abstract PushPermissionResponseListener getPushPermissionResponseListener();
public abstract List<PushPermissionResponseListener> getPushPermissionResponseListenerList();

public abstract CTInboxListener getInboxListener();

Expand Down Expand Up @@ -62,7 +63,9 @@ public abstract void setInAppNotificationButtonListener(

public abstract void setInAppNotificationListener(InAppNotificationListener inAppNotificationListener);

public abstract void setPushPermissionResponseListener(PushPermissionResponseListener pushPermissionResponseListener);
public abstract void unregisterPushPermissionResponseListener(PushPermissionResponseListener pushPermissionResponseListener);

public abstract void registerPushPermissionResponseListener(PushPermissionResponseListener pushPermissionResponseListener);

public abstract void setInboxListener(CTInboxListener inboxListener);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.clevertap.android.sdk.pushnotification.amp.CTPushAmpListener;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

@RestrictTo(Scope.LIBRARY)
public class CallbackManager extends BaseCallbackManager {
Expand All @@ -28,7 +29,7 @@ public class CallbackManager extends BaseCallbackManager {

private InAppNotificationListener inAppNotificationListener;

private PushPermissionResponseListener pushPermissionResponseListener;
private final List<PushPermissionResponseListener> pushPermissionResponseListenerList = new ArrayList<>();

private CTInboxListener inboxListener;

Expand Down Expand Up @@ -134,8 +135,8 @@ public InAppNotificationListener getInAppNotificationListener() {
}

@Override
public PushPermissionResponseListener getPushPermissionResponseListener() {
return pushPermissionResponseListener;
public List<PushPermissionResponseListener> getPushPermissionResponseListenerList() {
return pushPermissionResponseListenerList;
}

@Override
Expand All @@ -144,8 +145,13 @@ public void setInAppNotificationListener(final InAppNotificationListener inAppNo
}

@Override
public void setPushPermissionResponseListener(PushPermissionResponseListener pushPermissionResponseListener) {
this.pushPermissionResponseListener = pushPermissionResponseListener;
public void registerPushPermissionResponseListener(PushPermissionResponseListener pushPermissionResponseListener) {
this.pushPermissionResponseListenerList.add(pushPermissionResponseListener);
}

@Override
public void unregisterPushPermissionResponseListener(PushPermissionResponseListener pushPermissionResponseListener) {
this.pushPermissionResponseListenerList.remove(pushPermissionResponseListener);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -1637,25 +1638,34 @@ public void setInAppNotificationListener(InAppNotificationListener inAppNotifica
}

/**
* Returns the PushPermissionNotificationResponseListener object
* This method unregisters the given instance of the PushPermissionResponseListener if
* previously registered.
* <p>
* Use this method to stop observing the push permission result.
*
* @return An {@link PushPermissionResponseListener} object
* @param pushPermissionResponseListener An {@link PushPermissionResponseListener} object
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public PushPermissionResponseListener getPushPermissionNotificationResponseListener() {
return coreState.getCallbackManager().getPushPermissionResponseListener();
@SuppressWarnings({"unused"})
public void unregisterPushPermissionNotificationResponseListener(PushPermissionResponseListener
darshanclevertap marked this conversation as resolved.
Show resolved Hide resolved
pushPermissionResponseListener) {
coreState.getCallbackManager().
unregisterPushPermissionResponseListener(pushPermissionResponseListener);
}

/**
* This method sets the PushPermissionNotificationResponseListener
* This method registers the PushPermissionNotificationResponseListener.
* <p>
* Call this method only from the onCreate() of the activity/fragment and unregister the
* listener from the onDestroy() method using the
* {@link #unregisterPushPermissionNotificationResponseListener(PushPermissionResponseListener)}
*
* @param pushPermissionResponseListener An {@link PushPermissionResponseListener} object
*/
@SuppressWarnings({"unused"})
public void setPushPermissionNotificationResponseListener(PushPermissionResponseListener
public void registerPushPermissionNotificationResponseListener(PushPermissionResponseListener
pushPermissionResponseListener) {
coreState.getCallbackManager().
setPushPermissionResponseListener(pushPermissionResponseListener);
registerPushPermissionResponseListener(pushPermissionResponseListener);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,6 @@ public void checkPendingInAppNotifications(Activity activity) {

@RequiresApi(api = 33)
public void promptPushPrimer(JSONObject jsonObject){
PushPermissionResponseListener listener = callbackManager.
getPushPermissionResponseListener();
int permissionStatus = ContextCompat.checkSelfPermission(context,
Manifest.permission.POST_NOTIFICATIONS);

Expand All @@ -210,20 +208,15 @@ public void promptPushPrimer(JSONObject jsonObject){
if (!jsonObject.optBoolean(FALLBACK_TO_NOTIFICATION_SETTINGS, false)) {
Logger.v("Notification permission is denied. Please grant notification permission access" +
" in your app's settings to send notifications");
if (listener != null) {
listener.onPushPermissionResponse(false);
}
notifyPushPermissionResult(false);
} else {
showSoftOrHardPrompt(jsonObject);
}
return;
}
showSoftOrHardPrompt(jsonObject);
} else {
//Notification permission is granted
if (listener != null) {
listener.onPushPermissionResponse(true);
}
notifyPushPermissionResult(true);
}
}

Expand Down Expand Up @@ -377,17 +370,20 @@ public void run() {

@Override
public void onPushPermissionAccept() {
final PushPermissionResponseListener listener = callbackManager.getPushPermissionResponseListener();
if (listener != null){
listener.onPushPermissionResponse(true);
}
notifyPushPermissionResult(true);
}

@Override
public void onPushPermissionDeny() {
final PushPermissionResponseListener listener = callbackManager.getPushPermissionResponseListener();
if (listener != null){
listener.onPushPermissionResponse(false);
notifyPushPermissionResult(false);
}

//iterates over the PushPermissionResponseListenerList to notify the result
public void notifyPushPermissionResult(boolean result) {
for (final PushPermissionResponseListener listener: callbackManager.getPushPermissionResponseListenerList()) {
if (listener != null){
listener.onPushPermissionResponse(result);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions sample/src/main/java/com/clevertap/demo/HomeScreenActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ class HomeScreenActivity : AppCompatActivity(), CTInboxListener, DisplayUnitList

inAppNotificationListener = this@HomeScreenActivity

pushPermissionNotificationResponseListener = this@HomeScreenActivity

registerPushPermissionNotificationResponseListener(this@HomeScreenActivity)
}

//With CleverTap Android SDK v3.2.0 you can create additional instances to send data to multiple CleverTap accounts
Expand Down Expand Up @@ -282,4 +281,9 @@ class HomeScreenActivity : AppCompatActivity(), CTInboxListener, DisplayUnitList
override fun onInAppButtonClick(payload: HashMap<String, String>?) {
Log.i(TAG, "onInAppButtonClick() called")
}

override fun onDestroy() {
super.onDestroy()
cleverTapDefaultInstance?.unregisterPushPermissionNotificationResponseListener(this)
}
}