Android通过PendingIntent实现消息通知

来源:互联网 发布:在中国的外国人 知乎 编辑:程序博客网 时间:2024/06/08 11:38

当需要实现用户点击组件,系统就发送一条消息并在状态栏中提醒时。可使用PendingIntent实现这一效果:

private void notice(){        Intent intent = new Intent(this,PendingActivity.class);//即跳转到本类        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);        NotificationManager nm = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);        Notification notification = new NotificationCompat.Builder(this)                .setSmallIcon(R.mipmap.ic_launcher)                .setContentInfo("我是消息")                .setContentTitle("我是标题")                .setContentText("我是内容")                .setAutoCancel(true)                .setDefaults(Notification.DEFAULT_ALL)                .setContentIntent(pendingIntent)                .build();        nm.notify(0,notification);    }
0 0