1515 */
1616package com .github .moduth .blockcanary ;
1717
18- import android .annotation .TargetApi ;
18+ import static android .app .PendingIntent .FLAG_UPDATE_CURRENT ;
19+ import static android .os .Build .VERSION .SDK_INT ;
20+ import static android .os .Build .VERSION_CODES .JELLY_BEAN ;
21+
1922import android .app .Notification ;
23+ import android .app .NotificationChannel ;
2024import android .app .NotificationManager ;
2125import android .app .PendingIntent ;
2226import android .content .Context ;
2327import android .content .Intent ;
24- import android .util .Log ;
28+ import android .os .Build ;
29+
30+ import androidx .core .app .NotificationCompat ;
31+ import androidx .core .app .NotificationManagerCompat ;
2532
2633import com .github .moduth .blockcanary .internal .BlockInfo ;
2734import com .github .moduth .blockcanary .ui .DisplayActivity ;
2835
29- import java .lang .reflect .InvocationTargetException ;
30- import java .lang .reflect .Method ;
31-
32- import static android .app .PendingIntent .FLAG_UPDATE_CURRENT ;
33- import static android .os .Build .VERSION .SDK_INT ;
34- import static android .os .Build .VERSION_CODES .HONEYCOMB ;
35- import static android .os .Build .VERSION_CODES .JELLY_BEAN ;
36-
3736final class DisplayService implements BlockInterceptor {
3837
3938 private static final String TAG = "DisplayService" ;
@@ -49,39 +48,29 @@ public void onBlock(Context context, BlockInfo blockInfo) {
4948 show (context , contentTitle , contentText , pendingIntent );
5049 }
5150
52- @ TargetApi (HONEYCOMB )
5351 private void show (Context context , String contentTitle , String contentText , PendingIntent pendingIntent ) {
54- NotificationManager notificationManager = (NotificationManager )
55- context .getSystemService (Context .NOTIFICATION_SERVICE );
52+ NotificationManagerCompat notificationManager = NotificationManagerCompat .from (context );
5653
5754 Notification notification ;
58- if (SDK_INT < HONEYCOMB ) {
59- notification = new Notification ();
60- notification .icon = R .drawable .block_canary_notification ;
61- notification .when = System .currentTimeMillis ();
62- notification .flags |= Notification .FLAG_AUTO_CANCEL ;
63- notification .defaults = Notification .DEFAULT_SOUND ;
64- try {
65- Method deprecatedMethod = notification .getClass ().getMethod ("setLatestEventInfo" , Context .class , CharSequence .class , CharSequence .class , PendingIntent .class );
66- deprecatedMethod .invoke (notification , context , contentTitle , contentText , pendingIntent );
67- } catch (NoSuchMethodException | IllegalAccessException | IllegalArgumentException
68- | InvocationTargetException e ) {
69- Log .w (TAG , "Method not found" , e );
70- }
55+ String packageName = context .getPackageName ();
56+ NotificationCompat .Builder builder = new NotificationCompat .Builder (context , packageName )
57+ .setSmallIcon (R .drawable .block_canary_notification )
58+ .setWhen (System .currentTimeMillis ())
59+ .setContentTitle (contentTitle )
60+ .setContentText (contentText )
61+ .setAutoCancel (true )
62+ .setContentIntent (pendingIntent )
63+ .setDefaults (Notification .DEFAULT_SOUND );
64+ if (SDK_INT >= Build .VERSION_CODES .O ) {
65+ NotificationChannel notificationChannel = new NotificationChannel (packageName , packageName , NotificationManager .IMPORTANCE_LOW );
66+ notificationChannel .setDescription (packageName );
67+ notificationChannel .setShowBadge (false );
68+ notificationManager .createNotificationChannel (notificationChannel );
69+ }
70+ if (SDK_INT < JELLY_BEAN ) {
71+ notification = builder .getNotification ();
7172 } else {
72- Notification .Builder builder = new Notification .Builder (context )
73- .setSmallIcon (R .drawable .block_canary_notification )
74- .setWhen (System .currentTimeMillis ())
75- .setContentTitle (contentTitle )
76- .setContentText (contentText )
77- .setAutoCancel (true )
78- .setContentIntent (pendingIntent )
79- .setDefaults (Notification .DEFAULT_SOUND );
80- if (SDK_INT < JELLY_BEAN ) {
81- notification = builder .getNotification ();
82- } else {
83- notification = builder .build ();
84- }
73+ notification = builder .build ();
8574 }
8675 notificationManager .notify (0xDEAFBEEF , notification );
8776 }
0 commit comments