Notification 相关设置以及兼容性

本文详细介绍了在Android系统中实现程序通知的兼容性设置,包括如何在不同版本的Android系统上正确配置通知栏的相关属性,以确保通知能够顺利收发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于Android 的系统通知有时候会收不到的情况,如下给出一些兼容性的设置,代码如下:

  /** 
     * 程序通知,覆盖start
     * 通知栏相关属性设置 */
  @SuppressLint("NewApi")
  private void setNotification() { NotificationManager notificationManager = (NotificationManager) this .getSystemService(android.content.Context.NOTIFICATION_SERVICE); // 设置通知的事件消息 CharSequence contentTitle = getString("标题"); // 通知栏标题 CharSequence contentText = getString("内容"); // 通知栏内容 Intent notificationIntent = new Intent(); notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER); notificationIntent.setClass(this, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0); //以下针对不同版本的兼容设置 if (android.os.Build.VERSION.SDK_INT > 11) { //notification.builder Builder builder = new Notification.Builder(this); builder.setContentIntent(contentItent).setSmallIcon(R.drawable.ic_noti) //设置状态栏里面的图标(小图标)                        .setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_notification))//下拉列表里面的图标(大图标)           .setTicker(contentText) //设置状态栏的显示的信息   .setWhen(System.currentTimeMillis()) //设置时间发生时间   .setAutoCancel(false) //设置可以清除   .setOngoing(true)   .setContentTitle(contentTitle) //设置下拉列表里的标题   .setContentText(contentText); //设置上下文内容 Notification notification = builder.getNotification(); notification.flags |= Notification.FLAG_ONGOING_EVENT; //将此通知放到通知栏的"Ongoing"即"正在运行"组中 notification.flags |= Notification.FLAG_NO_CLEAR; //表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用 notification.defaults = Notification.DEFAULT_LIGHTS; notificationManager.notify(0, notification); }else { Notification notification = new Notification(
         R.drawable.ic_notification, getString(R.string.app_name),   System.currentTimeMillis()); notification.flags
|= Notification.FLAG_ONGOING_EVENT; // 将此通知放到通知栏的"Ongoing"即"正在运行"组中 notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在点击了通知栏中的"清除通知"后,此通知不清除,经常与FLAG_ONGOING_EVENT一起使用 notification.defaults = Notification.DEFAULT_LIGHTS; notification.setLatestEventInfo(this, contentTitle, contentText, contentItent); // 把Notification传递给NotificationManager notificationManager.notify(0, notification); } }

 

转载于:https://www.cnblogs.com/fly-allblue/p/4000470.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值