{"id":13643732,"url":"https://github.com/linglongxin24/NotificationUtil","last_synced_at":"2025-04-21T02:31:25.068Z","repository":{"id":90164313,"uuid":"73463559","full_name":"linglongxin24/NotificationUtil","owner":"linglongxin24","description":" 全新的Android通知栏,已抛弃setLatestEventInfo,兼容高版本","archived":false,"fork":false,"pushed_at":"2016-11-17T05:14:42.000Z","size":14136,"stargazers_count":259,"open_issues_count":1,"forks_count":65,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-03-31T08:44:15.027Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://blog.csdn.net/linglongxin24/article/details/53166551","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/linglongxin24.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-11-11T09:21:28.000Z","updated_at":"2025-03-23T08:46:53.000Z","dependencies_parsed_at":"2023-07-05T13:31:32.077Z","dependency_job_id":null,"html_url":"https://github.com/linglongxin24/NotificationUtil","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linglongxin24%2FNotificationUtil","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linglongxin24%2FNotificationUtil/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linglongxin24%2FNotificationUtil/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linglongxin24%2FNotificationUtil/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/linglongxin24","download_url":"https://codeload.github.com/linglongxin24/NotificationUtil/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249986063,"owners_count":21356317,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T01:01:51.889Z","updated_at":"2025-04-21T02:31:20.039Z","avatar_url":"https://github.com/linglongxin24.png","language":"Java","readme":"#全新的Android通知栏,已抛弃setLatestEventInfo,兼容高版本\n\u003e这算是一个入门级的Android通知栏notification的文章，因为在项目中要用到，又发现以前的低版本的用setLatestEventInfo已过时，还报错，完全不兼容。所以，在这里介绍下基本用法，代码比较简单，高手请略过。\n\n先看效果图\n\n\n\u003cdiv\u003e\n\u003cimg src=\"https://github.com/linglongxin24/NotificationUtil/blob/master/screenshorts/main.png?raw=true\" width=\"30%\" height=\"30%\"/\u003e\n\u003cimg src=\"https://github.com/linglongxin24/NotificationUtil/blob/master/screenshorts/effect.png?raw=true\" width=\"30%\" height=\"30%\" style = \"margin:20px\"/\u003e\n\u003cimg src=\"https://github.com/linglongxin24/NotificationUtil/blob/master/screenshorts/effect2.png?raw=true\" width=\"30%\" height=\"30%\" style = \"margin:20px\"/\u003e\n\u003c/div\u003e\n\n#1.主要参数介绍\n\n![图片1](https://github.com/linglongxin24/NotificationUtil/blob/master/screenshorts/demo1.png?raw=true)\n![图片2](https://github.com/linglongxin24/NotificationUtil/blob/master/screenshorts/demo2.png?raw=true)\n![图片3](https://github.com/linglongxin24/NotificationUtil/blob/master/screenshorts/demo3.png?raw=true)\n\n * 1.notification的title\n * 2.发送notification的application的icon或者发送者的image。\n * 3.notification的message\n * 4.notification的数目显示\n * 5.当main icon用来显示sender’s image时 Secondary icon用来显示发送application的icon。\n * 6.时间戳，默认为系统发出通知的时间。\n#2.显示一个普通的通知栏\n\n```\n    /**\n     * 显示一个普通的通知\n     *\n     * @param context 上下文\n     */\n    public static void showNotification(Context context) {\n        Notification notification = new NotificationCompat.Builder(context)\n                /**设置通知左边的大图标**/\n                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))\n                /**设置通知右边的小图标**/\n                .setSmallIcon(R.mipmap.ic_launcher)\n                /**通知首次出现在通知栏，带上升动画效果的**/\n                .setTicker(\"通知来了\")\n                /**设置通知的标题**/\n                .setContentTitle(\"这是一个通知的标题\")\n                /**设置通知的内容**/\n                .setContentText(\"这是一个通知的内容这是一个通知的内容\")\n                /**通知产生的时间，会在通知信息里显示**/\n                .setWhen(System.currentTimeMillis())\n                /**设置该通知优先级**/\n                .setPriority(Notification.PRIORITY_DEFAULT)\n                /**设置这个标志当用户单击面板就可以让通知将自动取消**/\n                .setAutoCancel(true)\n                /**设置他为一个正在进行的通知。他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待,因此占用设备(如一个文件下载,同步操作,主动网络连接)**/\n                .setOngoing(false)\n                /**向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置，使用defaults属性，可以组合：**/\n                .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND)\n                .setContentIntent(PendingIntent.getActivity(context, 1, new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT))\n                .build();\n        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);\n        /**发起通知**/\n        notificationManager.notify(0, notification);\n    }\n````\n#3.显示一个下载带进度条的通知\n\n```\n    /**\n     * 显示一个下载带进度条的通知\n     *\n     * @param context 上下文\n     */\n    public static void showNotificationProgress(Context context) {\n        //进度条通知\n        final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(context);\n        builderProgress.setContentTitle(\"下载中\");\n        builderProgress.setSmallIcon(R.mipmap.ic_launcher);\n        builderProgress.setTicker(\"进度条通知\");\n        builderProgress.setProgress(100, 0, false);\n        final Notification notification = builderProgress.build();\n        final NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);\n        //发送一个通知\n        notificationManager.notify(2, notification);\n        /**创建一个计时器,模拟下载进度**/\n        Timer timer = new Timer();\n        timer.schedule(new TimerTask() {\n            int progress = 0;\n\n            @Override\n            public void run() {\n                Log.i(\"progress\", progress + \"\");\n                while (progress \u003c= 100) {\n                    progress++;\n                    try {\n                        Thread.sleep(100);\n                    } catch (InterruptedException e) {\n                        // TODO Auto-generated catch block\n                        e.printStackTrace();\n                    }\n                    //更新进度条\n                    builderProgress.setProgress(100, progress, false);\n                    //再次通知\n                    notificationManager.notify(2, builderProgress.build());\n                }\n                //计时器退出\n                this.cancel();\n                //进度条退出\n                notificationManager.cancel(2);\n                return;//结束方法\n            }\n        }, 0);\n    }\n\n```\n#4.显示一个悬挂式的通知\n\n```\n    /**\n     * 悬挂式,支持6.0以上系统\n     *\n     * @param context\n     */\n    public static void showFullScreen(Context context) {\n        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);\n        Intent mIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(\"http://blog.csdn.net/itachi85/\"));\n        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, mIntent, 0);\n        builder.setContentIntent(pendingIntent);\n        builder.setSmallIcon(R.mipmap.ic_launcher);\n        builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher));\n        builder.setAutoCancel(true);\n        builder.setContentTitle(\"悬挂式通知\");\n        //设置点击跳转\n        Intent hangIntent = new Intent();\n        hangIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);\n        hangIntent.setClass(context, MainActivity.class);\n        //如果描述的PendingIntent已经存在，则在产生新的Intent之前会先取消掉当前的\n        PendingIntent hangPendingIntent = PendingIntent.getActivity(context, 0, hangIntent, PendingIntent.FLAG_CANCEL_CURRENT);\n        builder.setFullScreenIntent(hangPendingIntent, true);\n        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);\n        notificationManager.notify(3, builder.build());\n    }\n\n````\n#5.显示一个折叠式的通知\n\n```\n    /**\n     * 折叠式\n     *\n     * @param context\n     */\n    public static void shwoNotify(Context context) {\n        //先设定RemoteViews\n        RemoteViews view_custom = new RemoteViews(context.getPackageName(), R.layout.view_custom);\n        //设置对应IMAGEVIEW的ID的资源图片\n        view_custom.setImageViewResource(R.id.custom_icon, R.mipmap.icon);\n        view_custom.setTextViewText(R.id.tv_custom_title, \"今日头条\");\n        view_custom.setTextColor(R.id.tv_custom_title, Color.BLACK);\n        view_custom.setTextViewText(R.id.tv_custom_content, \"金州勇士官方宣布球队已经解雇了主帅马克-杰克逊，随后宣布了最后的结果。\");\n        view_custom.setTextColor(R.id.tv_custom_content, Color.BLACK);\n        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);\n        mBuilder.setContent(view_custom)\n                .setContentIntent(PendingIntent.getActivity(context, 4, new Intent(context, MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT))\n                .setWhen(System.currentTimeMillis())// 通知产生的时间，会在通知信息里显示\n                .setTicker(\"有新资讯\")\n                .setPriority(Notification.PRIORITY_HIGH)// 设置该通知优先级\n                .setOngoing(false)//不是正在进行的   true为正在进行  效果和.flag一样\n                .setSmallIcon(R.mipmap.icon);\n        Notification notify = mBuilder.build();\n        NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);\n        notificationManager.notify(4, notify);\n    }\n```\n\n#6.[GitHub](https://github.com/linglongxin24/NotificationUtil)\n","funding_links":[],"categories":["通知"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinglongxin24%2FNotificationUtil","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinglongxin24%2FNotificationUtil","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinglongxin24%2FNotificationUtil/lists"}