向通知栏发送通知点击跳转并传递数据(PendingIntent)传递数据

 // 为发送通知的按钮的点击事件定义事件处理方法
    public void send() {
///// 第一步:获取NotificationManager
            NotificationManager nm = (NotificationManager)
                    getSystemService(Context.NOTIFICATION_SERVICE);

            ///// 第二步:定义Notification
              Intent intentAct = new Intent(this, OnePage.class);
                intentAct.putExtra("redtitil",titil);
                intentAct.putExtra("redtext",text);
            //PendingIntent是待执行的Intent
            PendingIntent pi = PendingIntent.getActivity(this, 0, intentAct,
                    PendingIntent.FLAG_UPDATE_CURRENT);
            Notification notification = new Notification.Builder(this)
                    .setContentTitle(titil)
                    .setAutoCancel(true)
                    .setContentText(text)
                    .setSmallIcon(R.mipmap.app_icon).setContentIntent(pi)
                    .build();
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notification.defaults = Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND;

            /////第三步:启动通知栏,第一个参数是一个通知的唯一标识
            nm.notify(0, notification);
    }

 


//简单翻译一下:flag不对便会导致数据传递不过去
//int FLAG_CANCEL_CURRENT:如果该PendingIntent已经存在,则在生成新的之前取消当前的。
//int FLAG_NO_CREATE:如果该PendingIntent不存在,直接返回null而不是创建一个PendingIntent.
//int FLAG_ONE_SHOT:该PendingIntent只能用一次,在send()方法执行后,自动取消。
posted @ 2016-12-23 16:09  童话二分之一  阅读(333)  评论(0编辑  收藏  举报