@@ -843,18 +843,38 @@ public void onResponse(boolean success) {
843843 @ Override
844844 public void run () {
845845 try {
846- NotificationCompat .Builder mBuilder =
847- new NotificationCompat .Builder (currentContext )
848- .setSmallIcon (android .R .drawable .star_on )
849- .setContentTitle ("Leanplum" )
850- .setContentText ("Your device is registered." );
851- mBuilder .setContentIntent (PendingIntent .getActivity (
846+ NotificationCompat .Builder builder = null ;
847+ // If we are targeting API 26, try to post notification to default channel.
848+ if (LeanplumNotificationChannel .isNotificationChannelSupported (context )) {
849+ try {
850+ String channelId = LeanplumNotificationChannel .getDefaultNotificationChannelId (context );
851+ if (!TextUtils .isEmpty (channelId )) {
852+ builder = new NotificationCompat .Builder (context , channelId );
853+ } else {
854+ Log .w ("Failed to post notification, there are no notification channels configured." );
855+ return ;
856+ }
857+ } catch (Exception e ) {
858+ Log .e ("Failed to post notification, there are no notification channels configured." );
859+ }
860+ } else {
861+ builder = new NotificationCompat .Builder (context );
862+ }
863+
864+ if (builder == null ) {
865+ return ;
866+ }
867+
868+ builder .setSmallIcon (android .R .drawable .star_on )
869+ .setContentTitle ("Leanplum" )
870+ .setContentText ("Your device is registered." );
871+ builder .setContentIntent (PendingIntent .getActivity (
852872 currentContext .getApplicationContext (), 0 , new Intent (), 0 ));
853873 NotificationManager mNotificationManager =
854874 (NotificationManager ) currentContext .getSystemService (
855875 Context .NOTIFICATION_SERVICE );
856876 // mId allows you to update the notification later on.
857- mNotificationManager .notify (0 , mBuilder .build ());
877+ mNotificationManager .notify (0 , builder .build ());
858878 } catch (Throwable t ) {
859879 Log .i ("Device is registered." );
860880 }
0 commit comments