Skip to content

Commit ce38a4c

Browse files
committed
feat(notifications): adds ability to customize which activity will be launched
Provides a way to customize next steps when push notification is received.
1 parent 9b037ac commit ce38a4c

File tree

4 files changed

+127
-11
lines changed

4 files changed

+127
-11
lines changed

AndroidSDK/src/com/leanplum/LeanplumPushReceiver.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
import android.content.BroadcastReceiver;
2525
import android.content.Context;
2626
import android.content.Intent;
27+
import android.content.pm.ApplicationInfo;
28+
import android.content.pm.PackageManager;
29+
import android.os.Bundle;
2730

31+
import com.leanplum.internal.LeanplumManifestHelper;
2832
import com.leanplum.internal.Log;
2933
import com.leanplum.internal.Util;
3034

@@ -43,7 +47,20 @@ public void onReceive(Context context, Intent intent) {
4347
Log.e("Received a null intent.");
4448
return;
4549
}
46-
LeanplumPushService.openNotification(context, intent.getExtras());
50+
// Parse manifest and pull metadata which contains client broadcast receiver class.
51+
String receiver = LeanplumManifestHelper.parseNotificationMetadata();
52+
// If receiver isn't found we will open up notification with default activity
53+
if (receiver == null) {
54+
Log.d("Custom broadcast receiver class not set, using default one.");
55+
LeanplumPushService.openNotification(context, intent);
56+
} else {
57+
Log.d("Custom broadcast receiver class found, using it to handle push notifications.");
58+
// Forward Intent to a client broadcast receiver.
59+
Intent forwardIntent = new Intent();
60+
forwardIntent.setClassName(context, receiver);
61+
forwardIntent.putExtras(intent.getExtras());
62+
context.sendBroadcast(forwardIntent);
63+
}
4764
} catch (Throwable t) {
4865
Util.handleException(t);
4966
}

AndroidSDK/src/com/leanplum/LeanplumPushService.java

Lines changed: 89 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ public class LeanplumPushService {
7171
* Leanplum's built-in Google Cloud Messaging sender ID.
7272
*/
7373
public static final String LEANPLUM_SENDER_ID = "44059457771";
74+
75+
/**
76+
* Action param key contained when Notification Bundle is parsed with {@link
77+
* LeanplumPushService#parseNotificationBundle(Bundle)}.
78+
*/
79+
public static final String LEANPLUM_ACTION_PARAM = "lp_action_param";
80+
/**
81+
* Message title param key contained when Notification Bundle is parsed with {@link
82+
* LeanplumPushService#parseNotificationBundle(Bundle)}.
83+
*/
84+
public static final String LEANPLUM_MESSAGE_PARAM = "lp_message_param";
85+
/**
86+
* Message id param key contained when Notification Bundle is parsed with {@link
87+
* LeanplumPushService#parseNotificationBundle(Bundle)}.
88+
*/
89+
public static final String LEANPLUM_MESSAGE_ID = "lp_message_id";
90+
7491
private static final String LEANPLUM_PUSH_FCM_LISTENER_SERVICE_CLASS =
7592
"com.leanplum.LeanplumPushFcmListenerService";
7693
private static final String PUSH_FIREBASE_MESSAGING_SERVICE_CLASS =
@@ -349,10 +366,11 @@ private static void showNotification(Context context, Bundle message) {
349366
notificationManager.notify(notificationId, builder.build());
350367
}
351368

352-
static void openNotification(Context context, final Bundle notification) {
369+
static void openNotification(Context context, Intent intent) {
353370
Log.d("Opening push notification action.");
371+
// Pre handles push notification.
372+
Bundle notification = preHandlePushNotification(context, intent);
354373
if (notification == null) {
355-
Log.i("Received null Bundle.");
356374
return;
357375
}
358376

@@ -364,24 +382,85 @@ static void openNotification(Context context, final Bundle notification) {
364382
// Start activity.
365383
Class<? extends Activity> callbackClass = LeanplumPushService.getCallbackClass();
366384
boolean shouldStartActivity = true;
367-
if (LeanplumActivityHelper.currentActivity != null &&
368-
!LeanplumActivityHelper.isActivityPaused) {
385+
Activity currentActivity= LeanplumActivityHelper.currentActivity;
386+
if (currentActivity != null && !LeanplumActivityHelper.isActivityPaused) {
369387
if (callbackClass == null) {
370388
shouldStartActivity = false;
371-
} else if (callbackClass.isInstance(LeanplumActivityHelper.currentActivity)) {
389+
} else if (callbackClass.isInstance(currentActivity)) {
372390
shouldStartActivity = false;
373391
}
374392
}
375393

376394
if (shouldStartActivity) {
377395
Intent actionIntent = getActionIntent(context);
378396
actionIntent.putExtras(notification);
379-
actionIntent.addFlags(
380-
Intent.FLAG_ACTIVITY_CLEAR_TOP |
381-
Intent.FLAG_ACTIVITY_NEW_TASK);
397+
actionIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
382398
context.startActivity(actionIntent);
383399
}
400+
// Post handles push notification.
401+
postHandlePushNotification(context, intent);
402+
}
384403

404+
/**
405+
* Parse notification bundle. Use this method to get parsed bundle to decide next step. Parsed
406+
* data will contain {@link LeanplumPushService#LEANPLUM_ACTION_PARAM}, {@link
407+
* LeanplumPushService#LEANPLUM_MESSAGE_PARAM} and {@link LeanplumPushService#LEANPLUM_MESSAGE_ID}
408+
*
409+
* @param notificationBundle Bundle to be parsed.
410+
* @return Map containing Actions, Message title and Message Id.
411+
*/
412+
public static Map<String, Object> parseNotificationBundle(Bundle notificationBundle) {
413+
try {
414+
String notificationActions = notificationBundle.getString(Keys.PUSH_MESSAGE_ACTION);
415+
String notificationMessage = notificationBundle.getString(Keys.PUSH_MESSAGE_TEXT);
416+
String notificationMessageId = LeanplumPushService.getMessageId(notificationBundle);
417+
418+
Map<String, Object> arguments = new HashMap<>();
419+
arguments.put(LEANPLUM_ACTION_PARAM, JsonConverter.fromJson(notificationActions));
420+
arguments.put(LEANPLUM_MESSAGE_PARAM, notificationMessage);
421+
arguments.put(LEANPLUM_MESSAGE_ID, notificationMessageId);
422+
423+
return arguments;
424+
} catch (Throwable ignored) {
425+
Log.i("Failed to parse notification bundle.");
426+
}
427+
return null;
428+
}
429+
430+
/**
431+
* Must be called before deciding which activity will be opened, to allow Leanplum SDK to track
432+
* stats, open events etc.
433+
*
434+
* @param context Surrounding context.
435+
* @param intent Received Intent.
436+
* @return Bundle containing push notification data.
437+
*/
438+
public static Bundle preHandlePushNotification(Context context, Intent intent) {
439+
if (intent == null) {
440+
Log.i("Unable to pre handle push notification, Intent is null.");
441+
return null;
442+
}
443+
Bundle notification = intent.getExtras();
444+
if (notification == null) {
445+
Log.i("Unable to pre handle push notification, extras are null.");
446+
return null;
447+
}
448+
return notification;
449+
}
450+
451+
/**
452+
* Must be called after deciding which activity will be opened, to allow Leanplum SDK to track
453+
* stats, open events etc.
454+
*
455+
* @param context Surrounding context.
456+
* @param intent Received Intent.
457+
*/
458+
public static void postHandlePushNotification(Context context, Intent intent) {
459+
final Bundle notification = intent.getExtras();
460+
if (notification == null) {
461+
Log.i("Could not post handle push notification, extras are null.");
462+
return;
463+
}
385464
// Perform action.
386465
LeanplumActivityHelper.queueActionUponActive(new VariablesChangedCallback() {
387466
@Override
@@ -396,8 +475,8 @@ public void variablesChanged() {
396475
Map<String, Object> args = new HashMap<>();
397476
args.put(actionName, JsonConverter.fromJson(
398477
notification.getString(Keys.PUSH_MESSAGE_ACTION)));
399-
ActionContext context = new ActionContext(
400-
ActionManager.PUSH_NOTIFICATION_ACTION_NAME, args, messageId);
478+
ActionContext context = new ActionContext(ActionManager.PUSH_NOTIFICATION_ACTION_NAME,
479+
args, messageId);
401480
context.preventRealtimeUpdating();
402481
context.update();
403482
context.runTrackedActionNamed(actionName);

AndroidSDK/src/com/leanplum/internal/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,6 @@ public static class Messaging {
259259

260260
public static class ClassUtil {
261261
public static final String UI_INTERFACE_EDITOR = "com.leanplum.uieditor.LeanplumUIEditor";
262+
public static final String LEANPLUM_NOTIFICATION_RECEIVER = "LP_NOTIFICATION_RECEIVER";
262263
}
263264
}

AndroidSDK/src/com/leanplum/internal/LeanplumManifestHelper.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
package com.leanplum.internal;
2323

2424
import android.content.Context;
25+
import android.content.pm.ApplicationInfo;
2526
import android.content.pm.PackageManager;
27+
import android.os.Bundle;
2628
import android.text.TextUtils;
2729

2830
import com.leanplum.Leanplum;
@@ -518,6 +520,23 @@ public static boolean checkPermission(String permission, boolean definesPermissi
518520
return true;
519521
}
520522

523+
/**
524+
* Parses and returns client broadcast receiver class name.
525+
*
526+
* @return Client broadcast receiver class name.
527+
*/
528+
public static String parseNotificationMetadata() {
529+
try {
530+
Context context = Leanplum.getContext();
531+
ApplicationInfo app = context.getPackageManager().getApplicationInfo(context.getPackageName(),
532+
PackageManager.GET_META_DATA);
533+
Bundle bundle = app.metaData;
534+
return bundle.getString(Constants.ClassUtil.LEANPLUM_NOTIFICATION_RECEIVER);
535+
} catch (Throwable ignored) {
536+
}
537+
return null;
538+
}
539+
521540
/**
522541
* Class with Android manifest data.
523542
*/

0 commit comments

Comments
 (0)