# NotificationTest **Repository Path**: threekiloton/NotificationTest ## Basic Information - **Project Name**: NotificationTest - **Description**: android 通知测试 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2018-09-29 - **Last Updated**: 2025-05-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Notification ## NotificationChannel #### 创建channel适配android o ``` private static final String CHANNEL = "MainChannel"; private static final String CHANNEL_NAME = "TestChannel"; ``` ``` private String getChannel ( ) { if( VERSION.SDK_INT > VERSION_CODES.O ) { int importance = NotificationManager.IMPORTANCE_DEFAULT; /* 必须设置 importance */ NotificationChannel channel = new NotificationChannel( CHANNEL, CHANNEL_NAME, importance ); channel.setDescription( "this is test notification channel" ); NotificationManager notificationManager = getSystemService( NotificationManager.class ); assert notificationManager != null; notificationManager.createNotificationChannel( channel ); return CHANNEL; } else { return CHANNEL; } ``` #### 设置channel Importance ``` int importance = NotificationManager.IMPORTANCE_DEFAULT; /* 必须设置 importance */ NotificationChannel channel = new NotificationChannel(CHANNEL, CHANNEL_NAME, importance ); ``` * NotificationManager.IMPORTANCE_NONE * 哪里都不显示 ![](img/pic00.gif) * NotificationManager.IMPORTANCE_MIN * 只在下拉菜单显示 ![](img/pic01.gif) * NotificationManager.IMPORTANCE_LOW * 同时在状态栏下拉菜单显示,没有声音 ![](img/pic02.gif) * NotificationManager.IMPORTANCE_DEFAULT * 同时在状态栏下拉菜单显示,有声音 ![](img/pic04.gif) * NotificationManager.IMPORTANCE_HIGH * 同时在状态栏下拉菜单显示,有声音,同时在界面弹出 ![](img/pic03.gif) ## 创建普通通知 ``` // 创建频道 String channel = getChannel(); // 创建通知 Notification notification = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a basic notification " ) .setPriority( NotificationCompat.PRIORITY_DEFAULT ) .build(); // 发出通知 NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE ); assert notificationManager != null; notificationManager.notify( 0, notification ); ``` ![](img/pic04.gif) ## 点击通知跳转Activity ``` // 创建PendingIntent Intent intent = new Intent( this, DetailActivity.class ); intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP ); PendingIntent pendingIntent = PendingIntent.getActivity( this, 0, intent, 0 ); // 创建通知 String channel = getChannel(); Notification notification = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a content notification " ) .setAutoCancel( true ) .setContentIntent( pendingIntent ) .build(); // 发出通知 NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE ); assert notificationManager != null; notificationManager.notify( 11, notification ); ``` ![](img/pic05.gif) ## 构建有导航的跳转Activity activity声明父activity ``` ``` 构建返回栈 ``` // 意图 Intent intent = new Intent( this, DetailActivity.class ); intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP ); // 回退栈 TaskStackBuilder taskStackBuilder = TaskStackBuilder.create( this ); taskStackBuilder.addParentStack( DetailActivity.class ); taskStackBuilder.addNextIntent( intent ); PendingIntent pendingIntent = taskStackBuilder .getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); // 构建通知 String channel = getChannel(); Notification notification = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a TaskStack notification " ) .setAutoCancel( true ) .setContentIntent( pendingIntent ) .build(); // 发出通知 NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE ); assert notificationManager != null; notificationManager.notify( 12, notification ); ``` ![](img/pic06.gif) ## 添加BroadCastReceiver Action ``` public class TestReceiver extends BroadcastReceiver { private static final String TAG = TestReceiver.class.getSimpleName(); @Override public void onReceive ( Context context, Intent intent ) { String extra = intent.getStringExtra( "extra" ); Log.e( TAG, "onReceive : " + Thread.currentThread() + " " + System.currentTimeMillis() + " " + extra ); } } ``` ``` Intent intent = new Intent( this, TestReceiver.class ); String action = "tech.threekilogram.notification.ACTION"; intent.setAction( action ); intent.putExtra( "extra", "Hello" ); PendingIntent pendingIntent = PendingIntent.getBroadcast( this, 0, intent, 0 ); String channel = getChannel(); Notification notification = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a action notification " ) .setAutoCancel( true ) .addAction( R.drawable.weibo, "test action", pendingIntent ) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE ); assert notificationManager != null; notificationManager.notify( 13, notification ); ``` ![](img/pic07.gif) ``` onReceive : Thread[main,5,main] 1538188307396 Hello onReceive : Thread[main,5,main] 1538188308011 Hello onReceive : Thread[main,5,main] 1538188308550 Hello ``` ## 添加InputAction ``` public class InputReceiver extends BroadcastReceiver { private static final String TAG = InputReceiver.class.getSimpleName(); @Override public void onReceive ( Context context, Intent intent ) { Bundle results = RemoteInput.getResultsFromIntent( intent ); //读取输入 if( results != null ) { CharSequence key_input = results.getCharSequence( "key_input" ); Log.e( TAG, "onReceive : " + key_input + " " + Thread.currentThread() + " " + System .currentTimeMillis() ); // 移除输入 Notification notification = new Builder( context, "MainChannel" ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a inputReceive notification " ) .build(); NotificationManagerCompat notificationManager = NotificationManagerCompat .from( context ); assert notificationManager != null; notificationManager.notify( 14, notification ); } } } ``` ``` // 指定接收receiver Intent intent = new Intent( this, InputReceiver.class ); PendingIntent pendingIntent = PendingIntent .getBroadcast( this, 12, intent, PendingIntent.FLAG_UPDATE_CURRENT ); // 创建输入 RemoteInput remoteInput = new RemoteInput.Builder( "key_input" ).setLabel( "reply" ) .build(); // 添加输入到Action Action action = new Action.Builder( R.drawable.replay, "replay", pendingIntent ) .addRemoteInput( remoteInput ) .build(); // 构建通知 String channel = getChannel(); Notification notification = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a action notification " ) .setAutoCancel( true ) .addAction( action ) --> action .build(); NotificationManagerCompat notificationManager = NotificationManagerCompat.from( this ); assert notificationManager != null; notificationManager.notify( 14, notification ); ``` ![](img/pic08.gif) ## 创建进度条通知 创建进度条通知 ``` String channel = getChannel(); Builder builder = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a progress notification " ) .setProgress( mSeekBar.getMax(), mSeekBar.getProgress(), false ); Notification notification = builder.build(); NotificationManagerCompat notificationManager = NotificationManagerCompat .from( this ); assert notificationManager != null; notificationManager.notify( 15, notification ); ``` 更新进度条 ``` mBuilder.setProgress( 100, getProgress(), false ); mManagerCompat.notify( 15, mBuilder.build() ); ``` 删除进度条 ``` mBuilder.setContentText( "complete" ).setProgress( 0, 0, false ); mManagerCompat.notify( 15, mBuilder.build() ); ``` ![](img/pic09.gif) ## 锁屏通知 ``` String channel = getChannel(); Builder builder = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a lock screen notification " ) .setVisibility( Notification.VISIBILITY_PUBLIC ); --> 设置可见性 Notification notification = builder.build(); NotificationManagerCompat notificationManager = NotificationManagerCompat .from( this ); assert notificationManager != null; notificationManager.notify( 16, notification ); ``` ![](img/pic10.gif) ## 大段文本样式 ``` String channel = getChannel(); Notification notification = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "big text Notification" ) .setContentText( "this is a big text notification " ) .setStyle( new BigTextStyle().bigText( "曾经沧海难为水,除却巫山不是云.取次花丛懒回顾,半缘修道半缘君" ) ) .setPriority( NotificationCompat.PRIORITY_DEFAULT ) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE ); assert notificationManager != null; notificationManager.notify( 1, notification ); ``` ![](img/pic11.gif) ## 大图片 ``` Bitmap bitmap = BitmapFactory.decodeResource( getResources(), R.drawable.a704 ); String channel = getChannel(); Notification notification = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a action notification " ) .setAutoCancel( true ) .setLargeIcon( bitmap ) --> 折叠时图片 .setStyle( new BigPictureStyle().bigPicture( bitmap ).bigLargeIcon( null ) ) -->展开后折叠图片设置为null .build(); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE ); assert notificationManager != null; notificationManager.notify( 17, notification ); ``` ![](img/pic12.gif) ## InBox ``` String channel = getChannel(); Notification notification = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a action notification " ) .setAutoCancel( true ) .setStyle( new InboxStyle().addLine( "曾经沧海难为水" ) .addLine( "除却巫山不是云" ) .addLine( "取次花丛懒回顾" ) .addLine( "半缘修道半缘君" ) ).build(); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE ); assert notificationManager != null; notificationManager.notify( 18, notification ); ``` ![](img/pic13.gif) ## 自定义样式 ``` RemoteViews remoteViews = new RemoteViews( getPackageName(), R.layout.remote_00 ); RemoteViews remoteViewsE = new RemoteViews( getPackageName(), R.layout.remote_01 ); String channel = getChannel(); Notification notification = new Builder( this, channel ) .setSmallIcon( R.drawable.egg_select ) .setContentTitle( "new Notification" ) .setContentText( "this is a action notification " ) .setAutoCancel( true ) .setStyle( new DecoratedCustomViewStyle() ) .setCustomContentView( remoteViews ) .setCustomBigContentView( remoteViewsE ) .build(); NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE ); assert notificationManager != null; notificationManager.notify( 19, notification ); ``` ![](img/pic14.gif)