Android 通知栏(Notification)点击跳转页面

1,通知栏应用:

Notification,是一种具有全局效果的通知,可以在系统的通知栏中显示。当 APP 向系统发出通知时,它将先以图标的形式显示在通知栏中。用户可以下拉通知栏查看通知的详细信息。通知栏和抽屉式通知栏均是由系统控制,用户可以随时查看。 

2,代码:

【1】显示通知栏。跳转页面   

//[1]获取通知的管理者

        NotificationManager barmanager = (NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);

        Notification notice;

        //[2] 获取Notification

        Notification.Builder builder = new Notification.Builder(this).setTicker("我是测试内容")

                .setSmallIcon(R.mipmap.ic_launcher).setWhen(System.currentTimeMillis());

        //开启另一个页面

        Intent appIntent = new Intent(this,SplashActivity.class);

        appIntent.setAction(Intent.ACTION_MAIN);

        appIntent.addCategory(Intent.CATEGORY_LAUNCHER);

        //设置启动模式

        appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);



        PendingIntent contentIntent =PendingIntent.getActivity(this, 0,appIntent,PendingIntent.FLAG_UPDATE_CURRENT);



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

            //设置通知栏中的内容

            notice = builder.setContentIntent(contentIntent).setContentTitle("我是标题").setContentText("我是内容").build();

            notice.flags=Notification.FLAG_AUTO_CANCEL;

            barmanager.notify(10,notice);

        }

【2】取消通知ID是上面设置的

   barmanager.cancel(10);

【3】XML要配置打开的Activity。singleTask模式

<activity android:name=".SplashActivity"

android:launchMode="singleTask"

android:taskAffinity=""

android:excludeFromRecents="true"

/>

猜你喜欢

转载自blog.csdn.net/Cricket_7/article/details/85102318