Android_Notificaiton

Paste_Image.png

设计文档译文(http://adchs.github.io/patterns/notifications.html)

功能作用

1.显示接收到短消息、即使消息等信息 (如QQ、微信、新浪、短信)
2.显示客户端的推送消息(如有新版本发布,广告,推荐新闻等)
3.显示正在进行的事物(例如:后台运行的程序)(如音乐播放器、版本更新时候的下载进度等)

基本使用

public static NotificationManager getNotificationManager(Context context) {
    NotificationManager instance = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    return instance;
}

public static Notification getNotification(Context context, Messager msg) {
    Notification notification = new Builder(context)
            .setContentTitle(msg.title)
            .setContentText(msg.message)
            .setContentIntent(getDefalutIntent(context, msg.action, Notification.FLAG_AUTO_CANCEL))
            .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
            //.setPriority(Notification.PRIORITY_DEFAULT) // 设置该通知优先级
            .setAutoCancel(true)//设置这个标志当用户单击面板就可以让通知将自动取消
            .setOngoing(false)// ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
            .setDefaults(Notification.DEFAULT_VIBRATE)// 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
            .build();// 设置通知小ICON .build();

    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notification.icon = msg.icon;
    return notification;
}

private static PendingIntent getDefalutIntent(Context context, String action, int flag) {
    Intent intent = new Intent();
    if(action.startsWith("http")){
        Uri uri = Uri.parse(action);
        intent = new Intent(Intent.ACTION_VIEW, uri);
    } else {
        
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, flag);
    return pendingIntent;
}

public static void sendNotification(Context context, Messager msg) {
    getNotificationManager(context).notify(msg.id, getNotification(context, msg));
}

推荐使用(兼容性问题)(NotificationCompat)

public static Notification getNotification(Context context, Messager msg) {
    int icon = 0;
    try {
        ApplicationInfo info = context.getPackageManager()
                .getApplicationInfo(context.getPackageName(), 0);
        icon = info.icon;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    Notification notification = new NotificationCompat.Builder(context)
            .setContentTitle(msg.title)
            .setContentText(msg.message)
            .setSmallIcon(icon)
            .setAutoCancel(true)
            .setContentIntent(
                    getDefalutIntent(context, msg,
                            PendingIntent.FLAG_UPDATE_CURRENT))
            .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示,一般是系统获取到的时间
            // .setPriority(Notification.PRIORITY_DEFAULT) // 设置该通知优先级
            .setAutoCancel(true)// 设置这个标志当用户单击面板就可以让通知将自动取消
            .setOngoing(false)// ture,设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)
            .setDefaults(NotificationCompat.DEFAULT_VIBRATE)// 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置,使用defaults属性,可以组合
            .build();// 设置通知小ICON .build();

    return notification;
}

指定parent、独立存在(新task)

  • 添加到 parent task
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back 
stackstackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back 
stackPendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
  • new Task
private static PendingIntent getDefalutIntent(Context context,
        Messager msg, int flag) {
    Intent intent = new Intent();
    intent.putExtra(BKWebViewActivity.TITLE, msg.title);
    intent.putExtra(BKWebViewActivity.URL, msg.url);
    intent.putExtra(BKWebViewActivity.HTML, msg.html);
    intent.setClassName(context, "packname.WebViewActivity");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, msg.id, intent, flag);
    return pendingIntent;
}

<activity
        android:name=".WebViewActivity"
        android:configChanges="fontScale|orientation|keyboardHidden|locale|navigation|screenSize|uiMode"
        android:taskAffinity=""
        android:launchMode="singleTask"
        android:excludeFromRecents="true"
        android:screenOrientation="portrait" />

RemoteViews 自定义布局

public static Notification getCustomNotification(Context context, Messager msg) {
    Notification notification = new Notification();
    
    RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notify);
    notification.contentView = remoteView;
    notification.contentIntent = getDefalutIntent(context, msg.action, Notification.FLAG_AUTO_CANCEL);
    remoteView.setOnClickPendingIntent(R.id.notify_layout, getDefalutIntent(context, msg.action, Notification.FLAG_AUTO_CANCEL));
    return notification;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,504评论 18 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,952评论 4 60
  • 一,研究背景: 起点Android客户端670版本有个需求,就是游戏下载在通知栏显示。为了把其功能实现的优雅,特此...
    最有文化的码农阅读 541评论 0 5
  • 借着打哈欠,眼泪肆意的流出。这时的你在众人面前勿需掩饰,勿需尴尬,大方得任其自流!你可以不用手背抺去,可以不用纸巾...
    眸念阅读 251评论 0 2
  • 看到这个标题,是不是觉得特别的熟,甚至俗气。没办法,想跟别人谈谈,最近参加公司的年会,表演节目,有很多的感悟,一开...
    暖姐阅读 142评论 0 1