From 4ec13b53072b8b77c845ff9da13c4749905d5845 Mon Sep 17 00:00:00 2001 From: Akshay Ayyanchira Date: Thu, 9 Dec 2021 09:26:01 -0800 Subject: [PATCH] Conditional routing PushActionReceiver also uses the util method that trampoline activity uses to process the notification data it gets. It will be invoked and sent data only for those actionButtons with customAction where SDK is sure that openApp is false. In this case, Trampoline Activity will not be launched and the notification intent will be passed to IterablePushActionReceiver, to complete the background customActionHandling. Immutable flag removed from actionButton pending intent as test were failing and it seems like it needs mutable pending intents there. Noticeable change due to this: Customers could earlier add button with CustomAction with openApp -> False. It just prevented the SDK to open the homescreen. But their customActionHandler could always open the activity. Now this wont be allowed as the route it follows will be through a Broadcast Receiver and not through the trampoline Activity --- .../IterableNotificationBuilder.java | 33 +++++++++++++------ .../IterablePushActionReceiver.java | 7 +++- .../IterablePushNotificationUtil.java | 20 ++++++++++- .../IterableTrampolineActivity.java | 22 ++----------- 4 files changed, 51 insertions(+), 31 deletions(-) diff --git a/iterableapi/src/main/java/com/iterable/iterableapi/IterableNotificationBuilder.java b/iterableapi/src/main/java/com/iterable/iterableapi/IterableNotificationBuilder.java index f9cde9322..de7843897 100644 --- a/iterableapi/src/main/java/com/iterable/iterableapi/IterableNotificationBuilder.java +++ b/iterableapi/src/main/java/com/iterable/iterableapi/IterableNotificationBuilder.java @@ -111,24 +111,37 @@ public Notification build() { * @param extras Notification payload */ public void createNotificationActionButton(Context context, IterableNotificationData.Button button, Bundle extras) { + PendingIntent pendingButtonIntent = getPendingIntent(context, button, extras); + NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action + .Builder(NotificationCompat.BADGE_ICON_NONE, button.title, pendingButtonIntent); + if (button.buttonType.equals(IterableNotificationData.Button.BUTTON_TYPE_TEXT_INPUT)) { + actionBuilder.addRemoteInput(new RemoteInput.Builder(IterableConstants.USER_INPUT).setLabel(button.inputPlaceholder).build()); + } + addAction(actionBuilder.build()); + } + + private PendingIntent getPendingIntent(Context context, IterableNotificationData.Button button, Bundle extras) { + PendingIntent pendingButtonIntent; + Intent buttonIntent = new Intent(IterableConstants.ACTION_PUSH_ACTION); - buttonIntent.setClass(context, IterableTrampolineActivity.class); buttonIntent.putExtras(extras); buttonIntent.putExtra(IterableConstants.REQUEST_CODE, requestCode); buttonIntent.putExtra(IterableConstants.ITERABLE_DATA_ACTION_IDENTIFIER, button.identifier); buttonIntent.putExtra(IterableConstants.ACTION_IDENTIFIER, button.identifier); - PendingIntent pendingButtonIntent = PendingIntent.getActivity(context, buttonIntent.hashCode(), - buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE); - - NotificationCompat.Action.Builder actionBuilder = new NotificationCompat.Action - .Builder(NotificationCompat.BADGE_ICON_NONE, button.title, pendingButtonIntent); - - if (button.buttonType.equals(IterableNotificationData.Button.BUTTON_TYPE_TEXT_INPUT)) { - actionBuilder.addRemoteInput(new RemoteInput.Builder(IterableConstants.USER_INPUT).setLabel(button.inputPlaceholder).build()); + if (button.openApp) { + IterableLogger.d(TAG, "Go through TrampolineActivity"); + buttonIntent.setClass(context, IterableTrampolineActivity.class); + pendingButtonIntent = PendingIntent.getActivity(context, buttonIntent.hashCode(), + buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT); + } else { + IterableLogger.d(TAG, "Go through IterablePushActionReceiver"); + buttonIntent.setClass(context, IterablePushActionReceiver.class); + pendingButtonIntent = PendingIntent.getBroadcast(context, buttonIntent.hashCode(), + buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT); } - addAction(actionBuilder.build()); + return pendingButtonIntent; } /** diff --git a/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushActionReceiver.java b/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushActionReceiver.java index 543637bdc..03c964d78 100644 --- a/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushActionReceiver.java +++ b/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushActionReceiver.java @@ -13,6 +13,11 @@ public class IterablePushActionReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - + IterablePushNotificationUtil.dismissNotification(context, intent); + IterablePushNotificationUtil.dismissNotificationPanel(context); + String actionName = intent.getAction(); + if (IterableConstants.ACTION_PUSH_ACTION.equalsIgnoreCase(actionName)) { + IterablePushNotificationUtil.handlePushAction(context, intent); + } } } diff --git a/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushNotificationUtil.java b/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushNotificationUtil.java index 4190e4a90..9499db682 100644 --- a/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushNotificationUtil.java +++ b/iterableapi/src/main/java/com/iterable/iterableapi/IterablePushNotificationUtil.java @@ -1,5 +1,6 @@ package com.iterable.iterableapi; +import android.app.NotificationManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; @@ -9,7 +10,7 @@ import org.json.JSONException; import org.json.JSONObject; -public class IterablePushNotificationUtil { +class IterablePushNotificationUtil { private static PendingAction pendingAction = null; private static final String TAG = "IterablePushNotificationUtil"; @@ -121,4 +122,21 @@ private static class PendingAction { this.dataFields = dataFields; } } + + static void dismissNotificationPanel(Context context) { + // Dismiss the notifications panel + try { + context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); + } catch (SecurityException e) { + IterableLogger.w(TAG, e.getLocalizedMessage()); + } + } + + static void dismissNotification(Context context, Intent notificationIntent) { + // Dismiss the notification + int requestCode = notificationIntent.getIntExtra(IterableConstants.REQUEST_CODE, 0); + NotificationManager mNotificationManager = (NotificationManager) + context.getSystemService(Context.NOTIFICATION_SERVICE); + mNotificationManager.cancel(requestCode); + } } diff --git a/iterableapi/src/main/java/com/iterable/iterableapi/IterableTrampolineActivity.java b/iterableapi/src/main/java/com/iterable/iterableapi/IterableTrampolineActivity.java index 2c07ef29c..9e0e43781 100644 --- a/iterableapi/src/main/java/com/iterable/iterableapi/IterableTrampolineActivity.java +++ b/iterableapi/src/main/java/com/iterable/iterableapi/IterableTrampolineActivity.java @@ -1,11 +1,7 @@ package com.iterable.iterableapi; -import static com.iterable.iterableapi.IterablePushNotificationUtil.handlePushAction; - import androidx.appcompat.app.AppCompatActivity; -import android.app.NotificationManager; -import android.content.Context; import android.content.Intent; import android.os.Bundle; @@ -38,23 +34,11 @@ protected void onResume() { return; } - // Dismiss the notification - int requestCode = notificationIntent.getIntExtra(IterableConstants.REQUEST_CODE, 0); - NotificationManager mNotificationManager = (NotificationManager) - this.getSystemService(Context.NOTIFICATION_SERVICE); - mNotificationManager.cancel(requestCode); - - // Dismiss the notifications panel - try { - this.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); - } catch (SecurityException e) { - IterableLogger.w(TAG, e.getLocalizedMessage()); - } - + IterablePushNotificationUtil.dismissNotification(this, notificationIntent); + IterablePushNotificationUtil.dismissNotificationPanel(this); if (IterableConstants.ACTION_PUSH_ACTION.equalsIgnoreCase(actionName)) { - handlePushAction(this, notificationIntent); + IterablePushNotificationUtil.handlePushAction(this, notificationIntent); } - finish(); }