public static void showNotifictionIcon(Context context) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); Intent intent = new Intent(context, PlayActivity.class);//将要跳转的界面 builder.setAutoCancel(true);//点击后消失 builder.setSmallIcon(R.drawable.icon_music);//设置通知栏消息标题的头像 builder.setDefaults(NotificationCompat.DEFAULT_SOUND);//设置通知铃声 builder.setTicker("你好"); builder.setContentText("通知内容");//通知内容 builder.setContentTitle("Title"); //利用PendingIntent来包装我们的intent对象,使其延迟跳转 PendingIntent intents = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); builder.setContentIntent(intents); NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); manager.notify(0, builder.build()); }
再附上点击通知直接跳到正在运行界面的代码
//通知打开后台页面
Intent appIntent = new Intent(Intent.ACTION_MAIN); //appIntent.setAction(Intent.ACTION_MAIN); appIntent.addCategory(Intent.CATEGORY_LAUNCHER); appIntent.setComponent(new ComponentName(this.getPackageName(), this.getPackageName() + "." + this.getLocalClassName())); appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);//关键的一步,设置启动模式 PendingIntent contentIntent =PendingIntent.getActivity(this, 0,appIntent,0); builder.setContentIntent(contentIntent);