android 通知的创建

来源:互联网 发布:js判断数字大于等于0 编辑:程序博客网 时间:2024/05/16 04:49

  1. 创建通知的builder
  2. 定义通知的action
  3. 设置通知点击的事件
  4. 发出通知

推荐你阅读:

  • Notifications API Guide
  • Intents and Intent Filters
  • Notifications Design Guide

注:使用NotificationCompat.Builder 类需要因引入support包

Create a Notification Builder


 在创建一个Notification时 NotificationCompat.Builder 用于定义其ui 。Builder 对象至少包含以下几个方法:

  •  setSmallIcon()设置图标
  •  setContentTitle()设置标题
  • setContentText() 设置详情文本

例如:

NotificationCompat.Builder mBuilder =    new NotificationCompat.Builder(this)    .setSmallIcon(R.drawable.notification_icon)    .setContentTitle("My notification")    .setContentText("Hello World!");

 定义Notification的行为


尽管action是可选的,建议为你的Notification添加一个。action 可以让你直接从通知栏转跳到你应用的activity中,在其中可以查看引发notification的事件和做相应的操作。在notification内部,action通过一个包含 Intent 的PendingIntent启动一个activity

如何构建 PendingIntent 取决于你启动 Activity 的类型。当你启动Activity 时候,你要遵循用户使用notification的经验。 在下面的例子中,点击通知打开了一个有效扩展notification的新的activity.,因此没有必要创建一个人工回退堆栈。 (从 在启动Activity的时候保存通知 获取更多信息):

Intent resultIntent = new Intent(this, ResultActivity.class);...// Because clicking the notification opens a new ("special") activity, there's// no need to create an artificial back stack.PendingIntent resultPendingIntent =    PendingIntent.getActivity(    this,    0,    resultIntent,    PendingIntent.FLAG_UPDATE_CURRENT);

设置notification的点击行为


用一个手势和之前的创建的 PendingIntent 关联,调用NotificationCompat.Builder适当的方法。例如通过 setContentIntent()添加 PendingIntent,可以在用户点击通的文本的时候启动一个activity。 如下:

PendingIntent resultPendingIntent;...mBuilder.setContentIntent(resultPendingIntent);

发出通知


发出通知要满足以下条件:

  • 获取 NotificationManager.的实例
  • 使用 notify() 方法发出通知. 当调用 notify(), 方法添加特定的id.你可以通过这个id来更新notification.在Managing Notifications有更详细的描述.
  • 调用 build(), 返回 包含说明  Notification 的对象.

    For example:

    NotificationCompat.Builder mBuilder;...// Sets an ID for the notificationint mNotificationId = 001;// Gets an instance of the NotificationManager serviceNotificationManager mNotifyMgr =         (NotificationManager) getSystemService(NOTIFICATION_SERVICE);// Builds the notification and issues it.mNotifyMgr.notify(mNotificationId, mBuilder.build());
    完整代码如下:
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    this).setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText("Hello World!");
            Intent resultIntent = new Intent(this, AActicity.class);
            // Because clicking the notification opens a new ("special") activity,
            // there's
            // no need to create an artificial back stack.
            PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
                    resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);
            int mNotificationId = 001;
            // Gets an instance of the NotificationManager service
            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            // Builds the notification and issues it.
            Notification notification = mBuilder.build();
            //notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; //该行代码可以使通知常驻通知栏 
            mNotifyMgr.notify(mNotificationId, notification);
原创粉丝点击