notification全解和工具类

分类

Paste_Image.png

相关属性

显示相关

Paste_Image.png

标识符flag

Paste_Image.png

优先级

Paste_Image.png

提醒方式

Paste_Image.png

事件(PeddingIntent)

Paste_Image.png

返回activity栈

参考https://gold.xitu.io/post/5863264861ff4b0068b1817d

7.0新功能

Paste_Image.png

二次封装后的api

第一步:

buildSimple(int id,int icon,CharSequence contentTitle ,CharSequence contentText,PendingIntent contentIntent)

buildProgress(int id,int icon,CharSequence contentTitle,int progress,int max)

 buildBigPic(int id,int icon,CharSequence contentTitle,CharSequence contentText,CharSequence summaryText)

 buildBigText(int id,int icon,CharSequence contentTitle,CharSequence contentText)

 buildMailBox(int id,int icon,CharSequence contentTitle)

// buildMedia(int id,int icon,CharSequence contentTitle,CharSequence contentText)//todo 

第二步:其他可选设置

设置三类intent:
setContentIntent(PendingIntent contentIntent)
setDeleteIntent(PendingIntent deleteIntent)
setFullScreenIntent(PendingIntent fullscreenIntent)

添加按钮:
addBtn(int icon,CharSequence text,PendingIntent pendingIntent)
开启head-up模式
setHeadup()
//提示语,默认为"您有新的消息"
setTicker(CharSequence ticker)
//大小图标
setSamllIcon(int smallIcon)
setBigIcon(int bigIcon)
//优先级,默认为default
setPriority(int priority)
//设置成为不可删除
setOnGoing()
//提示模式:默认情况下只有呼吸灯提示
setAction(boolean sound, boolean vibrate, boolean lights)
//设置为前台服务的notification
setForgroundService() 
//锁屏显示的控制(默认不显示)
setLockScreenVisiablity(int lockScreenVisiablity)
  VISIBILITY_PRIVATE : 显示基本信息,如通知的图标,但隐藏通知的全部内容 
  VISIBILITY_PUBLIC : 显示通知的全部内容 
  VISIBILITY_SECRET : 不显示任何内容,包括图标

第三步

.show()

取消

cancel(int id)
cancelAll()

示例代码

 NotifyUtil.buildSimple(100,R.mipmap.ic_launcher,"标题标题标题图表题滴滴滴","哈哈哈哈哈哈哈呼呼呼呼呼呼",null)
                        .setHeadup()
                        .addBtn(R.mipmap.ic_launcher,"left", NotifyUtil.buildIntent(MainActivity.class))
                        .addBtn(R.mipmap.ic_launcher,"rightdd", NotifyUtil.buildIntent(MainActivity.class))
                        .show();

NotifyUtil.buildBigPic(101,R.drawable.timg,"title","content","summmaujds")
                        .setPicRes(R.drawable.timg2)
                        .show();

 NotifyUtil.buildProgress(102,R.mipmap.ic_launcher,"正在下载",progresses,100).show();

 NotifyUtil.buildMailBox(104,R.drawable.timg,"title")
                        .addMsg("11111111111")
                        .addMsg("33333333333333")
                        .addMsg("6666666666666666666")
                        .show();

NotifyUtil.buildBigText(103,R.drawable.timg,"jtitle","我学习最快的方法就是先看效果," +
                        "再想原理最后,将它实现。效果是最直观的,而且能够有效的判断所学的东西是不是想要的。" +
                        "现在网上的资料实在太杂,很多花了很多时间去研究,最后发现坑爹了,不是想要的。" +
                        "这篇文章首先会介绍能实现的主要功能。然后是解决问题的基本思想,接着是具体的一些实现。" +
                        "本篇文章和以前的风格有所不同,以前都是文章中代码比较少,附上demo,但发现这样可能不方便读者," +
                        "所以采用了实现效果以及主要代码的形式。这种方式现在还在测试阶段,如果觉得以前的模式比较" +
                        "好或者其他更好的方式的话可以給我留言,以后的文章会做出相应的调整 。").show();

代码

https://github.com/hss01248/NotifyUtil

感谢

详细分类: https://gold.xitu.io/post/5863264861ff4b0068b1817d
7.0新特性:http://www.jianshu.com/p/33e84d5cb43f
每种类型都有很全的示例代码:http://blog.csdn.net/w804518214/article/details/51231946

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Android中,我们可以使用`NotificationCompat`来创建通知,通知可以根据需要展开或收缩。当通知被展开时,我们可以自定义通知的布局,并添加一些交互式组件,例如按钮、文本框等。 为了监听通知的展开和收缩事件,我们需要使用`NotificationCompat`提供的`setCustomBigContentView()`和`setCustomContentView()`方法,分别设置通知展开和收缩时的自定义布局。然后,我们可以在自定义布局中添加相应的交互式组件,并为其添加监听器。 示例代码: ``` // 创建通知的Builder NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.notification_icon) .setContentTitle("My notification") .setContentText("Hello World!") .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(true); // 创建通知展开时的自定义布局 RemoteViews expandedView = new RemoteViews(getPackageName(), R.layout.notification_expanded); // 添加监听器 expandedView.setOnClickPendingIntent(R.id.button1, pendingIntent1); expandedView.setOnClickPendingIntent(R.id.button2, pendingIntent2); // 设置通知展开时的自定义布局 builder.setCustomBigContentView(expandedView); // 创建通知收缩时的自定义布局 RemoteViews collapsedView = new RemoteViews(getPackageName(), R.layout.notification_collapsed); // 添加监听器 collapsedView.setOnClickPendingIntent(R.id.button, pendingIntent); // 设置通知收缩时的自定义布局 builder.setCustomContentView(collapsedView); // 发送通知 NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); notificationManager.notify(notificationId, builder.build()); ``` 在上面的代码中,我们创建了一个通知的Builder,并设置了通知展开和收缩时的自定义布局。为了监听自定义布局中的按钮点击事件,我们使用`setOnClickPendingIntent()`方法为按钮添加了相应的`PendingIntent`。当用户点击按钮时,相应的`PendingIntent`会被触发。 需要注意的是,在自定义布局中添加的交互式组件的ID必须与布局文件中定义的ID一致。否则,添加的监听器将不会生效。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值