Notification操作大全

目录

  • 一:普通的Notification
    • Notification 的基本操作
    • 给 Notification 设置 Action
    • 更新 Notification
    • 取消 Notification
    • 设置 Notification 的通知效果
      • 铃声
      • 震动
      • 呼吸灯
  • 二: 折叠式(4.0以后出来的)
  • 三:悬挂式
  • 四:其他相关介绍
      1. 多个通知放入到一个组内
      1. 待回复功能的
  • 五:所有API 汇总记录
  • Demo地址

一:普通的Notification

1. Notification 的基本操作

一个 Notification 的必要属性有三项,如果不设置则在运行时会抛出异常:

  • 小图标,通过 setSmallIcon()方法设置
  • 标题,通过 setContentTitle()方法设置
  • 内容,通过setContentText() 方法设置
 private void baseNotify() {
        //获取NotificationManager实例
        NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //实例化NotificationCompat.Builder并设置相关属性
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
                //设置小图标
                .setSmallIcon(R.mipmap.ic_launcher)
                //设置通知标题
                .setContentTitle("最简单的Notification")
                //设置通知内容
                .setContentText("只有小图标、标题、内容");
        //通过builder.build()方法生成Notification对象,并发送通知,id=1
        if (notifyManager != null) {
            notifyManager.notify(1, builder.build());
        }
    }

2. 给 Notification 设置 Action

 /**
     * 发送一个点击跳转到MainActivity的消息
     */
    private void action() {
        //获取NotificationManager实例
        NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        //获取PendingIntent
        Intent mainIntent = new Intent(this, MainActivity.class);
        PendingIntent mainPendingIntent = PendingIntent.getActivity(this, 0, mainIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        //创建 Notification.Builder 对象
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                //点击通知后自动清除
                .setAutoCancel(true)
                .setContentTitle("我是带Action的Notification")
                .setContentText("点我会打开MainActivity")
                .setContentIntent(mainPendingIntent);
        //发送通知
        notifyManager.notify(2, builder.build());
    }

3. 更新 Notification

更新通知很简单,只需要再次发送相同 ID 的通知即可,如果之前的通知还未被取消,则会直接更新该通知相关的属性;如果之前的通知已经被取消,则会重新创建一个新通知。

4. 取消 Notification

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

5. 设置 Notification 的通知效果

Notification 有震动、响铃、呼吸灯三种响铃效果,可以通过
setDefaults(int defualts) 方法来设置。 Default 属性有以下四种,一旦设置了 Default 效果,自定义的效果就会失效

属性效果备注
Notification.DEFAULT_VIBRATE 震动效果 需要申请震动权限<uses-permission android:name="android.permission.VIBRATE" />
Notification.DEFAULT_SOUND 声音效果 设置此值后,调用setSound()设置自定义声音无效
Notification.DEFAULT_LIGHTS 呼吸灯效果 使用时须与 Notification.FLAG_SHOW_LIGHTS 结合使用,否则无效
Notification.DEFAULT_ALL 添加上述三种默认提醒效果
Notification.FLAG_SHOW_LIGHTS 三色灯提醒 过时的方法了
Notification.FLAG_ONGOING_EVENT 发起正在运行事件(活动中)
Notification.FLAG_INSISTENT 让声音、振动无限循环,直到用户响应 (取消或者打开)
Notification.FLAG_ONLY_ALERT_ONCE 发起Notification后,铃声和震动均只执行一次
Notification.FLAG_AUTO_CANCEL 用户单击通知后自动消失
Notification.FLAG_NO_CLEAR 只有调用NotificationManager.cancel()时才会清除
Notification.FLAG_FOREGROUND_SERVICE 表示正在运行的服务

(1) 铃声

 private void init_sound() {
        NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("我是伴有铃声效果的通知")
                .setContentText("美妙么?安静听~")
                //调用自己提供的铃声,位于 /res/values/raw 目录下
                .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notice));
      if (notifyManager != null) {
            notifyManager.notify(2, builder.build());
        }
    }

(2)震动

/**
* 展示有震动效果的通知,需要在AndroidManifest.xml中申请震动权限
* <uses-permission android:name="android.permission.VIBRATE" />
* 补充:测试震动的时候,手机的模式一定要调成铃声+震动模式,否则你是感受不到震动的
*/
private void init_vibration() {
        NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        long[] vibrate = new long[]{0, 500, 1000, 1500};
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("我是伴有震动效果的通知")
                .setContentText("颤抖吧,凡人~")
                .setVibrate(vibrate);
        if (notifyManager != null) {
            notifyManager.notify(3, builder.build());
        }
    }

(3)呼吸灯


//我是没测试成功,有知道原因的小伙伴,望告知
    private void init_ligths() {
        final NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("我是带有呼吸灯效果的通知")
                .setContentText("一闪一闪亮晶晶~")
                //ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
                .setLights(0xFF0000, 3000, 3000);
        Notification notify = builder.build();
        //只有在设置了标志符Flags为Notification.FLAG_SHOW_LIGHTS的时候,才支持呼吸灯提醒。
        notify.flags = Notification.FLAG_SHOW_LIGHTS;
        //使用handler延迟发送通知,因为连接usb时,呼吸灯一直会亮着
        notifyManager.notify(4, builder.build());
    }

二: 折叠式(4.0以后出来的)

 
Untitled4.gif

一张图说明什么是折叠式

 @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    private void init_remote() {
        NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("折叠式")
                .setContentText("只有小图标、标题、内容");

        Notification notification = builder.build();
//就这里需要加上一个属性,R.layout.notity_remote 是展开后的布局
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notity_remote);
        notification.bigContentView = remoteViews;
        if (notifyManager != null) {
            notifyManager.notify(8,notification);
        }
    }

三:悬挂式

 
Untitled4.gif
 private void init_screen() {
        NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("悬挂式")
                //通过setFullScreenIntent将一个Notification变成悬挂式Notification
                .setFullScreenIntent(pendingIntent,true)
                //设置Notification的显示等级
                .setVisibility(Notification.VISIBILITY_PRIVATE)
                .setContentText("只有小图标、标题、内容");

        Notification notification = builder.build();
        if (notifyManager != null) {
            notifyManager.notify(9,notification);
        }
    }

补充,Notification的显示等级(setVisibility())

属性说明
VISIBILITY_PRIVATE 表面只有当没有锁屏的时候才能够显示
VISIBILITY_PUBLIC 表明任何情况下都会显示
VISIBILITY_SECRET 表明在pin,password等安全锁和没有锁屏的情况下才能够显示

四:其他相关介绍

1. 多个通知放入到一个组内

 
Untitled4.gif
  int i = 200;
    private void init_group() {
        NotificationManager notifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        I++;
        String notificationContent = "相同组:" + I;
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setStyle(new NotificationCompat.BigTextStyle().setSummaryText(notificationContent))
                .setGroup("EAT")
                .setGroupSummary(true);
        final Notification notification = builder.build();

        notifyManager.notify(10, notification);
    }

2. 待回复功能的

 
Untitled4.gif
    @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
    private void init_huifu() {
        //[1]获取一个NotificationManager
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(
                Context.NOTIFICATION_SERVICE);
        //[2]创建remoteInput对象,这个对象指定了这个notification的标题和一个key
        String replyLabel = getResources().getString(R.string.app_name);
        RemoteInput remoteInput = new RemoteInput.Builder("KEY")
                .setLabel(replyLabel)
                .build();
        //[3]创建一个Action对象 可以指定用户一个友好的输入提示,可以指定跳转意图,
        Intent deleteIntent = new Intent(this, MainActivity.class);
        Notification.Action action =
                new Notification.Action.Builder(R.mipmap.ic_launcher,
                        "请输入想回复内容", PendingIntent.getActivity(this, 10002, deleteIntent, 0))
                        .addRemoteInput(remoteInput)
                        .build();

        //[3]创建一个Notification对象
        Notification notification =
                new Notification.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Title")
                        .setContentText("msg")
                        .addAction(action)
                        .build();

        //[4]发送这个notification
        mNotificationManager.notify(11, notification);

    }

五:所有API 汇总记录

API介绍备注
setSmallIcon 设置小图标
setContentTitle 设置标题
setContentText 设置内容
.setLights() 设置灯光 ledARGB 表示灯光颜色、 ledOnMS 亮持续时间、ledOffMS 暗的时间
setSound 设置自己提供的铃声 Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notice)
setVibrate 设置震动 long[] vibrate = new long[]{0, 500, 1000, 1500};
setAutoCancel() 点击通知后自动清除 true自动清除
setFullScreenIntent 将一个Notification变成悬挂式Notification 具体使用看上面的介绍
setVisibility 设置Notification的显示等级 看上面 悬挂式
setContentIntent 设置点击事件 查看上面 给 Notification 设置 Action
setDefaults 设置 Notification 的通知效果 查看上面的 5

最后

GitHub 项目地址 点击跳转

感谢

http://blog.csdn.net/axi295309066/article/details/52929879



作者:Allens_Jiang
链接:https://www.jianshu.com/p/bdc132bdb0fa
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
posted @ 2018-01-03 17:02  Alter  阅读(431)  评论(0编辑  收藏  举报