https://developer.android.com/training/notify-user/build-notification 创建一个通知
https://www.cnblogs.com/travellife/p/Android-Notification-xiang-jie.html
dependencies {
implementation "com.android.support:support-compat:28.0.0"
}
1 notificationManager 是状态栏通知的管理类,负责发通知,清除通知,NotificationManager 是一个系统的service 必须通过getSystemService 方法来获取
2 notification 状态栏栏通知
2.1 创建 notification nofificationMnager.notify(int,Notification)
2.2 更新notification notification.setLatesEventInfo
2.3 删除notification notificationManager.cancel()
3 notification设置震动 铃声
baseNf = new Notification()
baseNf.icon = R.drawable.icon
baseNf.tickertext ="you clicked BaseNF"
发送通知
NotificationManager notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)//设置小图标
.setContentTitle("最简单的Notification")//设置通知标题
.setContentText("只有小图标、标题、内容");//设置通知内容
//.setWhen(System.currentTimeMillis());//设置通知时间,默认为系统发出通知的时间,通常不用设置
notifyManager.notify(1, builder.build()); //通过builder.build()方法生成Notification对象,并发送通知,id=1
可点击的通知
NotificationManager notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)//设置小图标
.setContentTitle("最简单的Notification")//设置通知标题
.setContentText("只有小图标、标题、内容") //设置通知内容;
.setContentIntent(pendingIntent);
//.setWhen(System.currentTimeMillis());//设置通知时间,默认为系统发出通知的时间,通常不用设置
notifyManager.notify(1, builder.build()); //通过builder.build()方法生成Notification对象,并发送通知,id=1
带震动的通知
<uses-permission android:name="android.permission.VIBRATE" />
NotificationManager notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(MainActivity.this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.mipmap.ic_launcher)//设置小图标
.setContentTitle("最简单的Notification")//设置通知标题
.setContentText("只有小图标、标题、内容") //设置通知内容;
.setContentIntent(pendingIntent)
.setSound(Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI,"2"));
//.setWhen(System.currentTimeMillis());//设置通知时间,默认为系统发出通知的时间,通常不用设置
notifyManager.notify(1, builder.build()); //通过builder.build()方法生成Notification对象,并发送通知,id=1
关闭通知
manager.cancel(4);