Skip to content

Commit

Permalink
Conditional routing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Ayyanchira committed Jan 10, 2022
1 parent ce5e587 commit 4ec13b5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.iterable.iterableapi;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
Expand All @@ -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";

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 4ec13b5

Please sign in to comment.