|
1 | 1 | package com.iterable.iterableapi; |
2 | 2 |
|
3 | | -import android.app.NotificationManager; |
4 | 3 | import android.content.BroadcastReceiver; |
5 | 4 | import android.content.Context; |
6 | 5 | import android.content.Intent; |
7 | | -import android.os.Bundle; |
8 | | - |
9 | | -import androidx.core.app.RemoteInput; |
10 | | - |
11 | | -import org.json.JSONException; |
12 | | -import org.json.JSONObject; |
13 | 6 |
|
14 | 7 | /** |
15 | 8 | * Handles incoming push actions built by {@link IterableNotificationBuilder} |
16 | 9 | * Action id is passed in the Intent extras under {@link IterableConstants#REQUEST_CODE} |
17 | 10 | */ |
18 | 11 | public class IterablePushActionReceiver extends BroadcastReceiver { |
19 | 12 | private static final String TAG = "IterablePushActionReceiver"; |
20 | | - // Used to hold intents until the SDK is initialized |
21 | | - private static PendingAction pendingAction = null; |
22 | 13 |
|
23 | 14 | @Override |
24 | 15 | public void onReceive(Context context, Intent intent) { |
25 | | - // Dismiss the notification |
26 | | - int requestCode = intent.getIntExtra(IterableConstants.REQUEST_CODE, 0); |
27 | | - NotificationManager mNotificationManager = (NotificationManager) |
28 | | - context.getSystemService(Context.NOTIFICATION_SERVICE); |
29 | | - mNotificationManager.cancel(requestCode); |
30 | | - |
31 | | - // Dismiss the notifications panel |
32 | | - try { |
33 | | - context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); |
34 | | - } catch (SecurityException e) { |
35 | | - IterableLogger.w(TAG, e.getLocalizedMessage()); |
36 | | - } |
37 | | - |
| 16 | + IterablePushNotificationUtil.dismissNotification(context, intent); |
| 17 | + IterablePushNotificationUtil.dismissNotificationPanel(context); |
38 | 18 | String actionName = intent.getAction(); |
39 | 19 | if (IterableConstants.ACTION_PUSH_ACTION.equalsIgnoreCase(actionName)) { |
40 | | - handlePushAction(context, intent); |
41 | | - } |
42 | | - } |
43 | | - |
44 | | - static boolean processPendingAction(Context context) { |
45 | | - boolean handled = false; |
46 | | - if (pendingAction != null) { |
47 | | - handled = executeAction(context, pendingAction); |
48 | | - pendingAction = null; |
| 20 | + IterablePushNotificationUtil.handlePushAction(context, intent); |
49 | 21 | } |
50 | | - return handled; |
51 | 22 | } |
52 | | - |
53 | | - private static void handlePushAction(Context context, Intent intent) { |
54 | | - if (intent.getExtras() == null) { |
55 | | - IterableLogger.e(TAG, "handlePushAction: extras == null, can't handle push action"); |
56 | | - return; |
57 | | - } |
58 | | - IterableNotificationData notificationData = new IterableNotificationData(intent.getExtras()); |
59 | | - String actionIdentifier = intent.getStringExtra(IterableConstants.ITERABLE_DATA_ACTION_IDENTIFIER); |
60 | | - IterableAction action = null; |
61 | | - JSONObject dataFields = new JSONObject(); |
62 | | - |
63 | | - boolean openApp = true; |
64 | | - |
65 | | - if (actionIdentifier != null) { |
66 | | - try { |
67 | | - if (actionIdentifier.equals(IterableConstants.ITERABLE_ACTION_DEFAULT)) { |
68 | | - // Default action (click on a push) |
69 | | - dataFields.put(IterableConstants.ITERABLE_DATA_ACTION_IDENTIFIER, IterableConstants.ITERABLE_ACTION_DEFAULT); |
70 | | - action = notificationData.getDefaultAction(); |
71 | | - if (action == null) { |
72 | | - action = getLegacyDefaultActionFromPayload(intent.getExtras()); |
73 | | - } |
74 | | - } else { |
75 | | - dataFields.put(IterableConstants.ITERABLE_DATA_ACTION_IDENTIFIER, actionIdentifier); |
76 | | - IterableNotificationData.Button button = notificationData.getActionButton(actionIdentifier); |
77 | | - action = button.action; |
78 | | - openApp = button.openApp; |
79 | | - |
80 | | - if (button.buttonType.equals(IterableNotificationData.Button.BUTTON_TYPE_TEXT_INPUT)) { |
81 | | - Bundle results = RemoteInput.getResultsFromIntent(intent); |
82 | | - if (results != null) { |
83 | | - String userInput = results.getString(IterableConstants.USER_INPUT); |
84 | | - if (userInput != null) { |
85 | | - dataFields.putOpt(IterableConstants.KEY_USER_TEXT, userInput); |
86 | | - action.userInput = userInput; |
87 | | - } |
88 | | - } |
89 | | - } |
90 | | - } |
91 | | - } catch (JSONException e) { |
92 | | - IterableLogger.e(TAG, "Encountered an exception while trying to handle the push action", e); |
93 | | - } |
94 | | - } |
95 | | - |
96 | | - pendingAction = new PendingAction(intent, notificationData, action, openApp, dataFields); |
97 | | - |
98 | | - boolean handled = false; |
99 | | - if (IterableApi.getInstance().getMainActivityContext() != null) { |
100 | | - handled = processPendingAction(context); |
101 | | - } |
102 | | - |
103 | | - // Open the launcher activity if the action was not handled by anything, and openApp is true |
104 | | - if (openApp && !handled) { |
105 | | - Intent launcherIntent = IterableNotificationHelper.getMainActivityIntent(context); |
106 | | - launcherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); |
107 | | - if (launcherIntent.resolveActivity(context.getPackageManager()) != null) { |
108 | | - context.startActivity(launcherIntent); |
109 | | - } |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - private static boolean executeAction(Context context, PendingAction action) { |
114 | | - // Automatic tracking |
115 | | - IterableApi.sharedInstance.setPayloadData(action.intent); |
116 | | - IterableApi.sharedInstance.setNotificationData(action.notificationData); |
117 | | - IterableApi.sharedInstance.trackPushOpen(action.notificationData.getCampaignId(), action.notificationData.getTemplateId(), |
118 | | - action.notificationData.getMessageId(), action.dataFields); |
119 | | - |
120 | | - return IterableActionRunner.executeAction(context, action.iterableAction, IterableActionSource.PUSH); |
121 | | - } |
122 | | - |
123 | | - private static IterableAction getLegacyDefaultActionFromPayload(Bundle extras) { |
124 | | - try { |
125 | | - if (extras.containsKey(IterableConstants.ITERABLE_DATA_DEEP_LINK_URL)) { |
126 | | - JSONObject actionJson = new JSONObject(); |
127 | | - actionJson.put("type", IterableAction.ACTION_TYPE_OPEN_URL); |
128 | | - actionJson.put("data", extras.getString(IterableConstants.ITERABLE_DATA_DEEP_LINK_URL)); |
129 | | - return IterableAction.from(actionJson); |
130 | | - } |
131 | | - } catch (Exception e) { |
132 | | - e.printStackTrace(); |
133 | | - } |
134 | | - return null; |
135 | | - } |
136 | | - |
137 | | - private static class PendingAction { |
138 | | - Intent intent; |
139 | | - IterableNotificationData notificationData; |
140 | | - IterableAction iterableAction; |
141 | | - boolean openApp; |
142 | | - JSONObject dataFields; |
143 | | - |
144 | | - PendingAction(Intent intent, IterableNotificationData notificationData, IterableAction iterableAction, boolean openApp, JSONObject dataFields) { |
145 | | - this.intent = intent; |
146 | | - this.notificationData = notificationData; |
147 | | - this.iterableAction = iterableAction; |
148 | | - this.openApp = openApp; |
149 | | - this.dataFields = dataFields; |
150 | | - } |
151 | | - } |
152 | | - |
153 | 23 | } |
0 commit comments