通知的使用
调用Context的getSystemService方法获得一个NotificationManager对象manager
调用NotificationCompat.Builder(MainActivity.this,“default”)new一个Notification对象
对这个Notification对象进行一系列操作
调用NotificationManager对象manager的manager.notify(1, notification)方法
下面是显示通知,点击通知跳转另外一个活动的代码
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);//跳转pendingIntent
NotificationManager manager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(MainActivity.this,"default")
.setContentTitle("这是一个通知标题")
.setContentText("这是通知内容")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground))
.setContentIntent(pendingIntent)//设置跳转
.setAutoCancel(true)//点击后撤销通知
.setPriority(NotificationCompat.PRIORITY_MAX)
.build();
manager.notify(1, notification);