android 通知的创建

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

推荐你阅读:

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

Create a Notification Builder


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

例如:

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 notification
    int mNotificationId = 001;
    // Gets an instance of the NotificationManager service
    NotificationManager 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);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值