2222package com .leanplum ;
2323
2424import android .app .Activity ;
25- import android .app .NotificationManager ;
26- import android .app .PendingIntent ;
2725import android .content .Context ;
28- import android .content .Intent ;
2926import android .location .Location ;
3027import android .os .AsyncTask ;
31- import android .support .v4 .app .NotificationCompat ;
3228import android .text .TextUtils ;
3329
3430import com .leanplum .ActionContext .ContextualValues ;
@@ -81,6 +77,7 @@ public class Leanplum {
8177 * Default event name to use for Purchase events.
8278 */
8379 public static final String PURCHASE_EVENT_NAME = "Purchase" ;
80+ public static final String LEANPLUM_PUSH_SERVICE = "com.leanplum.LeanplumPushService" ;
8481
8582 private static final ArrayList <StartCallback > startHandlers = new ArrayList <>();
8683 private static final ArrayList <VariablesChangedCallback > variablesChangedHandlers =
@@ -90,6 +87,8 @@ public class Leanplum {
9087 private static final ArrayList <VariablesChangedCallback > onceNoDownloadsHandlers =
9188 new ArrayList <>();
9289 private static final Object heartbeatLock = new Object ();
90+ private static final String LEANPLUM_NOTIFICATION_CHANNEL =
91+ "com.leanplum.LeanplumNotificationChannel" ;
9392 private static RegisterDeviceCallback registerDeviceHandler ;
9493 private static RegisterDeviceFinishedCallback registerDeviceFinishedHandler ;
9594 private static LeanplumDeviceIdMode deviceIdMode = LeanplumDeviceIdMode .MD5_MAC_ADDRESS ;
@@ -577,11 +576,25 @@ protected Void doInBackground(Void... params) {
577576 }
578577 }
579578
579+ /**
580+ * Checks for leanplum notifications modules and if someone present - invoke onStart method.
581+ */
582+ private static void checkAndStartNotificationsModules () {
583+ if (Util .hasPlayServices ()) {
584+ try {
585+ Class .forName (LEANPLUM_PUSH_SERVICE ).getDeclaredMethod ("onStart" )
586+ .invoke (null );
587+ } catch (Throwable ignored ) {
588+ }
589+ } else {
590+ Log .i ("No valid Google Play Services APK found." );
591+ }
592+ }
593+
580594 private static void startHelper (
581595 String userId , final Map <String , ?> attributes , final boolean isBackground ) {
582596 LeanplumEventDataManager .init (context );
583- LeanplumPushService .onStart ();
584-
597+ checkAndStartNotificationsModules ();
585598 Boolean limitAdTracking = null ;
586599 String deviceId = Request .deviceId ();
587600 if (deviceId == null ) {
@@ -759,14 +772,13 @@ protected Void doInBackground(Void... params) {
759772 Constants .Keys .NOTIFICATION_GROUPS );
760773 String defaultNotificationChannel = response .optString (
761774 Constants .Keys .DEFAULT_NOTIFICATION_CHANNEL );
762-
763- // Configure notification channels and groups
764- LeanplumNotificationChannel .configureNotificationGroups (
765- context , notificationGroups );
766- LeanplumNotificationChannel .configureNotificationChannels (
767- context , notificationChannels );
768- LeanplumNotificationChannel .configureDefaultNotificationChannel (
769- context , defaultNotificationChannel );
775+ try {
776+ Class .forName (LEANPLUM_NOTIFICATION_CHANNEL )
777+ .getDeclaredMethod ("configureChannels" , Context .class , JSONArray .class ,
778+ JSONArray .class , String .class ).invoke (new Object (), context ,
779+ notificationGroups , notificationChannels , defaultNotificationChannel );
780+ } catch (Throwable ignored ) {
781+ }
770782 }
771783
772784 String token = response .optString (Constants .Keys .TOKEN , null );
@@ -834,24 +846,10 @@ public void onResponse(boolean success) {
834846 @ Override
835847 public void run () {
836848 try {
837- NotificationCompat .Builder builder =
838- LeanplumNotificationHelper .getDefaultCompatNotificationBuilder (context ,
839- BuildUtil .isNotificationChannelSupported (context ));
840- if (builder == null ) {
841- return ;
842- }
843- builder .setSmallIcon (android .R .drawable .star_on )
844- .setContentTitle ("Leanplum" )
845- .setContentText ("Your device is registered." );
846- builder .setContentIntent (PendingIntent .getActivity (
847- currentContext .getApplicationContext (), 0 , new Intent (), 0 ));
848- NotificationManager mNotificationManager =
849- (NotificationManager ) currentContext .getSystemService (
850- Context .NOTIFICATION_SERVICE );
851- // mId allows you to update the notification later on.
852- mNotificationManager .notify (0 , builder .build ());
853- } catch (Throwable t ) {
854- Log .i ("Device is registered." );
849+ Class .forName (Leanplum .LEANPLUM_PUSH_SERVICE )
850+ .getDeclaredMethod ("showDeviceRegistedPush" , Context .class ,
851+ Context .class ).invoke (new Object (), context , currentContext );
852+ } catch (Throwable ignored ) {
855853 }
856854 }
857855 });
0 commit comments