Notification

复习的时候发现旧版本的api用不了了,贴个新的以后备用

          Intent intent = new Intent(MainActivity.this,Main2Activity.class);

                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);//用于点击通知切换Activity的

                NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);//获取系统服务

          //构造通知 Notification notify2 = new Notification.Builder(MainActivity.this) .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏中的小图片,尺寸一般建议在24×24,这个图片同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,可以使用setLargeIcon(Bitmap // icon) .setTicker("TickerText:" + "您有新短消息,请注意查收!")// 设置在status // bar上显示的提示文字 .setContentTitle("Notification Title")// 设置在下拉status // bar后Activity,本例子中的NotififyMessage的TextView中显示的标题 .setContentText("This is the notification message")// TextView中显示的详细内容 .setContentIntent(pendingIntent) // 关联PendingIntent .setNumber(1) // 在TextView的右方显示的数字,可放大图片看,在最右侧。这个number同时也起到一个序列号的左右,如果多个触发多个通知(同一ID),可以指定显示哪一个。 .build(); manager.notify(1, notify2);//设置唯一的id

  

在新的活动取消通知

NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

        manager.cancel(1);//唯一的通知ID

  

原文地址:https://www.cnblogs.com/wzben/p/6088470.html