@@ -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 );
0 commit comments