android Notification 的使用

NotificationManager mNotificationManager
= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification
 = new Notification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());             

notification.flags = Notification.FLAG_AUTO_CANCEL;                
Intent notificationIntent = new Intent(arg0.getContext(), NotificationShow.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);           
//PendingIntent
PendingIntent contentIntent = PendingIntent.getActivity(
        arg0.getContext(), 
        R.string.app_name, 
        notificationIntent, 
        PendingIntent.FLAG_UPDATE_CURRENT);
                 
notification.setLatestEventInfo(
        arg0.getContext(),
        "Hello,there!", 
        "Hello,there,I'm john.", 
        contentIntent);
mNotificationManager.notify(R.string.app_name, n);   

下面依次对每一段代码进行分析:

NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

创建 NotificationManager,其中创建的 mNotificationManager 对象负责“发出”与“取消” Notification。

Notification notification = newNotification(R.drawable.chat, "Hello,there!", System.currentTimeMillis());            
notification.flags = Notification.FLAG_ONGOING_EVENT; 

设置 notification.flags 为 Notification.FLAG_AUTO_CANCEL ,该标志表示当用户点击 Clear 之后,能够清除该通知。

Intent notificationIntent 
= new Intent(arg0.getContext(), NotificationShow.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);

创建一个Intent,该Intent使得当用户点击该通知后发出这个Intent。

请注意,如果要以该Intent启动一个Activity,一定要设置 Intent.FLAG_ACTIVITY_NEW_TASK 标记。

Intent.FLAG_ACTIVITY_CLEAR_TOP :如果在当前Task中,有要启动的Activity,那么把该Acitivity之前的所有Activity都关掉,并把此Activity置前以避免创建Activity的实例
Intent.FLAG_ACTIVITY_NEW_TASK :系统会检查当前所有已创建的Task中是否有该要启动的Activity的Task,若有,则在该Task上创建Activity,若没有则新建具有该Activity属性的Task,并在该新建的Task上创建Activity。

notification.setLatestEventInfo(
    arg0.getContext(),
    "Hello,there!", 
    "Hello,there,I'm john.", 
    contentIntent);

设置显示在通知下拉框中的信息,参数依次为:Context,标题,内容,PendingIntent。

mNotificationManager.notify(R.string.app_name, n);

启动Notification,参数依次为:在你的程序中标识Notification的id值(用来区分同一程序中的不同Notifycation,如果程序中只有一个Notification那么这里随便你填什么都可以,不过类型必须要为int),要通知的Notification。
如何使自己的Notification像Android QQ一样能出现在 “正在运行的”栏目下面
其实很简单,只需设置Notification.flags = Notification.FLAG_ONGOING_EVENT;便可以了。
如何改变 Notification 在“正在运行的”栏目下面的布局
创建 RemoteViews 并赋给 Notification.contentView ,再把 PendingIntent 赋给 Notification.contentIntent 便可以了,如:

PendingIntent contentIntent = PendingIntent.getActivity(  
arg0.getContext(),    
R.string.app_name,    
notificationIntent,   
PendingIntent.FLAG_UPDATE_CURRENT);
      
RemoteViews rv = new RemoteViews(Main.this.getPackageName(), R.layout.notification_view);

rv.setImageViewResource(R.id.image, R.drawable.chat);
rv.setTextViewText(R.id.text, "Hello,there,I'm john.");

notification.contentView = rv;

notification.contentIntent = contentIntent;

mNotificationManager.notify(R.string.app_name, n);

如果使用了contentView,那么便不要使用Notification.setLatestEventInfo。如果setLatestEventInfo在赋给 Notification.contentView 的代码之后,那么contentView的效果将被覆盖,显示的便是 setLatestEventInfo 的效果;
如果 setLatestEventInfo 在 Notification.contentView 的代码之前,那么显示的便是 Notification.contentView 的效果,也就是说不管你想要setLatestEventInfo 或 contentView 的自定义效果,请保证始终只有一句设置代码,因为在最后一句绑定的时候,之前的设置contentView或setLatestEventInfo的代码都是完全没有必要的。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容