安卓通知与NotificationCompat.Builder

安卓更新挺快,以至于之前学的关于通知的使用方法都过时了

现在重新再写个文章记录一下


创建通知

Android 3.0 (API level 11)之前,使用new Notification()方式创建通知:

NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
      this, 0, new Intent(this, ResultActivity.class), 0);

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, title, content, contentIntent);

mNotifyMgr.notify(NOTIFICATIONS_ID, notification);

Android 3.0 (API level 11)及更高版本,改用Notification.Builder()来创建通知:
NotificationManager mNotifyMgr = 
      (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
      this, 0, new Intent(this, ResultActivity.class), 0);

Notification notification = new Notification.Builder(this)
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle("My notification")
            .setContentText("Hello World!")
            .setContentIntent(contentIntent)
            .build();// getNotification()

mNotifyMgr.notify(NOTIFICATIONS_ID, notification);

这里需要注意: "build()" 是Androdi 4.1(API level 16)加入的,用以替代
"getNotification()"。API level 16开始弃用"getNotification()"


通知基本用法

一个通知必须包含以下三项属性:

  • 小图标,对应 setSmallIcon()
  • 通知标题,对应 setContentTitle()
  • 详细信息,对应 setContentText()

其他属性均为可选项,更多属性方法请参考NotificationCompat.Builder

尽管其他都是可选的,但一般都会为通知添加至少一个动作(Action),这个动作可以是跳转到Activity、启动一个Service或发送一个Broadcas等。 通过以下方式为通知添加动作:

  • 使用PendingIntent
  • 通过大视图通知的 Action Button //仅支持Android 4.1 (API level 16)及更高版本,稍后会介绍

创建通知

1、实例化一个NotificationCompat.Builder对象

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("我是头部")
                .setContentText("内容内容内容内容内容内容内容内容");


NotificationCompat.Builder会自动设置默认值:

  • priority: PRIORITY_DEFAULT           --------优先级,有五个优先级别,范围从 PRIORITY_MIN (-2) 到 PRIORITY_MAX (2);默认为 PRIORITY_DEFAULT (0)。
  • when: System.currentTimeMillis()--------通知创建的时间,有的手机会显示,有的不会
  • audio stream: STREAM_DEFAULT--------当声音响起时,所用的音频流的类型
2、定义并设置一个通知动作(Action)


PendingIntent类封装了一个Intent和一个动作,当调用该类的send方法的时候,将会执行该动作。由于PendingIntent类是一个待处理的意图,这个动作通常是在将来的某个时刻要调用的一个操作,很可能是系统要调用的。
PendingIntent类中的动作是Context类中的几个方法之一,例如startActivity,startService,sendBroadcast
使用PendingIntent来启动一个Activity

Intent resultIntent = new Intent(this, ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
            this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);

PendingIntent pi = PendingIntent.getActivity(Context context, int requestCode,Intent intent, int flags);

静态方法getActivity是返回PendingIntent类的一个实例的几个方法之一,其他的方法还有 getActivities、getService和getBroadcast
这些方法决定了最终PendingIntent所能执行的动作,可以用来启动Activity,启动Service,发送广播等


3、NotificationManager

要发布一个通知,可以使用NotificationManager,这是Android系统中的内建服务之一,是一个已有的系统服务,可以通过如下代码来获取它
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


然后,在该NotificationManager上调用notify方法来发布一个通知,需要传入唯一的ID和Notification对象
mNotificationManager.notify(int id, notification);

注意这里要求传入的是notification,但是我们创建的是 NotificationCompat.Builder,这时候你需要生成Notification对象
Notificatioin notification = Builder.build();

通知ID用于标识该通知,在想要取消特定Notification的时候,就需要使用到它

mNotificationManager.cancel(int id);


此外,除非发生以下情况之一,否则通知会一直可见:

  • 用户单独或通过使用“全部清除”清除了该通知(如果通知可以清除)
  • 用户点击通知,且在创建通知时调用了 setAutoCancel(true)
  • 针对特定的通知 ID 调用了 cancel(int id)
  • 调用了 cancelAll() 方法,该方法将删除之前发出的所有通知

更新通知

更新通知很简单,只需再次发送相同ID的通知即可,如果之前的通知依然存在则会更新通知属性,如果之前通知不存在则重新创建。

取消通知

取消通知有如下4种方式:

  • 点击通知栏的清除按钮,会清除所有可清除的通知
  • 设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL的通知,点击该通知时会清除它
  • 通过 NotificationManager 调用 cancel() 方法清除指定ID的通知
  • 通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知


创建简单通知


private int id = 1;

    public void notification(View view) {
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.ic_submit);
        Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
        //设置小图标
        mBuilder.setSmallIcon(R.mipmap.ic_launcher);
        //设置大图标
        mBuilder.setLargeIcon(bitmap);
        //设置标题
        mBuilder.setContentTitle("这是标题");
        //设置通知正文
        mBuilder.setContentText("这是正文,当前ID是:" + id);
        //设置摘要
        mBuilder.setSubText("这是摘要");
        //设置是否点击消息后自动clean
        mBuilder.setAutoCancel(true);
        //在通知的右边设置大的文本。
        mBuilder.setContentInfo("右侧文本");
        //与setContentInfo类似,但如果设置了setContentInfo则无效果
        //用于当显示了多个相同ID的Notification时,显示消息总数
        mBuilder.setNumber(2);
        //通知在状态栏显示时的文本
        mBuilder.setTicker("在状态栏上显示的文本");
        //设置优先级
        mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
        //自定义消息时间,以毫秒为单位,当前设置为比系统时间少一小时
        mBuilder.setWhen(System.currentTimeMillis() - 3600000);
        //设置为一个正在进行的通知,此时用户无法清除通知
        mBuilder.setOngoing(true);
        //设置消息的提醒方式,震动提醒:DEFAULT_VIBRATE     声音提醒:NotificationCompat.DEFAULT_SOUND
        //三色灯提醒NotificationCompat.DEFAULT_LIGHTS     以上三种方式一起:DEFAULT_ALL
        mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        //设置震动方式,延迟零秒,震动一秒,延迟一秒、震动一秒
        mBuilder.setVibrate(new long[]{0, 1000, 1000, 1000});

        Intent intent = new Intent(this, ShadowTextViewActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
        mBuilder.setContentIntent(pIntent);

        NotificationManager mNotificationManager = 
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值