notification
#前言
最近在做消息通知类notification的相关业务,利用闲暇时间总结一下。主要分为两部分来记录:发送消息和接收消息。
发送消息
发送消息利用notificationmanager类的notify方法来实现,现用最普通的方式发送:
notification.builder builder = new notification.builder(context, channelid); builder.setsmallicon(r.drawable.ic_launcher) .setcontenttitle("标题") .setcontenttext("内容") notification notification = builder.build(); notificationmanager manager = (notificationmanager) getsystemservice(context.notification_service); notificationchannel notificationchannel = new notificationchannel(channelid, name,notificationmanager.importance_default); manager.createnotificationchannel(notificationchannel); manager.notify(r.string.app_name,notification);
这样一段简单的发送消息的代码就完成了。
在发送消息的时候builder可以携带很多参数,具体查看api即可,这里我记录一下我用到的一些常用的方法。
builder..addaction(icon,title,pendingintent);
三个参数分别是int类型(api已过时),chartext类型和pendingintent意图。
这个方法我认为就很不错,比如当需要封装一个公共的弹窗,但是按钮数量和按钮颜色以及弹窗的标题不一样,这时候就可以利用这个方法进行规定。因为action是个数组,需要几个传几个就可以,比如弹窗有两个按钮,那么就传两个action过去,第一个参数传按钮的颜色值,第二个参数传按钮的名称,第三个参数传pendingintent意图。这样就不用很复杂的实现逻辑就可以完成这个公共的弹窗。
同样,builder支持传bundle,方法如下:
bundle bundle = new bundle(); builder.setextras(bundle);
bundle可以携带参数,同理刚才说的封装公共弹窗也可以用bundle传值的形式实现,但是相比较action的方式是不是就会复杂很多。
bundle可以传递一些公共的参数,比如一个type,当接收到通知的时候利用type来区分要做什么动作,这个就看项目实际需求了。
这里我把通知跳转意图的跳转activity和接收广播的代码放上来,也为自己做个记录。
bundle bundle = new bundle(); notificationchannel notificationchannel = new notificationchannel("99", "temp", notificationmanager.importance_default); notification.builder builder = new notification.builder(mainactivity.this, "99"); //intent intent = new intent(mainactivity.this,secondactivity.class); intent intent = new intent(mainactivity.this,mybroadcastreceiver.class); intent.setaction("android.intent.action.my_broadcast"); intent.addcategory(intent.category_launcher); intent.setflags(intent.flag_activity_new_task); //pendingintent pendingintent = pendingintent.getactivity(mainactivity.this,0,intent,pendingintent.flag_cancel_current); pendingintent pendingintent = pendingintent.getbroadcast(mainactivity.this,0,intent,pendingintent.flag_cancel_current); builder.setsmallicon(r.drawable.ic_launcher_background) .setcontenttitle("标题") .setcontenttext("内容") .setwhen(system.currenttimemillis()) .addaction(r.color.purple_200,"确定",pendingintent) .addaction(r.color.black,"取消",pendingintent) .setextras(bundle); notification notification = builder.build(); notificationmanager manager = (notificationmanager) getsystemservice(context.notification_service); manager.createnotificationchannel(notificationchannel); manager.notify(r.string.app_name,notification);
删除通知:
manager.cancel(tag,id);
这里注意的是发送通知可以是两个参数和三个参数,三个参数的方法第一个参数是tag,那么调用删除方法的时候要和这个tag对应上,就是要删除哪条消息就传哪个tag。
接收消息
接收消息我是重新创建一个类继承自notificationlistenerservice,然后复写需要用到的几个方法,由于我的项目是systemui的开发,所以和实际需求上可能会略有不同。
@override public void onnotificationposted(statusbarnotification sbn)
该方法是接收到消息通知时的回调。
我不太清楚为什么我只发送了一次通知,但在该方法中却收到了两次消息。那就想办法过滤一下吧。利用sbn返回的key和time来判断。
mpreviousnotificationkey = sbn.getkey(); mpreviousnotificationkeytime = sbn.getposttime();
正常创建两个变量来保存该次受到的key和posttime。然后利用sp先来获取一次上次保存的这两个值,再把该次获取的这两个值利用sp存上。
string mpreviousnotification = sphelper.getstring("notificationkey"); long mpreviousnotificationtime = sphelper.getlong("notificationtime"); sphelper.putvalues(new sphelper.contentvalue("notificationkey", mpreviousnotificationkey)); sphelper.putvalues(new sphelper.contentvalue("notificationtime", mpreviousnotificationkeytime));
然后根据两次取值判断:
if (mpreviousnotificationkey.equals(mpreviousnotification) && mpreviousnotificationkeytime.equals(mpreviousnotificationtime)) { return; }
两次值一样直接return过滤掉。
@override public void onnotificationremoved(statusbarnotification sbn)
该方法是接收到删除消息的通知。
至此,结束,方便过后有类似需求查阅。
到此这篇关于android消息通知notification的文章就介绍到这了,更多相关android消息通知内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论