系统默认的Notification
Notification为用于在状态栏显示通知信息的控件.
功能作用:
1.显示接收到短消息、即时消息等信息,如微信/QQ等;
2.显示客户端的推送消息,如广告/推荐新闻等;
3.显示正在进行的事件,如音乐播放/后台下载等.实现步骤
a.调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotificationManager
b.通过Notification.Builder来创建通知
c.为Notification设置各种属性。
d.通过 NotificationManager 的 notify ()方法发送Notification。2.1 获取NotificationManager的对象
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
2..2 创建Builder的对象,用来设置设置属性
Notification.Builder builder = new Notification.Builder(MainActivity.this);
2.3设置属性
设置大图标
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
builder.setLargeIcon(bitmap);
设置消息来时的提示
builder.setTicker(“通知”);
设置通知标题
builder.setContentTitle(“紧急通知”);
通知内容
builder.setContentText(“下大暴雨,明天不用上班”);
设置小图标
builder.setSmallIcon(R.mipmap.ic_launcher);
创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的
activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity
Intent intent = new Intent(MainActivity.this, Result.class);
参数1.上下文对象,参数2.返回的状态码,参数3.Intent 通常是跳转的Intent,
参数4.通知与页面 的更新类型
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 100,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
2.4 根据builder创建Notification
Notification notification = builder.build();
通过 NotificationManager 的 notify ()方法发送Notification。
参数1.为id,用来区分不同的Notification
manager.notify(100, notification);自定义Notification
3.1 获取NotificationManager的对象
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
3.2通过Notification.Builder来创建Notification对象
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
3.3设置属性
设置图标,如果不设置这个,自定义的图标将显示不出来
builder.setSmallIcon(R.mipmap.ic_launcher);
3.4 创建remoteView的对象
RemoteViews views = new RemoteViews(getPackageName(),R.layout.notification_layout);
views.setImageViewResource(R.id.notification_iv,R.mipmap.ic_launcher);
views.setTextViewText(R.id.notification_tv,”这是一个机器人”);
builder.setContent(views);使用PendingIntent设置跳转 Intent intent = new Intent(MainActivity.this,Main2Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 12,intent,PendingIntent.FLAG_UPDATE_CURRENT); **使用 setContentIntent 时点击通知栏时,就可以跳转** builder.setContentIntent(pendingIntent); **设置OnClick时,点击某一个控件时才会跳转** views.setOnClickPendingIntent(R.id.notification_iv,pendingIntent); 用 builder.build()得到Notification的对象 Notification notification = builder.build(); 3.5使用NotificationManager发送Notification manager.notify(1,notification);
4.取消通知
1.拿到manager
NotificationManager manager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
2..取消通知
取消所有通知
manager.cancelAll();
取消指定的通知,根据就id号确定对象
manager.cancel(100);5 PendingIntent
Notification.Builder有一个非常重要的方法,setContentIntent(PendingIntent intent).此方法代表的是,
点击该Notification后执行的意图.
PendingIntent和Intent略有不同,它可以设置执行次数,主要用于远程服务通信/闹铃/通知/启动器/短信
中,在一般情况下用的比较少。表示的是即将发生的/待定的意图.1.通过getActivity(Context context, int requestCode, Intent intent, int flags)系列
方法从系统取得一个用于启动一个Activity的PendingIntent对象。
2.通过getService(Context context, int requestCode, Intent intent, int flags)方法
从系统取得一个用于启 动一个Service的PendingIntent对象。
3.通过getBroadcast(Context context, int requestCode, Intent intent, int flags)方法
从系统取得一个用于 向BroadcastReceiver的Intent广播的。Intent intent = new Intent(MainActivity.this, Result.class); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent); PendingIntent的位标识符: FLAG_ONE_SHOT : 这个PendingIntent只能使用一次。 FLAG_NO_CREATE : 如果被描述的PendingIntent不存在,那么简单地返回null, 而不是创建它。 FLAG_CANCEL_CURRENT : 如果被描述的PendingIntent已经存在,在即将生成一个新的 PendingIntent前,当前的一个要被取消。 FLAG_UPDATE_CURRENT :如果被描述的PendingIntent已经存在,那么继续保持它,但它其中的 数据会因为新Intent而更新。
Notification
最新推荐文章于 2025-01-08 09:00:00 发布