首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Building Android notifications 2.3 through to 6.0

Building Android notifications 2.3 through to 6.0

作者头像
小小工匠
发布于 2021-08-16 02:14:29
发布于 2021-08-16 02:14:29
29400
代码可运行
举报
文章被收录于专栏:小工匠聊架构小工匠聊架构
运行总次数:0
代码可运行

Sadly ,Notification.setLatestEventInfo() is removed in API Level 23….

The base class Notification was introduced in API level 1, from the very beginning. Back then you would create your notifications by creating a new instance via the constructor and then set the various properties on it.

If you wanted to display further details on the notification you would call

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)

In API level 11 aka “Honeycomb” Android introduced the Notification.Builder API which completely encapsulates the configuration step of a notification. setLatestEventInfo() was consequently deprecated in the process.

Using it was fine, until they dropped it completely in Android 6.0. So now you can’t set your notification’s event info this way anymore. And you can’t use the Builder API in 2.3 since it’s not available yet.

To make it a bit worse, the builder API introduced in 11 used the uncommon method name getNotification() to build the notification, this was later corrected in 16 to build() as it is in all other builder APIs.

This means that if you want to have notifications with custom detail views from Android 2.3 (or earlier) until 6.0 you now have two choices:

Option A. The easy but heavy

The easy way to resolve this is to use the support-v4 library which provides you with a back port of the Notification.Builder API. You then use this exclusively for all your notifications and let it take care of the version specific stuff under the hood. This is a viable option if:

  • your app already depends on the support-v4 library or
  • you don’t care that support-v4 is about one of the largest libraries to include.

If however you’re building something that should have absolutely no external dependencies to be as small as it can possibly be, like say… a crash reporting SDK which is included in millions of apps, you might look for something else:

Option B. The light but painful

This way you fall back to old Java tricks of trade so your code paths dependent on old functionality are only executed at runtime. Reflection is your friend.

This helper class combines all the necessary steps:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class NotificationUtil {
    public static Notification createNotification(Context context, PendingIntent pendingIntent, String title, String text, int iconId) {
        Notification notification;
        if (isNotificationBuilderSupported()) {
            notification = buildNotificationWithBuilder(context, pendingIntent, title, text, iconId);
        } else {
            notification = buildNotificationPreHoneycomb(context, pendingIntent, title, text, iconId);
        }
        return notification;
    }

    public static boolean isNotificationBuilderSupported() {
        try {
            return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) && Class.forName("android.app.Notification.Builder") != null;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }

    @SuppressWarnings("deprecation")
    private static Notification buildNotificationPreHoneycomb(Context context, PendingIntent pendingIntent, String title, String text, int iconId) {
        Notification notification = new Notification(iconId, "", System.currentTimeMillis());
        try {
            // try to call "setLatestEventInfo" if available
            Method m = notification.getClass().getMethod("setLatestEventInfo", Context.class, CharSequence.class, CharSequence.class, PendingIntent.class);
            m.invoke(notification, context, title, text, pendingIntent);
        } catch (Exception e) {
            // do nothing
        }
        return notification;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressWarnings("deprecation")
    private static Notification buildNotificationWithBuilder(Context context, PendingIntent pendingIntent, String title, String text, int iconId) {
        android.app.Notification.Builder builder = new android.app.Notification.Builder(context)
            .setContentTitle(title)
            .setContentText(text)
            .setContentIntent(pendingIntent)
            .setSmallIcon(iconId);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            return builder.build();
        } else {
            return builder.getNotification();
        }
    }
}
  • It checks whether you’re on 3.0 or later and also if the Notification.Builder class is available at runtime.
  • If the builder is available it will use that - it’ll call build() if you’re running later than Jelly Bean or getNotification() from 11 through 15. By addressing the builder class directly without an import this will run on pre 3.0 devices as well.
  • Pre 3.0 it will use the classic Notification API and call setLatestEventInfo() via Reflection.
  • The annotations are in place so the compiler won’t complain or bug you.
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/02/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
信息提醒之Notification,兼容全部SDK-更新中
Notification与对话框、Toast无论从外观上还是从使用方法上有本质的区别。 Notification是Android中很理想的提示方法,Notification可以在Android桌面上最上方的状态栏显示提示信息,还可以显示图像,甚至可以将控件加载到上面,而且只要用户不清空,这些信息可以永久的保留在状态栏,除了这些还有其他更吸引人的特性,让我们一起发掘下吧。
小小工匠
2021/08/16
9700
android notification,notificationmanager详解
我们知道在使用Android的通知的时候一定会用到NotificationManager 、 Notification这两个类,这两个类的作用分别是: NotificationManager :  是状态栏通知的管理类,负责发通知、清楚通知等。 Notification:状态栏通知对象,可以设置icon、文字、提示声音、振动等等参数。 这里需要声明一点,由于Android的系统升级,Android在通知这块也有很多老的东西被抛弃了,一个是api11的版本,一个是api16的版本。我们来比较下api11之前
xiangzhihong
2018/02/05
1.7K0
android notification,notificationmanager详解
android 实现本地定时推送(兼容)
首先写几点感悟: - 做兼容真的很累很费劲~ - android 8.0 广播部分不再支持动态注册,所以应该用service来实现定时推送功能 - 无论是闹钟还是通知,都得做兼容处理,android 8.0通知必须加channel_id,否则通知无法显示 - 查阅大量资料,发现代码都参差不齐,不过还是有很多值得参考的地方,目前这份代码有很多都是抄字那些博主的文章,然后稍加改动,加以整合而成 - 代码分为三个类,service类、闹钟工具类和通知工具类
陨石坠灭
2018/10/19
3.9K0
Android适配全面总结(二)----版本适配
版权声明:本文为博主原创文章(部分引用他人博文,已加上引用说明),未经博主允许不得转载。https://www.jianshu.com/p/49fa8ebc0105
AWeiLoveAndroid
2018/09/03
2.6K0
Android适配全面总结(二)----版本适配
Android Notification使用
  在应用的开发中,我们必然会接触到应用通知这个知识,而在通知是随着Android版本进行不断变化,为了能在高版本和低版本中使用,就需要开发者去做适配,也属于查漏补缺。了解之前,先看一个效果图吧。
晨曦_LLW
2022/07/12
2.9K0
Android Notification使用
Android开发笔记(五十二)通知推送Notification
准备工作复习一下PendingIntent,前面的博文《Android开发笔记(五十)定时器AlarmManager》已经提到了它。PendingIntent意即延迟的Intent,主要用于非立即响应的通信场合。上回的博文,博主介绍了PendingIntent的用法,下面再列出有用到它的场合: 1、用于定时器AlarmManager,处理时刻到达后的提醒动作 2、用于通知推送Notification,处理点击通知后的相应动作 3、用于远程视图RemoteViews,处理远程控件上的点击动作 4、用于发送短信SmsManager,处理短信发送完的后续动作
aqi00
2019/01/18
2.9K0
Android实现进程保活方案解析
众所周知,日活率是一款App的核心绩效指标,日活量不仅反应了应用的受欢迎程度,同时反应了产品的变现能力,进而直接影响盈利能力和企业估值。为了抢占市场,谁都不会放过任何一个可以提高应用日活的方法,所以App进程保活都是各大厂商,特别是头部应用开发商永恒的追求,毕竟一旦 App 进程死亡,那就再也无法在用户的手机上开展任何业务,所有的商业模型在用户侧都没有立足之地。
SoullessCoder
2021/02/02
9.6K0
Android实现进程保活方案解析
0703-APP-Notification-statue-bar
1.展示显示textTicker和仅仅有icon的两种情况:当參数showTicker为true时显示否则不显示
全栈程序员站长
2022/07/11
3590
Android Notification细思极恐的适配
近期项目的迭代版本开发,部门惊喜的申请了一台9.0的机器,是目前部门有史以来第一台8.0以上的机器,满怀喜悦的跑起项目,惊讶地发现Notification的在9.0以上的机器突然不能弹出通知了,惊讶之余发现发通知管理的权限没有开启(就觉得在我的代码怎么会有问题),结果开启了仍然无法接收到通知(打脸了...),马上请教了google大神,发现了毛病
包子388321
2020/06/16
1.4K0
Notification与Widget(其实没怎么讲)Android应用界面开发
Notification与Widget,他们为什么要一起讲呢?因为他们很相似,甚至自定义界面的方法都是一样的,这点可能很多书里没有写
爱因斯坦福
2018/09/10
1.5K0
[android] notification入门
通知栏,对话框,Toast是我们接触的三个提示框,通知栏是在系统的应用com.adnroid.systemui当中的
唯一Chat
2019/09/10
5570
Android 中使用通知Kotlin 版
在 MainActivity 中重写 onRequestPermissionsResult:
龙小雨
2025/05/16
1080
Android Notification的用法
旧版本 api11中废弃(Android 3.0) String service = NOTIFICATION_SERVICE; nManager = (NotificationManager) this.getSystemService(service); notification = new Notification(); // 通知提示 String tickerText = "状态栏上显示的消息"; // 显示时间 long when = System.currentTimeMillis();
码客说
2019/10/22
6980
Glide类似You cannot start a load for a destroyed activity异常简单分析
根据异常的提示,我们可以确定问题应该是出在了Glide.with(context) 中的context 我们点到源码中看一下 Glide.with() 是怎么实现的。
程思扬
2022/01/10
4290
Glide类似You cannot start a load for a destroyed activity异常简单分析
Android Notification
经常熬夜有三大害处:第一,记忆力越来越差;第二,数学水平下降;第四,记忆力越来越差。
大公爵
2018/09/05
1.8K0
Android Notification
Android通知Notification使用全解析,看这篇就够了
通知是 Android 在您的应用 UI 之外显示的消息,用于向用户提供提醒、来自其他人的通信或来自您的应用的其他及时信息。用户可以点击通知打开您的应用或直接从通知中执行操作。
yechaoa
2022/06/27
7.2K0
Android通知Notification使用全解析,看这篇就够了
Android之Notification介绍
Notification就是在桌面的状态通知栏。这主要涉及三个主要类: Notification:设置通知的各个属性。 NotificationManager:负责发送通知和取消通知 Notification.Builder:Notification内之类,创建Notification对象。非常方便的控制所有的flags,同时构建Notification的风格。 主要作用: 1.创建一个状态条图标。 2.在扩展的状态条窗口中显示额外的信息(和启动一个Intent)。 3.闪灯或LED。 4.电话震动。 5.
欢醉
2018/01/22
1.2K0
Android之Notification介绍
相关推荐
信息提醒之Notification,兼容全部SDK-更新中
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档